NadekoBot/NadekoBot.Modules.Games/SpeedTypingCommands.cs

120 lines
4.3 KiB
C#
Raw Normal View History

2016-09-01 01:12:08 +00:00
using Discord;
using Discord.Commands;
using Discord.WebSocket;
2016-09-01 01:12:08 +00:00
using NadekoBot.Extensions;
using Newtonsoft.Json;
using System.IO;
2016-09-01 01:12:08 +00:00
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Games.Common;
using NadekoBot.Modules.Games.Services;
2016-09-01 01:12:08 +00:00
namespace NadekoBot.Modules.Games
{
public partial class Games
2016-09-01 01:12:08 +00:00
{
[Group]
public class SpeedTypingCommands : NadekoSubmodule<GamesService>
2016-09-01 01:12:08 +00:00
{
2017-05-27 08:19:27 +00:00
private readonly GamesService _games;
private readonly DiscordSocketClient _client;
public SpeedTypingCommands(DiscordSocketClient client, GamesService games)
{
2017-05-27 08:19:27 +00:00
_games = games;
_client = client;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task TypeStart()
2016-09-01 01:12:08 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
var game = _service.RunningContests.GetOrAdd(channel.Guild.Id, id => new TypingGame(_games, _client, channel, Prefix));
if (game.IsActive)
{
await channel.SendErrorAsync(
$"Contest already running in " +
$"{game.Channel.Mention} channel.")
.ConfigureAwait(false);
}
else
{
await game.Start().ConfigureAwait(false);
}
2016-09-01 01:12:08 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task TypeStop()
2016-09-01 01:12:08 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (_service.RunningContests.TryRemove(channel.Guild.Id, out TypingGame game))
{
await game.Stop().ConfigureAwait(false);
return;
}
await channel.SendErrorAsync("No contest to stop on this channel.").ConfigureAwait(false);
2016-09-01 01:12:08 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2016-12-16 19:45:46 +00:00
public async Task Typeadd([Remainder] string text)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (string.IsNullOrWhiteSpace(text))
return;
_games.AddTypingArticle(Context.User, text);
await channel.SendConfirmAsync("Added new article for typing game.").ConfigureAwait(false);
}
2016-10-28 11:14:16 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 19:45:46 +00:00
public async Task Typelist(int page = 1)
2016-10-28 11:14:16 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-10-28 11:14:16 +00:00
if (page < 1)
return;
2017-05-27 08:19:27 +00:00
var articles = _games.TypingArticles.Skip((page - 1) * 15).Take(15).ToArray();
2016-10-28 11:14:16 +00:00
if (!articles.Any())
{
2016-12-16 18:43:57 +00:00
await channel.SendErrorAsync($"{Context.User.Mention} `No articles found on that page.`").ConfigureAwait(false);
2016-10-28 11:14:16 +00:00
return;
}
var i = (page - 1) * 15;
await channel.SendConfirmAsync("List of articles for Type Race", string.Join("\n", articles.Select(a => $"`#{++i}` - {a.Text.TrimTo(50)}")))
2016-10-28 11:14:16 +00:00
.ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2016-12-16 19:45:46 +00:00
public async Task Typedel(int index)
2016-10-28 11:14:16 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-10-28 11:14:16 +00:00
index -= 1;
2017-05-27 08:19:27 +00:00
if (index < 0 || index >= _games.TypingArticles.Count)
2016-10-28 11:14:16 +00:00
return;
2017-05-27 08:19:27 +00:00
var removed = _games.TypingArticles[index];
_games.TypingArticles.RemoveAt(index);
2016-10-28 11:14:16 +00:00
2017-05-27 08:19:27 +00:00
File.WriteAllText(_games.TypingArticlesPath, JsonConvert.SerializeObject(_games.TypingArticles));
2016-10-28 11:14:16 +00:00
await channel.SendConfirmAsync($"`Removed typing article:` #{index + 1} - {removed.Text.TrimTo(50)}")
2016-10-28 11:14:16 +00:00
.ConfigureAwait(false);
}
}
2016-09-01 01:12:08 +00:00
}
}