public static async Task SendErrorMessageByException(IGuild guild, ISocketMessageChannel channel, IUser user, string source, Exception exception)
        {
            // Try to get the Exception as a LocalizedException
            LocalizedException localizedException = exception as LocalizedException;

            // Check if this really is a LocalizedException
            if (localizedException != null)
            {
                // Send the error message
                await SendErrorMessageByLocalizedDescription(guild, channel, localizedException.Message);
            }
            else
            {
                // Notify the logging channel
                await DiscordUtil.HandleException(exception, source, guild, channel, user);

                // Send the error message
                await SendErrorMessageByTypeAndMessage(guild, channel, exception.GetType().Name, exception.Message, true);
            }
        }
Example #2
0
        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);
                    }
                }
            }
        }