NadekoBot/src/NadekoBot/Modules/Gambling/CurrencyEventsCommands.cs

246 lines
8.7 KiB
C#
Raw Normal View History

2017-01-15 14:09:48 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Services;
using System;
using System.Linq;
using System.Threading.Tasks;
2017-01-30 05:26:47 +00:00
using Discord.WebSocket;
using System.Threading;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using NadekoBot.Common.Collections;
using NLog;
2017-01-15 14:09:48 +00:00
namespace NadekoBot.Modules.Gambling
{
public partial class Gambling
{
[Group]
2017-07-17 19:42:36 +00:00
public class CurrencyEventsCommands : NadekoSubmodule
2017-01-15 14:09:48 +00:00
{
public enum CurrencyEvent
{
2017-01-30 05:26:47 +00:00
FlowerReaction,
SneakyGameStatus
2017-01-15 14:09:48 +00:00
}
//flower reaction event
2017-02-15 10:41:32 +00:00
private static readonly ConcurrentHashSet<ulong> _sneakyGameAwardedUsers = new ConcurrentHashSet<ulong>();
2017-05-24 20:28:16 +00:00
2017-01-30 05:26:47 +00:00
private static readonly char[] _sneakyGameStatusChars = Enumerable.Range(48, 10)
.Concat(Enumerable.Range(65, 26))
.Concat(Enumerable.Range(97, 26))
.Select(x => (char)x)
.ToArray();
2017-05-24 20:28:16 +00:00
private string _secretCode = string.Empty;
private readonly DiscordSocketClient _client;
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
2017-05-24 20:28:16 +00:00
public CurrencyEventsCommands(DiscordSocketClient client, IBotConfigProvider bc, CurrencyService cs)
2017-05-24 20:28:16 +00:00
{
_client = client;
_bc = bc;
_cs = cs;
2017-05-24 20:28:16 +00:00
}
2017-01-30 05:26:47 +00:00
2017-01-15 14:09:48 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2017-01-30 05:26:47 +00:00
public async Task StartEvent(CurrencyEvent e, int arg = -1)
2017-01-15 14:09:48 +00:00
{
2017-02-14 13:30:21 +00:00
switch (e)
2017-01-15 14:09:48 +00:00
{
2017-02-14 13:30:21 +00:00
case CurrencyEvent.FlowerReaction:
await FlowerReactionEvent(Context, arg).ConfigureAwait(false);
2017-02-14 13:30:21 +00:00
break;
case CurrencyEvent.SneakyGameStatus:
await SneakyGameStatusEvent(Context, arg).ConfigureAwait(false);
break;
2017-01-15 14:09:48 +00:00
}
}
2017-04-15 00:54:19 +00:00
public async Task SneakyGameStatusEvent(ICommandContext context, int? arg)
2017-01-30 05:26:47 +00:00
{
int num;
if (arg == null || arg < 5)
num = 60;
else
num = arg.Value;
2017-02-15 10:41:32 +00:00
if (_secretCode != string.Empty)
2017-01-30 05:26:47 +00:00
return;
var rng = new NadekoRandom();
2017-02-15 10:41:32 +00:00
for (var i = 0; i < 5; i++)
2017-01-30 05:26:47 +00:00
{
_secretCode += _sneakyGameStatusChars[rng.Next(0, _sneakyGameStatusChars.Length)];
}
2017-02-14 13:30:21 +00:00
await _client.SetGameAsync($"type {_secretCode} for " + _bc.BotConfig.CurrencyPluralName)
2017-01-30 05:26:47 +00:00
.ConfigureAwait(false);
try
{
2017-02-15 10:41:32 +00:00
var title = GetText("sneakygamestatus_title");
var desc = GetText("sneakygamestatus_desc", Format.Bold(100.ToString()) + _bc.BotConfig.CurrencySign, Format.Bold(num.ToString()));
2017-02-15 10:41:32 +00:00
await context.Channel.SendConfirmAsync(title, desc).ConfigureAwait(false);
2017-01-30 05:26:47 +00:00
}
2017-02-14 13:30:21 +00:00
catch
{
// ignored
}
2017-01-30 05:26:47 +00:00
2017-05-24 20:28:16 +00:00
_client.MessageReceived += SneakyGameMessageReceivedEventHandler;
2017-01-30 05:26:47 +00:00
await Task.Delay(num * 1000);
2017-05-24 20:28:16 +00:00
_client.MessageReceived -= SneakyGameMessageReceivedEventHandler;
2017-01-30 05:26:47 +00:00
var cnt = _sneakyGameAwardedUsers.Count;
2017-01-30 05:26:47 +00:00
_sneakyGameAwardedUsers.Clear();
2017-02-15 10:41:32 +00:00
_secretCode = string.Empty;
2017-01-30 05:26:47 +00:00
2017-05-24 20:28:16 +00:00
await _client.SetGameAsync(GetText("sneakygamestatus_end", cnt))
2017-01-30 05:26:47 +00:00
.ConfigureAwait(false);
}
2017-05-24 20:28:16 +00:00
private Task SneakyGameMessageReceivedEventHandler(SocketMessage arg)
2017-01-30 05:26:47 +00:00
{
if (arg.Content == _secretCode &&
_sneakyGameAwardedUsers.Add(arg.Author.Id))
{
var _ = Task.Run(async () =>
{
await _cs.AddAsync(arg.Author, "Sneaky Game Event", 100, false)
2017-01-30 05:26:47 +00:00
.ConfigureAwait(false);
try { await arg.DeleteAsync(new RequestOptions() { RetryMode = RetryMode.AlwaysFail }).ConfigureAwait(false); }
2017-02-14 13:30:21 +00:00
catch
{
// ignored
}
2017-01-30 05:26:47 +00:00
});
}
2017-06-15 23:55:14 +00:00
return Task.CompletedTask;
2017-01-30 05:26:47 +00:00
}
2017-01-15 14:09:48 +00:00
2017-04-15 00:54:19 +00:00
public async Task FlowerReactionEvent(ICommandContext context, int amount)
2017-02-15 10:41:32 +00:00
{
if (amount <= 0)
amount = 100;
2017-02-15 10:41:32 +00:00
var title = GetText("flowerreaction_title");
var desc = GetText("flowerreaction_desc", "🌸", Format.Bold(amount.ToString()) + _bc.BotConfig.CurrencySign);
2017-02-15 10:41:32 +00:00
var footer = GetText("flowerreaction_footer", 24);
var msg = await context.Channel.SendConfirmAsync(title,
desc, footer: footer)
.ConfigureAwait(false);
await new FlowerReactionEvent(_client, _cs).Start(msg, context, amount);
2017-02-15 10:41:32 +00:00
}
}
}
public abstract class CurrencyEvent
{
2017-04-15 00:54:19 +00:00
public abstract Task Start(IUserMessage msg, ICommandContext channel, int amount);
}
public class FlowerReactionEvent : CurrencyEvent
{
2017-02-15 10:41:32 +00:00
private readonly ConcurrentHashSet<ulong> _flowerReactionAwardedUsers = new ConcurrentHashSet<ulong>();
private readonly Logger _log;
private readonly DiscordSocketClient _client;
private readonly CurrencyService _cs;
private readonly SocketSelfUser _botUser;
2017-04-15 00:54:19 +00:00
private IUserMessage StartingMessage { get; set; }
2017-04-15 00:54:19 +00:00
private CancellationTokenSource Source { get; }
private CancellationToken CancelToken { get; }
public FlowerReactionEvent(DiscordSocketClient client, CurrencyService cs)
{
_log = LogManager.GetCurrentClassLogger();
2017-05-24 20:28:16 +00:00
_client = client;
_cs = cs;
_botUser = client.CurrentUser;
2017-04-15 00:54:19 +00:00
Source = new CancellationTokenSource();
CancelToken = Source.Token;
}
private async Task End()
{
2017-04-15 00:54:19 +00:00
if(StartingMessage != null)
await StartingMessage.DeleteAsync().ConfigureAwait(false);
2017-04-15 00:54:19 +00:00
if(!Source.IsCancellationRequested)
Source.Cancel();
2017-05-24 20:28:16 +00:00
_client.MessageDeleted -= MessageDeletedEventHandler;
}
2017-04-15 00:54:19 +00:00
private Task MessageDeletedEventHandler(Cacheable<IMessage, ulong> msg, ISocketMessageChannel channel) {
if (StartingMessage?.Id == msg.Id)
2017-01-15 14:09:48 +00:00
{
_log.Warn("Stopping flower reaction event because message is deleted.");
2017-02-14 13:30:21 +00:00
var __ = Task.Run(End);
}
return Task.CompletedTask;
}
2017-04-15 00:54:19 +00:00
public override async Task Start(IUserMessage umsg, ICommandContext context, int amount)
{
2017-04-15 00:54:19 +00:00
StartingMessage = umsg;
2017-05-24 20:28:16 +00:00
_client.MessageDeleted += MessageDeletedEventHandler;
try { await StartingMessage.AddReactionAsync(new Emoji("🌸")).ConfigureAwait(false); }
catch
{
try { await StartingMessage.AddReactionAsync(new Emoji("🌸")).ConfigureAwait(false); }
2017-01-15 17:04:22 +00:00
catch
{
2017-04-15 00:54:19 +00:00
try { await StartingMessage.DeleteAsync().ConfigureAwait(false); }
catch { return; }
}
}
2017-05-24 20:28:16 +00:00
using (StartingMessage.OnReaction(_client, async (r) =>
{
try
{
if (r.UserId == _botUser.Id)
return;
2017-05-24 20:28:16 +00:00
if (r.Emote.Name == "🌸" && r.User.IsSpecified && ((DateTime.UtcNow - r.User.Value.CreatedAt).TotalDays > 5) && _flowerReactionAwardedUsers.Add(r.User.Value.Id))
2017-01-15 17:04:22 +00:00
{
await _cs.AddAsync(r.User.Value, "Flower Reaction Event", amount, false)
2017-02-14 13:30:21 +00:00
.ConfigureAwait(false);
2017-01-15 17:04:22 +00:00
}
}
2017-02-14 13:30:21 +00:00
catch
{
// ignored
}
}))
{
try
2017-01-15 14:09:48 +00:00
{
2017-04-15 00:54:19 +00:00
await Task.Delay(TimeSpan.FromHours(24), CancelToken).ConfigureAwait(false);
2017-01-15 14:09:48 +00:00
}
catch (OperationCanceledException)
{
}
2017-04-15 00:54:19 +00:00
if (CancelToken.IsCancellationRequested)
return;
_log.Warn("Stopping flower reaction event because it expired.");
await End();
2017-01-15 14:09:48 +00:00
}
}
}
}