public async Task Acro(int time = 60) { if (time < 10 || time > 120) { return; } var channel = (ITextChannel)Context.Channel; var game = new AcrophobiaGame(channel, time); if (AcrophobiaGames.TryAdd(channel.Id, game)) { try { await game.Run(); } finally { game.EnsureStopped(); AcrophobiaGames.TryRemove(channel.Id, out game); } } else { await ReplyErrorLocalized("acro_running").ConfigureAwait(false); } }
public async Task Acro(params string[] args) { var(options, _) = OptionsParser.ParseFrom(new AcrophobiaGame.Options(), args); var channel = (ITextChannel)ctx.Channel; var game = new AcrophobiaGame(options); if (_service.AcrophobiaGames.TryAdd(channel.Id, game)) { try { game.OnStarted += Game_OnStarted; game.OnEnded += Game_OnEnded; game.OnVotingStarted += Game_OnVotingStarted; game.OnUserVoted += Game_OnUserVoted; _client.MessageReceived += _client_MessageReceived; await game.Run().ConfigureAwait(false); } finally { _client.MessageReceived -= _client_MessageReceived; _service.AcrophobiaGames.TryRemove(channel.Id, out game); game.Dispose(); } } else { await ReplyErrorLocalizedAsync("acro_running").ConfigureAwait(false); } Task _client_MessageReceived(SocketMessage msg) { if (msg.Channel.Id != ctx.Channel.Id) { return(Task.CompletedTask); } var _ = Task.Run(async() => { try { var success = await game.UserInput(msg.Author.Id, msg.Author.ToString(), msg.Content) .ConfigureAwait(false); if (success) { await msg.DeleteAsync().ConfigureAwait(false); } } catch { } }); return(Task.CompletedTask); } }
public async Task Acro(int time = 60) { var channel = (ITextChannel)Context.Channel; var game = new AcrophobiaGame(channel, time); if (AcrophobiaGames.TryAdd(channel.Id, game)) { try { await game.Run(); } finally { game.EnsureStopped(); AcrophobiaGames.TryRemove(channel.Id, out game); } } else { await channel.SendErrorAsync("Acrophobia game is already running in this channel.").ConfigureAwait(false); } }