Esempio n. 1
0
        private static async Task CheckMessageAsync(DiscordClient client, DiscordChannel channel, DiscordUser user, DiscordMessage message, DiscordEmoji emoji, bool isBacklog)
        {
            try
            {
                if (user.IsBotSafeCheck() || channel.IsPrivate)
                {
                    return;
                }

                // in case it's not in cache and doesn't contain any info, including Author
                message = await channel.GetMessageAsync(message.Id).ConfigureAwait(false);

                if (emoji == Config.Reactions.Starbucks)
                {
                    await CheckMediaTalkAsync(client, channel, message, emoji).ConfigureAwait(false);
                }
                if (emoji == Config.Reactions.Shutup && !isBacklog)
                {
                    await ShutupAsync(client, user, message).ConfigureAwait(false);
                }
                if (emoji == Config.Reactions.BadUpdate && !isBacklog)
                {
                    await BadUpdateAsync(client, user, message, emoji).ConfigureAwait(false);
                }

                await CheckGameFansAsync(client, channel, message).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Config.Log.Error(e);
            }
        }
Esempio n. 2
0
        public async Task Remove(CommandContext ctx, [Description("Discord user to remove from forced nickname list.")] DiscordUser discordUser)
        {
            try
            {
                if (discordUser.IsBotSafeCheck())
                {
                    var mem = ctx.Client.GetMember(ctx.Guild.Id, discordUser);
                    if (mem is not null)
                    {
                        await mem.ModifyAsync(m => m.Nickname = new(discordUser.Username)).ConfigureAwait(false);

                        await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
                    }
                    return;
                }

                await using var db = new BotDb();
                var enforcedRules = ctx.Guild == null
                    ? await db.ForcedNicknames.Where(mem => mem.UserId == discordUser.Id).ToListAsync().ConfigureAwait(false)
                    : await db.ForcedNicknames.Where(mem => mem.UserId == discordUser.Id && mem.GuildId == ctx.Guild.Id).ToListAsync().ConfigureAwait(false);

                if (enforcedRules.Count == 0)
                {
                    return;
                }

                db.ForcedNicknames.RemoveRange(enforcedRules);
                await db.SaveChangesAsync().ConfigureAwait(false);

                foreach (var rule in enforcedRules)
                {
                    if (ctx.Client.GetMember(rule.GuildId, discordUser) is DiscordMember discordMember)
                    {
                        try
                        {
                            //todo: change to mem.Nickname = default when the library fixes their shit
                            await discordMember.ModifyAsync(mem => mem.Nickname = new(discordMember.Username)).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            Config.Log.Debug(ex);
                        }
                    }
                }
                await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Config.Log.Error(e);
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to reset user nickname").ConfigureAwait(false);
            }
        }
Esempio n. 3
0
        public async Task Rename(CommandContext ctx,
                                 [Description("Discord user to add to forced nickname list.")] DiscordUser discordUser,
                                 [Description("Nickname which should be displayed."), RemainingText] string expectedNickname)
        {
            if (!await new RequiresBotModRole().ExecuteCheckAsync(ctx, false).ConfigureAwait(false))
            {
                return;
            }

            try
            {
                if (expectedNickname.Length is < 2 or > 32)
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, "Nickname must be between 2 and 32 characters long", true).ConfigureAwait(false);

                    return;
                }

                if ((!expectedNickname.All(c => char.IsLetterOrDigit(c) ||
                                           char.IsWhiteSpace(c) ||
                                           char.IsPunctuation(c)) ||
                     expectedNickname.Any(c => c is ':' or '#' or '@' or '`')
                     ) && !discordUser.IsBotSafeCheck())
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, "Nickname must follow Rule 7", true).ConfigureAwait(false);

                    return;
                }

                List <DiscordGuild> guilds;
                if (ctx.Guild == null)
                {
                    guilds = ctx.Client.Guilds?.Values.ToList() ?? new List <DiscordGuild>(0);
                    if (guilds.Count > 1)
                    {
                        await ctx.Channel.SendMessageAsync($"{discordUser.Mention} will be renamed in all {guilds.Count} servers").ConfigureAwait(false);
                    }
                }
                else
                {
                    guilds = new(){ ctx.Guild }
                };

                int changed = 0, noPermissions = 0, failed = 0;
                await using var db = new BotDb();
                foreach (var guild in guilds)
                {
                    if (!discordUser.IsBotSafeCheck())
                    {
                        var enforceRules = db.ForcedNicknames.FirstOrDefault(mem => mem.UserId == discordUser.Id && mem.GuildId == guild.Id);
                        if (enforceRules is null)
                        {
                            enforceRules = new() { UserId = discordUser.Id, GuildId = guild.Id, Nickname = expectedNickname };
                            await db.ForcedNicknames.AddAsync(enforceRules).ConfigureAwait(false);
                        }
                        else
                        {
                            if (enforceRules.Nickname == expectedNickname)
                            {
                                continue;
                            }
                            enforceRules.Nickname = expectedNickname;
                        }
                    }
                    if (!(ctx.Guild?.Permissions?.HasFlag(Permissions.ChangeNickname) ?? true))
                    {
                        noPermissions++;
                        continue;
                    }

                    if (ctx.Client.GetMember(guild, discordUser) is DiscordMember discordMember)
                    {
                        try
                        {
                            await discordMember.ModifyAsync(x => x.Nickname = expectedNickname).ConfigureAwait(false);

                            changed++;
                        }
                        catch (Exception ex)
                        {
                            Config.Log.Warn(ex, "Failed to change nickname");
                            failed++;
                        }
                    }
                }
                await db.SaveChangesAsync().ConfigureAwait(false);

                if (guilds.Count > 1)
                {
                    if (changed > 0)
                    {
                        await ctx.ReactWithAsync(Config.Reactions.Success, $"Forced nickname for {discordUser.Mention} in {changed} server{(changed == 1 ? "" : "s")}", true).ConfigureAwait(false);
                    }
                    else
                    {
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to force nickname for {discordUser.Mention} in any server").ConfigureAwait(false);
                    }
                }
                else
                {
                    if (changed > 0)
                    {
                        await ctx.ReactWithAsync(Config.Reactions.Success, $"Forced nickname for {discordUser.Mention}", true).ConfigureAwait(false);
                    }
                    else if (failed > 0)
                    {
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to force nickname for {discordUser.Mention}").ConfigureAwait(false);
                    }
                    else if (noPermissions > 0)
                    {
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"No permissions to force nickname for {discordUser.Mention}").ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                Config.Log.Error(e);
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to change nickname, check bot's permissions").ConfigureAwait(false);
            }
        }