From fab46bc3f237437849ca0d28d24bcf687f0032b4 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 30 Jan 2017 06:26:47 +0100 Subject: [PATCH] sneakygamestatus event added --- .../Gambling/Commands/CurrencyEvents.cs | 78 ++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/src/NadekoBot/Modules/Gambling/Commands/CurrencyEvents.cs b/src/NadekoBot/Modules/Gambling/Commands/CurrencyEvents.cs index 598a19b0..ad6e7023 100644 --- a/src/NadekoBot/Modules/Gambling/Commands/CurrencyEvents.cs +++ b/src/NadekoBot/Modules/Gambling/Commands/CurrencyEvents.cs @@ -9,6 +9,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Discord.WebSocket; +using NadekoBot.Services.Database; namespace NadekoBot.Modules.Gambling { @@ -19,15 +21,27 @@ namespace NadekoBot.Modules.Gambling { public enum CurrencyEvent { - FlowerReaction + FlowerReaction, + SneakyGameStatus } //flower reaction event public static readonly ConcurrentHashSet _flowerReactionAwardedUsers = new ConcurrentHashSet(); + + private static readonly char[] _sneakyGameStatusChars = Enumerable.Range(48, 10) + .Concat(Enumerable.Range(65, 26)) + .Concat(Enumerable.Range(97, 26)) + .Select(x => (char)x) + .ToArray(); + + public static readonly ConcurrentHashSet _sneakyGameAwardedUsers = new ConcurrentHashSet(); + + private static string _secretCode = String.Empty; + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] [OwnerOnly] - public async Task StartEvent(CurrencyEvent e) + public async Task StartEvent(CurrencyEvent e, int arg = -1) { var channel = (ITextChannel)Context.Channel; try @@ -38,6 +52,9 @@ namespace NadekoBot.Modules.Gambling case CurrencyEvent.FlowerReaction: await FlowerReactionEvent(Context).ConfigureAwait(false); break; + case CurrencyEvent.SneakyGameStatus: + await SneakyGameStatusEvent(Context, arg).ConfigureAwait(false); + break; default: break; } @@ -45,6 +62,63 @@ namespace NadekoBot.Modules.Gambling catch { } } + public static async Task SneakyGameStatusEvent(CommandContext Context, int? arg) + { + int num; + if (arg == null || arg < 5) + num = 60; + else + num = arg.Value; + + if (_secretCode != String.Empty) + return; + var rng = new NadekoRandom(); + + for (int i = 0; i < 5; i++) + { + _secretCode += _sneakyGameStatusChars[rng.Next(0, _sneakyGameStatusChars.Length)]; + } + + await NadekoBot.Client.SetGameAsync($"type {_secretCode} for " + NadekoBot.BotConfig.CurrencyPluralName) + .ConfigureAwait(false); + try + { + await Context.Channel.SendConfirmAsync($"SneakyGameStatus event started", + $"Users must type a secret code to get 100 currency.\n" + + $"Lasts {num} seconds. Don't tell anyone. Shhh.") + .ConfigureAwait(false); + } + catch { } + + + NadekoBot.Client.MessageReceived += SneakyGameMessageReceivedEventHandler; + await Task.Delay(num * 1000); + NadekoBot.Client.MessageReceived -= SneakyGameMessageReceivedEventHandler; + + _sneakyGameAwardedUsers.Clear(); + _secretCode = String.Empty; + + await NadekoBot.Client.SetGameAsync($"SneakyGame event ended.") + .ConfigureAwait(false); + } + + private static Task SneakyGameMessageReceivedEventHandler(SocketMessage arg) + { + if (arg.Content == _secretCode && + _sneakyGameAwardedUsers.Add(arg.Author.Id)) + { + var _ = Task.Run(async () => + { + await CurrencyHandler.AddCurrencyAsync(arg.Author, "Sneaky Game Event", 100, false) + .ConfigureAwait(false); + + try { await arg.DeleteAsync(new RequestOptions() { RetryMode = RetryMode.AlwaysFail }).ConfigureAwait(false); } + catch { } + }); + } + + return Task.Delay(0); + } public static async Task FlowerReactionEvent(CommandContext Context) {