NadekoBot/NadekoBot.Modules.Games/TriviaCommands.cs

98 lines
3.6 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
2017-05-27 08:19:27 +00:00
using Discord.WebSocket;
using NadekoBot.Extensions;
2017-05-27 08:19:27 +00:00
using NadekoBot.Services;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Games.Common.Trivia;
using NadekoBot.Modules.Games.Services;
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
public class TriviaCommands : NadekoSubmodule<GamesService>
{
private readonly CurrencyService _cs;
private readonly DiscordSocketClient _client;
private readonly IBotConfigProvider _bc;
2017-05-27 08:19:27 +00:00
public TriviaCommands(DiscordSocketClient client, IBotConfigProvider bc, CurrencyService cs)
2017-05-27 08:19:27 +00:00
{
_cs = cs;
2017-05-27 08:19:27 +00:00
_client = client;
_bc = bc;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-31 10:55:12 +00:00
public Task Trivia([Remainder] string additionalArgs = "")
2017-03-23 11:09:15 +00:00
=> InternalTrivia(10, additionalArgs);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2017-03-23 11:09:15 +00:00
public Task Trivia(int winReq = 10, [Remainder] string additionalArgs = "")
=> InternalTrivia(winReq, additionalArgs);
public async Task InternalTrivia(int winReq, string additionalArgs = "")
{
var channel = (ITextChannel)Context.Channel;
additionalArgs = additionalArgs?.Trim()?.ToLowerInvariant();
var showHints = !additionalArgs.Contains("nohint");
2017-03-23 11:09:15 +00:00
var isPokemon = additionalArgs.Contains("pokemon");
var trivia = new TriviaGame(_strings, _client, _bc, _cs, channel.Guild, channel, showHints, winReq, isPokemon);
if (_service.RunningTrivias.TryAdd(channel.Guild.Id, trivia))
{
try
{
await trivia.StartGame().ConfigureAwait(false);
}
finally
{
_service.RunningTrivias.TryRemove(channel.Guild.Id, out trivia);
await trivia.EnsureStopped().ConfigureAwait(false);
}
2017-03-23 11:09:15 +00:00
return;
}
2017-03-23 11:09:15 +00:00
await Context.Channel.SendErrorAsync(GetText("trivia_already_running") + "\n" + trivia.CurrentQuestion)
.ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Tl()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (_service.RunningTrivias.TryGetValue(channel.Guild.Id, out TriviaGame trivia))
2016-12-25 07:10:27 +00:00
{
await channel.SendConfirmAsync(GetText("leaderboard"), trivia.GetLeaderboard()).ConfigureAwait(false);
2016-12-25 07:10:27 +00:00
return;
}
await ReplyErrorLocalized("trivia_none").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Tq()
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (_service.RunningTrivias.TryGetValue(channel.Guild.Id, out TriviaGame trivia))
{
await trivia.StopGame().ConfigureAwait(false);
2016-12-25 07:10:27 +00:00
return;
}
2016-12-25 07:10:27 +00:00
await ReplyErrorLocalized("trivia_none").ConfigureAwait(false);
}
}
}
}