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

123 lines
4.4 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.Attributes;
using NadekoBot.Extensions;
2017-05-27 08:19:27 +00:00
using NadekoBot.Modules.Games.Models;
using NadekoBot.Services.Games;
using Newtonsoft.Json;
2016-09-01 01:12:08 +00:00
using System.Collections.Concurrent;
using System.IO;
2016-09-01 01:12:08 +00:00
using System.Linq;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Games
{
public partial class Games
2016-09-01 01:12:08 +00:00
{
[Group]
2017-02-19 14:18:40 +00:00
public class SpeedTypingCommands : NadekoSubmodule
2016-09-01 01:12:08 +00:00
{
2017-05-27 08:19:27 +00:00
public static ConcurrentDictionary<ulong, TypingGame> RunningContests = new ConcurrentDictionary<ulong, TypingGame>();
private readonly GamesService _games;
private readonly DiscordShardedClient _client;
2017-05-27 08:19:27 +00:00
public SpeedTypingCommands(DiscordShardedClient 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 = 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;
TypingGame game;
if (RunningContests.TryRemove(channel.Guild.Id, out 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
}
}