mirror of
https://onedev.fprog.nl/DiscordClient
synced 2025-12-29 08:18:36 +01:00
Initial implementation for delete and scan.
This commit is contained in:
parent
4baac35064
commit
2acfa2ed0d
|
|
@ -1,16 +1,112 @@
|
|||
using CommandLine;
|
||||
using DiscordClient;
|
||||
using DiscordClient.Data;
|
||||
using DiscordDelete;
|
||||
|
||||
CommandLine
|
||||
string? token = Environment.GetEnvironmentVariable("DISCORD_TOKEN");
|
||||
|
||||
if (token == null)
|
||||
{
|
||||
// Exit.
|
||||
return;
|
||||
}
|
||||
|
||||
DiscordContext db = new DiscordContext();
|
||||
DiscordUserClient client = new DiscordUserClient(token);
|
||||
|
||||
int finalCode = await CommandLine
|
||||
.Parser.Default.ParseArguments<DeleteOptions, ScanOptions>(args)
|
||||
.MapResult(
|
||||
(DeleteOptions opt) =>
|
||||
async (DeleteOptions opt) =>
|
||||
{
|
||||
foreach (Message message in db.Messages)
|
||||
{
|
||||
// If required data is null, continue to next message.
|
||||
if (message.MessageId is null || message.ChannelId is null)
|
||||
continue;
|
||||
|
||||
HttpResponseMessage response = await client.DeleteMessage(
|
||||
message.ChannelId,
|
||||
message.MessageId
|
||||
);
|
||||
message.LastHttpCode = (int)response.StatusCode;
|
||||
|
||||
db.Update(message);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return 0;
|
||||
},
|
||||
(ScanOptions opt) =>
|
||||
async (ScanOptions opt) =>
|
||||
{
|
||||
// This should never happen as those args are required.
|
||||
if (opt.ChannelId is null || opt.Author is null)
|
||||
return 1;
|
||||
|
||||
int totalMessages = 1;
|
||||
int offset = 0;
|
||||
|
||||
if (opt.GuildId is null)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// Scanning direct messages.
|
||||
ChannelMessagesResponse[]? messages = await client.GetChannelMessages(
|
||||
opt.ChannelId,
|
||||
offset,
|
||||
opt.Author
|
||||
);
|
||||
|
||||
if (messages is null)
|
||||
return 2;
|
||||
|
||||
await db.AddRangeAsync(
|
||||
messages.Select(message => new Message
|
||||
{
|
||||
ChannelId = message.ChannelId,
|
||||
MessageId = message.Id,
|
||||
Content = message.Content
|
||||
})
|
||||
);
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
offset += 50;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (offset < totalMessages)
|
||||
{
|
||||
// Scanning guild messages.
|
||||
GuildMessageSearchResponse? response = await client.SearchGuildMessages(
|
||||
opt.GuildId,
|
||||
offset,
|
||||
opt.Author
|
||||
);
|
||||
|
||||
if (response is null || response.Messages is null)
|
||||
return 3;
|
||||
|
||||
totalMessages = response.TotalMessages;
|
||||
|
||||
await db.AddRangeAsync(
|
||||
response.Messages.SelectMany(message =>
|
||||
message.Select(messagePart => new Message
|
||||
{
|
||||
ChannelId = messagePart.ChannelId,
|
||||
MessageId = messagePart.Id,
|
||||
Content = messagePart.Content
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
offset += response.Messages.Count();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
},
|
||||
errs => 1
|
||||
errs => Task.FromResult(0)
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue