private async Task Client_ChannelPinsUpdated(ChannelPinsUpdateEventArgs e) { if (e.Channel.Guild == null) { return; } using (_services.CreateScope()) { var db = _services.GetService <PinsDbContext>(); var info = await db.Guilds .Include(p => p.PinnedMessages) .FirstOrDefaultAsync(b => b.Id == (long)e.Channel.Guild.Id); if (info == null) { _logger.LogInformation("Ignoring pins update for {0} because the guild isn't in the database.", e.Channel); return; } if (e.Channel.IsNSFW && !info.IncludeNSFW) { _logger.LogInformation("Ignoring pins update for {0} because it's marked as NSFW.", e.Channel); return; } var hook = _webhookClient.GetRegisteredWebhook((ulong)info.WebhookId); if (hook == null) { _logger.LogInformation("Ignoring pins update for {0} because the webhook is unavailable.", e.Channel); return; } var pins = await e.Channel.GetPinnedMessagesAsync(); var newPins = pins.Reverse().Where(p => !info.PinnedMessages.Any(m => m.Id == (long)p.Id)); foreach (var pin in newPins) { await CopyPinAsync(hook, pin, info, db); } await db.SaveChangesAsync(); } }
public async Task MigrateAsync(CommandContext ctx) { var info = await _database.Guilds .Include(p => p.PinnedMessages) .FirstOrDefaultAsync(b => b.Id == (long)ctx.Guild.Id); if (info == null) { // TODO: prompt to reconfigure/setup _logger.LogError("Unable to migrate pins in {0} because it isn't setup.", ctx.Guild); await ctx.RespondAsync("Pinned message redireciton isn't enabled in this server!"); return; } var hook = _webhookClient.GetRegisteredWebhook((ulong)info.WebhookId); var channels = ctx.Guild.Channels.Values .Where(c => c.Type == ChannelType.Text && (!c.IsNSFW || info.IncludeNSFW)) .Where(c => c.PermissionsFor(ctx.Guild.CurrentMember).HasPermission(Permissions.AccessChannels | Permissions.ReadMessageHistory)) .ToList(); var messages = new List <DiscordMessage>(); var message = await ctx.RespondAsync($"Migrating messages for {channels.Count()} channels, this may take a while!"); foreach (var channel in channels) { messages.AddRange(await channel.GetPinnedMessagesAsync()); } messages = messages.Where(m => !info.PinnedMessages.Any(i => i.Id == (long)m.Id)).ToList(); await message.ModifyAsync($"Migrating {messages.Count} messages from {channels.Count()} channels, this may take a while!"); foreach (var msg in messages.OrderBy(m => m.Timestamp)) { await _service.CopyPinAsync(hook, msg, info, _database); } await ctx.RespondAsync("Pins copied!"); await _database.SaveChangesAsync(); }