コード例 #1
0
        public override Task <bool> ExecuteCheckAsync(CommandContext ctx, bool help)
        {
            if (!ctx.Services.GetRequiredService <BotActivityService>().IsBotListening)
            {
                return(Task.FromResult(false));
            }
            if (ctx.Services.GetRequiredService <BlockingService>().IsBlocked(ctx.Guild?.Id, ctx.Channel.Id, ctx.User.Id))
            {
                return(Task.FromResult(false));
            }
            if (BlockingCommandRuleExists())
            {
                return(Task.FromResult(false));
            }

            if (!help)
            {
                LogExt.Debug(ctx, "Executing {Command} in message: {Message}", ctx.Command?.QualifiedName ?? "<unknown cmd>", ctx.Message.Content);
            }

            return(Task.FromResult(true));


            bool BlockingCommandRuleExists()
            {
                if (ctx.Guild is null || ctx.Command is null)
                {
                    return(false);
                }
                CommandRulesService?crs = ctx.Services.GetRequiredService <CommandRulesService>();

                return(crs?.IsBlocked(ctx.Command.QualifiedName, ctx.Guild.Id, ctx.Channel.Id, ctx.Channel.Parent?.Id) ?? false);
            }
        }
コード例 #2
0
        public static async Task MessageCreateEventHandlerAsync(TheGodfatherBot bot, MessageCreateEventArgs e)
        {
            if (e.Author.IsBot)
            {
                return;
            }

            if (e.Guild is null)
            {
                LogExt.Debug(bot.GetId(null), new[] { "DM message received from {User}:", "{Message}" }, e.Author, e.Message);
                return;
            }

            if (bot.Services.GetRequiredService <BlockingService>().IsBlocked(e.Guild.Id, e.Channel.Id, e.Author.Id))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(e.Message?.Content))
            {
                return;
            }

            if (!e.Message.Content.StartsWith(bot.Services.GetRequiredService <GuildConfigService>().GetGuildPrefix(e.Guild.Id)))
            {
                short rank = bot.Services.GetRequiredService <UserRanksService>().ChangeXp(e.Guild.Id, e.Author.Id);
                if (rank != 0)
                {
                    LocalizationService ls = bot.Services.GetRequiredService <LocalizationService>();
                    LevelRole?          lr = await bot.Services.GetRequiredService <LevelRoleService>().GetAsync(e.Guild.Id, rank);

                    DiscordRole?levelRole = lr is { } ? e.Guild.GetRole(lr.RoleId) : null;
コード例 #3
0
        public async Task ListAsync(CommandContext ctx)
        {
            IReadOnlyList <PrivilegedUser> privileged = await this.Service.GetAsync();

            var notFound = new List <PrivilegedUser>();
            var valid    = new List <DiscordUser>();

            foreach (PrivilegedUser pu in privileged)
            {
                try {
                    DiscordUser user = await ctx.Client.GetUserAsync(pu.UserId);

                    valid.Add(user);
                } catch (NotFoundException) {
                    LogExt.Debug(ctx, "Found 404 privileged user: {UserId}", pu.UserId);
                    notFound.Add(pu);
                }
            }

            if (!valid.Any())
            {
                throw new CommandFailedException(ctx, "cmd-err-choice-none");
            }

            await ctx.PaginateAsync(
                "str-priv",
                valid,
                user => user.ToString(),
                this.ModuleColor,
                10
                );

            LogExt.Information(ctx, "Removing {Count} not found privileged users", notFound.Count);
            await this.Service.RemoveAsync(notFound);
        }
コード例 #4
0
        public async Task BaseAddAsync(CommandContext ctx, string?reason, params T[] entities)
        {
            if (reason?.Length >= 60)
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-rsn", BlockedEntity.ReasonLimit);
            }

            if (entities is null || !entities.Any())
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-block-none");
            }

            int blocked = await this.BlockAsync(entities.Select(c => c.Id), reason);

            await ctx.InfoAsync(this.ModuleColor, "fmt-block-add", blocked);

            if (typeof(T) == typeof(DiscordGuild))
            {
                foreach (T entity in entities)
                {
                    try {
                        DiscordGuild g = await ctx.Client.GetGuildAsync(entity.Id);

                        await g.LeaveAsync();
                    } catch {
                        LogExt.Debug(ctx, "Failed to find/leave guild: {Id}", entity.Id);
                    }
                }
            }
        }
コード例 #5
0
        public static async Task MessageReactionAddedEventHandlerAsync(TheGodfatherBot bot, MessageReactionAddEventArgs e)
        {
            if (e.Guild is null || e.Channel is null || e.Message is null)
            {
                return;
            }

            StarboardService ss = bot.Services.GetRequiredService <StarboardService>();

            if (ss.IsStarboardEnabled(e.Guild.Id, out ulong cid, out string star) && cid != e.Channel.Id && e.Emoji.GetDiscordName() == star)
            {
                LogExt.Debug(bot.GetId(e.Guild.Id), "Reacted with star emoji: Message {MessageId}, {Guild}", e.Message.Id, e.Guild);
                ss.RegisterModifiedMessage(e.Guild.Id, e.Channel.Id, e.Message.Id);
            }

            ReactionRoleService rrs = bot.Services.GetRequiredService <ReactionRoleService>();
            ReactionRole?       rr  = await rrs.GetAsync(e.Guild.Id, e.Emoji.GetDiscordName());

            if (rr is { })
コード例 #6
0
        public static async Task GuildMemberJoinEventHandlerAsync(TheGodfatherBot bot, GuildMemberAddEventArgs e)
        {
            if (e.Guild is null)
            {
                return;
            }

            LogExt.Debug(bot.GetId(e.Guild.Id), "Member added: {Member} {Guild}", e.Member, e.Guild);

            GuildConfigService gcs  = bot.Services.GetRequiredService <GuildConfigService>();
            GuildConfig        gcfg = await gcs.GetConfigAsync(e.Guild.Id);

            await Task.Delay(TimeSpan.FromSeconds(gcfg.AntiInstantLeaveSettings.Cooldown + 1));

            if (e.Member.Guild is null)     // User left in meantime
            {
                return;
            }

            // TODO move to service
            DiscordChannel?wchn = e.Guild.GetChannel(gcfg.WelcomeChannelId);

            if (wchn is { })