public async Task ExecuteGroupAsync(CommandContext ctx) { if (this.Shared.IsEventRunningInChannel(ctx.Channel.Id)) { if (this.Shared.GetEventInChannel(ctx.Channel.Id) is NumberRace) { await this.JoinRaceAsync(ctx); } else { throw new CommandFailedException("Another event is already running in the current channel."); } return; } var race = new NumberRace(ctx.Client.GetInteractivity(), ctx.Channel); this.Shared.RegisterEventInChannel(race, ctx.Channel.Id); try { await this.InformAsync(ctx, StaticDiscordEmoji.Clock1, $"The race will start in 30s or when there are 10 participants. Use command {Formatter.InlineCode("game numberrace")} to join the race."); await this.JoinRaceAsync(ctx); await Task.Delay(TimeSpan.FromSeconds(30)); if (race.ParticipantCount > 1) { await race.RunAsync(); if (race.IsTimeoutReached) { if (race.Winner is null) { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, "No replies, aborting the game..."); } else { await this.InformAsync(ctx, StaticDiscordEmoji.Trophy, $"{race.Winner.Mention} won due to no replies from other users!"); } } else { await this.InformAsync(ctx, StaticDiscordEmoji.Trophy, $"Winner is: {race.Winner.Mention}! GGWP!"); } if (!(race.Winner is null)) { await this.Database.UpdateStatsAsync(race.Winner.Id, s => s.NumberRacesWon++); } } else { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, "Not enough users joined the race."); } } finally { this.Shared.UnregisterEventInChannel(ctx.Channel.Id); } }
public async Task ExecuteGroupAsync(CommandContext ctx) { if (this.Service.IsEventRunningInChannel(ctx.Channel.Id)) { if (this.Service.GetEventInChannel(ctx.Channel.Id) is NumberRace) { await this.JoinAsync(ctx); } else { throw new CommandFailedException(ctx, "cmd-err-evt-dup"); } return; } var game = new NumberRace(ctx.Client.GetInteractivity(), ctx.Channel); this.Service.RegisterEventInChannel(game, ctx.Channel.Id); try { await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-game-nr-start", NumberRace.MaxParticipants); await this.JoinAsync(ctx); await Task.Delay(TimeSpan.FromSeconds(30)); if (game.ParticipantCount > 1) { GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>(); await game.RunAsync(this.Localization); if (game.Winner is { }) { if (game.IsTimeoutReached) { await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "cmd-err-game-timeout-w", game.Winner.Mention); } else { await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "fmt-winners", game.Winner.Mention); } await gss.UpdateStatsAsync(game.Winner.Id, s => s.NumberRacesWon++); }