public static async Task FindBadGuilds(bool ignoreExcessiveAmount = false) { // Create a copy of the GuildSettings List <GuildSettings> allGuildSettings = new List <GuildSettings>(Configuration.LoadedConfiguration.DiscordConfig.GuildSettings); // Create a list for deregistration candidates List <SocketGuildChannel> deregistrationCandidates = new List <SocketGuildChannel>(); foreach (GuildSettings guildSettings in allGuildSettings) { foreach (KeyValuePair <ulong, DynamicSettingsData> pair in guildSettings.ChannelSettings) { // Get the channel SocketGuildChannel channel = (SocketGuildChannel)DiscordBot.GetChannel(pair.Key); // Check if the channel no longer exists if (channel == null) { continue; } // Get the Permissions ChannelPermissions permissions = channel.Guild.CurrentUser.GetPermissions(channel); // Check if we can't write to this channel if (!permissions.Has(ChannelPermission.SendMessages)) { deregistrationCandidates.Add(channel); } } } // Skip deregistration if there are a large number of guilds to deregister if (deregistrationCandidates.Count > 5 && !ignoreExcessiveAmount) { await DiscordBot.LoggingChannel.SendMessageAsync($"**[DiscordUtil]** Skipping deregistration, there seems to be an excessive amount of guilds to deregister ({deregistrationCandidates.Count})"); return; } foreach (SocketGuildChannel guildChannel in deregistrationCandidates) { // Remove the channel settings Configuration.LoadedConfiguration.DiscordConfig.GuildSettings.Where(g => g.GuildId == guildChannel.Guild.Id).FirstOrDefault().ChannelSettings.TryRemove(guildChannel.Id, out DynamicSettingsData data); // Send a message to this server that their guild has been deregistered Embed embed = new EmbedBuilder() .WithTitle("Warning") .WithDescription(Localizer.Localize("discord.guild.deregister", (Language)data.GetSetting("language"))) .WithColor(Color.Orange) .Build(); await DiscordBot.SendMessageToFirstWritableChannel(guildChannel.Guild, embed : embed); await DiscordBot.LoggingChannel.SendMessageAsync($"**[Guild]** Deregistered \"#{guildChannel.Name}\" ({guildChannel.Id}) on \"{guildChannel.Guild.Name}\""); } }
public static async Task ProcessJoinedGuild(SocketGuild socketGuild) { // Build an Embed Embed embed = new EmbedBuilder() .WithTitle("Welcome") .WithDescription(Localizer.Localize("discord.guild.join", Language.EnglishUS)) .WithColor(Color.Blue) .Build(); await DiscordBot.SendMessageToFirstWritableChannel(socketGuild, embed : embed); await DiscordBot.LoggingChannel.SendMessageAsync($"**[Guild]** Joined \"{socketGuild.Name}\" ({socketGuild.Id})"); }
public static async Task SendNotificationAsync(Predicate <DynamicSettingsData> shouldPost, string message = null, Dictionary <Language, Embed> localizedEmbeds = null) { // Make a copy of the GuildSettings list just in case it is modified while notifications are sent List <GuildSettings> allGuildSettings = new List <GuildSettings>(Configuration.LoadedConfiguration.DiscordConfig.GuildSettings); foreach (GuildSettings guildSettings in allGuildSettings) { foreach (KeyValuePair <ulong, DynamicSettingsData> pair in guildSettings.ChannelSettings) { // Run the Predicate to see if a message should be sent if (!shouldPost(pair.Value)) { continue; } // Get the guild SocketGuild socketGuild = DiscordBot.GetGuild(guildSettings.GuildId); // Get the channel SocketTextChannel textChannel = socketGuild.GetTextChannel(pair.Key); // Get the Embed if it exists Embed embed = localizedEmbeds != null ? localizedEmbeds[(Language)pair.Value.GetSetting("language")] : null; // Send the message if possible try { await textChannel.SendMessageAsync(text : message, embed : embed); } catch (Exception exception) { await DiscordUtil.HandleException(exception, "in ``SendNotificationsAsync()``", socketGuild, textChannel); } } } }