Add configuration capabilities.

This commit is contained in:
femsci 2023-02-23 10:27:59 +01:00
parent 139fc375e4
commit 3f2fd049fb
Signed by: femsci
GPG key ID: 08F7911F0E650C67
6 changed files with 87 additions and 19 deletions

View file

@ -8,8 +8,8 @@ public class Delete
_cli = Utils.CreateClient();
}
private HttpClient _cli;
private List<Message> _messages;
private readonly HttpClient _cli;
private readonly List<Message> _messages;
public async Task<int> StartDeletion()
{

View file

@ -8,7 +8,7 @@ public record Response
public int TotalResults { get; set; }
[JsonPropertyName("messages")]
public IEnumerable<IEnumerable<Message>> MessagesInternal { get; set; } = null!;
public IEnumerable<IEnumerable<Message>> MessagesInternal { get; set; } = Array.Empty<IEnumerable<Message>>();
[JsonIgnore]
public ICollection<Message> Messages => MessagesInternal.Select(m => m.First()).ToList();
@ -32,3 +32,15 @@ public record IdChn
[JsonPropertyName("chn")]
public ulong Channel { get; set; }
}
public class UserData
{
[JsonPropertyName("token")]
public string Token { get; set; } = "";
[JsonPropertyName("cfcookie")]
public string Cookie { get; set; } = "";
[JsonPropertyName("type")]
public string Type { get; set; } = "DM";
[JsonPropertyName("myId")]
public ulong UserId { get; set; }
}

View file

@ -1,11 +1,26 @@
using Disforget;
int pages = int.Parse(args[0]);
ulong channel = ulong.Parse(args[1]);
string channelArg;
if (args.Length > 0)
{
channelArg = args[0];
}
else
{
Console.Write($"Enter {(Utils.Data.Type.ToLower() == "dm" ? "channel" : "guild")} ID...\n> ");
channelArg = Console.ReadLine()!;
}
if (!ulong.TryParse(channelArg, out ulong channel))
{
Console.WriteLine($"Invalid channel argument: {args[0]}. Expected a positive integer...");
Environment.Exit(-1);
return;
}
Console.WriteLine("Starting...");
Search search = new(channel, pages);
Search search = new(channel);
var messages = await search.Perform();

View file

@ -5,28 +5,27 @@ namespace Disforget;
public class Search
{
public Search(ulong channel, int pages)
public Search(ulong channel)
{
_channel = channel;
_pages = pages;
_cli = Utils.CreateClient();
}
private HttpClient _cli;
private List<Message> _messages = new();
private ulong _channel;
private int _pages, _total;
private readonly HttpClient _cli;
private readonly List<Message> _messages = new();
private readonly ulong _channel;
private int _pages = -1, _total;
public async Task<List<Message>> Perform()
{
int page = 0;
while (page < _pages)
do
{
await DoSearch(page);
await Task.Delay(1500);
page++;
}
} while (page < _pages);
Console.WriteLine($"\nFound and processed {_messages.Count} messages.");
@ -35,7 +34,7 @@ public class Search
private async Task DoSearch(int page)
{
var reqstr = $"https://discord.com/api/v9/guilds/790954455621304330/messages/search?author_id=340510759588986882&channel_id={_channel}&include_nsfw=true{(page > 0 ? $"&offset={page * 25}" : "")}";
var reqstr = $"https://discord.com/api/v9/{(Utils.Data.Type.ToLower() == "dm" ? "channels" : "guilds")}/{_channel}/messages/search?author_id={Utils.Data.UserId}&include_nsfw=true{(page > 0 ? $"&offset={page * 25}" : "")}";
REQ:
var resp = await _cli.GetAsync(reqstr);
@ -56,9 +55,21 @@ public class Search
}
_total = result.TotalResults;
_pages = (int)Math.Ceiling(_total / 25.0);
_messages.AddRange(result.Messages);
Console.Write($"\x1b[2K\rRequest {++page}/{_pages} Status: {resp.StatusCode} ({_messages.Count}/{_total})");
}
public override bool Equals(object? obj)
{
return obj is Search search &&
_channel == search._channel;
}
public override int GetHashCode()
{
return HashCode.Combine(_cli, _messages, _channel, _pages, _total);
}
}

View file

@ -1,7 +1,31 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Disforget;
public static class Utils
{
static Utils()
{
if (!File.Exists("./config.json"))
{
Console.WriteLine("Cannot find config.json file! Aborting...");
Environment.Exit(-4);
return;
}
Data = JsonSerializer.Deserialize<UserData>(File.ReadAllText("./config.json"))!;
if (Data == null)
{
Console.WriteLine("Invalid config.json file! Aborting...");
Environment.Exit(-5);
return;
}
}
public static readonly UserData Data;
public static void AddHeader(this HttpClient client, string h, string v)
{
if (!client.DefaultRequestHeaders.TryAddWithoutValidation(h, v))
@ -18,10 +42,10 @@ public static class Utils
cli.AddHeader("authority", "discord.com");
cli.AddHeader("x-discord-locale", "en-US");
cli.AddHeader("accept-language", "en-US");
cli.AddHeader("authorization", $"");
cli.AddHeader("authorization", $"{Data.Token}");
cli.AddHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) discord/0.0.21 Chrome/91.0.4472.164 Electron/13.6.6 Safari/537.36");
cli.AddHeader("accept", "*/*");
cli.AddHeader("cookie", "");
cli.AddHeader("cookie", $"{Data.Cookie}");
return cli;
}

6
config.json Normal file
View file

@ -0,0 +1,6 @@
{
"token": "someBase64.Data.ThatDiscordUsesForAuthentication",
"cfcookie": "some=cookies; from=browser",
"type": "DM",
"myId": 21370000
}