コード例 #1
0
        public async Task ClearCommandMessages(CommandContext ctx, int amount = 10)
        {
            if (!ctx.BotCan(Permissions.ManageMessages))
            {
                await ctx.ReplyAsync("The bot needs the permission to Manage Messages to clear messages!\n" +
                                     "Also, game inputs are cleared automatically when the bot has this permission enabled.");

                return;
            }

            if (amount < 1 || amount > 100)
            {
                await ctx.ReplyAsync("Please choose a reasonable number of messages to delete.");

                return;
            }

            IEnumerable <DiscordMessage> toDelete = await ctx.Channel.GetMessagesAsync();

            toDelete = toDelete.Where(x => x.Author.Id == ctx.Client.CurrentUser.Id ||
                                      x.Content.StartsWith(Storage.GetGuildPrefix(ctx.Guild)) ||
                                      Input.MentionPrefix.IsMatch(x.Content));

            toDelete = toDelete.Take(amount);

            if (toDelete.Any())
            {
                await ctx.Channel.DeleteMessagesAsync(toDelete);
            }
        }
コード例 #2
0
ファイル: UnoModule.cs プロジェクト: superbar13/Pac-Man-Bot
        public async Task JoinUno(CommandContext ctx, DiscordMember member = null)
        {
            bool self = false;

            if (member is null)
            {
                self   = true;
                member = ctx.Member;
            }

            if (Game(ctx) is null)
            {
                await ctx.RespondAsync($"There's no Uno game in this channel! Use `{ctx.Prefix}uno` to start.");

                return;
            }
            if (Game(ctx).UserId.Contains(member.Id))
            {
                await ctx.RespondAsync($"{(self ? "You're" : "They're")} already playing!");

                return;
            }
            if (!self && !member.IsBot)
            {
                await ctx.RespondAsync($"{member.Mention} You're being invited to play {Game(ctx).GameName}.\nDo `{Storage.GetPrefix(ctx)}uno join` to join.");

                return;
            }

            string failReason = await Game(ctx).TryAddPlayerAsync(member);

            if (failReason is null)
            {
                await DeleteGameMessageAsync(ctx);
                await RespondGameAsync(ctx);
            }
            else
            {
                await ctx.RespondAsync($"{member.Mention} {"You ".If(self)}can't join this game: {failReason}");
            }

            if (ctx.BotCan(Permissions.ManageMessages))
            {
                await ctx.Message.DeleteAsync();
            }
            else
            {
                await ctx.AutoReactAsync(failReason is null);
            }

            if (Game(ctx)?.State == GameState.Cancelled)
            {
                EndGame(ctx);
            }
        }
コード例 #3
0
ファイル: MiscModule.cs プロジェクト: superbar13/Pac-Man-Bot
        public async Task CancelGame(CommandContext ctx)
        {
            var game = Game(ctx);

            if (game is null)
            {
                await ctx.ReplyAsync("There is no active game in this channel!");

                return;
            }

            if (ctx.Guild is null || game.UserId.Contains(ctx.User.Id) || ctx.UserCan(Permissions.ManageMessages) ||
                DateTime.Now - game.LastPlayed > TimeSpan.FromSeconds(60))
            {
                var msg = await game.GetMessageAsync();

                EndGame(ctx);
                if (msg is not null)
                {
                    await game.UpdateMessageAsync();
                }

                if (game is PacManGame pacManGame)
                {
                    if (ctx.Guild is not null)
                    {
                        await ctx.RespondAsync($"Game ended.\n**Result:** {pacManGame.score} points in {pacManGame.Time} turns");
                    }
                    if (msg is not null && ctx.BotCan(Permissions.ManageMessages))
                    {
                        try { await msg.DeleteAllReactionsAsync(); }
                        catch (NotFoundException) { }
                    }
                }
                else
                {
                    await ctx.AutoReactAsync();
                }
            }