NadekoBot/src/NadekoBot/Modules/Games/Commands/HangmanCommands.cs

78 lines
2.7 KiB
C#
Raw Normal View History

2016-12-17 00:16:14 +00:00
using Discord.Commands;
2016-12-14 23:56:13 +00:00
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
2017-02-18 20:07:36 +00:00
using NadekoBot.Modules.Games.Hangman;
using Discord;
2017-05-27 08:19:27 +00:00
using Discord.WebSocket;
2016-12-14 23:56:13 +00:00
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
2017-02-19 14:18:40 +00:00
public class HangmanCommands : NadekoSubmodule
2016-12-14 23:56:13 +00:00
{
private readonly DiscordSocketClient _client;
2017-05-27 08:19:27 +00:00
public HangmanCommands(DiscordSocketClient client)
2017-05-27 08:19:27 +00:00
{
_client = client;
}
2016-12-14 23:56:13 +00:00
//channelId, game
public static ConcurrentDictionary<ulong, HangmanGame> HangmanGames { get; } = new ConcurrentDictionary<ulong, HangmanGame>();
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Hangmanlist()
2016-12-14 23:56:13 +00:00
{
await Context.Channel.SendConfirmAsync(Format.Code(GetText("hangman_types", Prefix)) + "\n" + string.Join(", ", HangmanTermPool.data.Keys));
2016-12-14 23:56:13 +00:00
}
2017-01-08 07:57:26 +00:00
2016-12-14 23:56:13 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Hangman([Remainder]string type = "All")
2016-12-14 23:56:13 +00:00
{
2017-05-27 08:19:27 +00:00
var hm = new HangmanGame(_client, Context.Channel, type);
2016-12-14 23:56:13 +00:00
2016-12-16 18:43:57 +00:00
if (!HangmanGames.TryAdd(Context.Channel.Id, hm))
2016-12-14 23:56:13 +00:00
{
await ReplyErrorLocalized("hangman_running").ConfigureAwait(false);
2016-12-14 23:56:13 +00:00
return;
}
hm.OnEnded += g =>
2016-12-14 23:56:13 +00:00
{
2017-05-27 08:19:27 +00:00
HangmanGames.TryRemove(g.GameChannel.Id, out HangmanGame throwaway);
2016-12-14 23:56:13 +00:00
};
try
{
hm.Start();
}
2017-01-11 12:54:25 +00:00
catch (Exception ex)
{
try { await Context.Channel.SendErrorAsync(GetText("hangman_start_errored") + " " + ex.Message).ConfigureAwait(false); } catch { }
2017-05-27 08:19:27 +00:00
HangmanGames.TryRemove(Context.Channel.Id, out HangmanGame throwaway);
2017-01-11 12:54:25 +00:00
throwaway.Dispose();
return;
}
2016-12-14 23:56:13 +00:00
await Context.Channel.SendConfirmAsync(GetText("hangman_game_started"), hm.ScrambledWord + "\n" + hm.GetHangman());
2016-12-14 23:56:13 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task HangmanStop()
{
if (HangmanGames.TryRemove(Context.Channel.Id, out HangmanGame throwaway))
{
throwaway.Dispose();
await ReplyConfirmLocalized("hangman_stopped").ConfigureAwait(false);
}
}
2016-12-14 23:56:13 +00:00
}
}
}