using CommandLine; using DiscordClient; using DiscordClient.Data; using DiscordDelete; 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(args) .MapResult( 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; }, 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 => Task.FromResult(0) );