public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                }

                DiscordDmChannel?dm = await ctx.Client.CreateDmChannelAsync(ctx.User.Id);

                if (dm is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-dm-create");
                }

                await dm.LocalizedEmbedAsync(this.Localization, Emojis.Question, this.ModuleColor, "q-game-hm");

                await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Question, "fmt-game-hm", ctx.User.Mention);

                string?word = null;

                for (int i = 0; i < 5; i++)
                {
                    DiscordMessage?reply = await ctx.WaitForDmReplyAsync(dm, ctx.User);

                    if (string.IsNullOrWhiteSpace(reply?.Content))
                    {
                        await ctx.FailAsync("cmd-err-game-hm");

                        return;
                    }
                    if (reply.Content.All(c => char.IsLetter(c)))
                    {
                        word = reply.Content;
                        break;
                    }
                    await dm.LocalizedEmbedAsync(this.Localization, Emojis.Question, this.ModuleColor, "cmd-err-game-hm-format");
                }

                if (word is null)
                {
                    await ctx.FailAsync("cmd-err-game-hm");

                    return;
                }

                await dm.LocalizedEmbedAsync(this.Localization, Emojis.Information, this.ModuleColor, "fmt-game-hm-ok", word);

                var game = new HangmanGame(ctx.Client.GetInteractivity(), ctx.Channel, word, ctx.User);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await game.RunAsync(this.Localization);

                    if (game.Winner is { })
                    {
                        GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                        await gss.UpdateStatsAsync(game.Winner.Id, s => s.HangmanWon++);
                    }
                } finally {
Ejemplo n.º 2
0
            public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Shared.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    throw new CommandFailedException("Another event is already running in the current channel!");
                }

                DiscordDmChannel dm = await ctx.Client.CreateDmChannelAsync(ctx.User.Id);

                if (dm == null)
                {
                    throw new CommandFailedException("Please enable direct messages, so I can ask you about the word to guess.");
                }

                await dm.EmbedAsync("What is the secret word?", StaticDiscordEmoji.Question, this.ModuleColor);

                await this.InformAsync(ctx, StaticDiscordEmoji.Question, $"{ctx.User.Mention}, check your DM. When you give me the word, the game will start.");

                MessageContext mctx = await ctx.Client.GetInteractivity().WaitForMessageAsync(
                    xm => xm.Channel == dm && xm.Author.Id == ctx.User.Id,
                    TimeSpan.FromMinutes(1)
                    );

                if (mctx == null)
                {
                    await this.InformFailureAsync(ctx, "I didn't get the word, so I will abort the game.");

                    return;
                }
                else
                {
                    await dm.EmbedAsync($"Alright! The word is: {Formatter.Bold(mctx.Message.Content)}", StaticDiscordEmoji.Information, this.ModuleColor);
                }

                var hangman = new HangmanGame(ctx.Client.GetInteractivity(), ctx.Channel, mctx.Message.Content, mctx.User);

                this.Shared.RegisterEventInChannel(hangman, ctx.Channel.Id);
                try {
                    await hangman.RunAsync();

                    if (hangman.Winner != null)
                    {
                        await this.Database.UpdateUserStatsAsync(hangman.Winner.Id, GameStatsType.HangmansWon);
                    }
                } finally {
                    this.Shared.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }