From e604bbca50f36245c18a6e1887ef9da8efc65c1c Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Tue, 31 Oct 2017 09:52:46 +0100 Subject: [PATCH] public nadeko stuff --- .../Modules/Administration/LogCommands.cs | 9 +++--- .../Modules/Administration/SelfCommands.cs | 8 +++-- .../Services/LogCommandService.cs | 8 +++-- .../Administration/Services/SelfService.cs | 14 +++++++-- NadekoBot.Core/Modules/Games/Games.cs | 30 ++++++++++++++++++- .../Modules/Games/Services/GamesService.cs | 25 +++++++++++++--- NadekoBot.Core/Services/Impl/ImagesService.cs | 1 + 7 files changed, 78 insertions(+), 17 deletions(-) diff --git a/NadekoBot.Core/Modules/Administration/LogCommands.cs b/NadekoBot.Core/Modules/Administration/LogCommands.cs index 83d5b27d..26e311d8 100644 --- a/NadekoBot.Core/Modules/Administration/LogCommands.cs +++ b/NadekoBot.Core/Modules/Administration/LogCommands.cs @@ -1,4 +1,6 @@ -using Discord; + +#if !GLOBAL_NADEKO +using Discord; using Discord.Commands; using NadekoBot.Extensions; using NadekoBot.Core.Services; @@ -14,7 +16,6 @@ using static NadekoBot.Modules.Administration.Services.LogCommandService; namespace NadekoBot.Modules.Administration { -#if !GLOBAL_NADEKO public partial class Administration { [Group] @@ -181,5 +182,5 @@ namespace NadekoBot.Modules.Administration } } } -#endif -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/NadekoBot.Core/Modules/Administration/SelfCommands.cs b/NadekoBot.Core/Modules/Administration/SelfCommands.cs index 71e6fa5f..1a83a454 100644 --- a/NadekoBot.Core/Modules/Administration/SelfCommands.cs +++ b/NadekoBot.Core/Modules/Administration/SelfCommands.cs @@ -482,9 +482,11 @@ namespace NadekoBot.Modules.Administration public async Task ReloadImages() { var sw = Stopwatch.StartNew(); - _images.Reload(); - sw.Stop(); - await ReplyConfirmLocalized("images_loaded", sw.Elapsed.TotalSeconds.ToString("F3")).ConfigureAwait(false); + var sub = _cache.Redis.GetSubscriber(); + sub.Publish(_creds.RedisKey() + "_reload_images", + "", + StackExchange.Redis.CommandFlags.FireAndForget); + await ReplyConfirmLocalized("images_loaded", 0).ConfigureAwait(false); } private static UserStatus SettableUserStatusToUserStatus(SettableUserStatus sus) diff --git a/NadekoBot.Core/Modules/Administration/Services/LogCommandService.cs b/NadekoBot.Core/Modules/Administration/Services/LogCommandService.cs index 1d1c1f4d..ffa5fd93 100644 --- a/NadekoBot.Core/Modules/Administration/Services/LogCommandService.cs +++ b/NadekoBot.Core/Modules/Administration/Services/LogCommandService.cs @@ -1,4 +1,6 @@ -using System; + +#if !GLOBAL_NADEKO +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -15,7 +17,6 @@ using NLog; namespace NadekoBot.Modules.Administration.Services { -#if !GLOBAL_NADEKO public class LogCommandService : INService { @@ -1023,5 +1024,6 @@ namespace NadekoBot.Modules.Administration.Services } } } -#endif + } +#endif diff --git a/NadekoBot.Core/Modules/Administration/Services/SelfService.cs b/NadekoBot.Core/Modules/Administration/Services/SelfService.cs index 11aeb7fa..b6efd5ee 100644 --- a/NadekoBot.Core/Modules/Administration/Services/SelfService.cs +++ b/NadekoBot.Core/Modules/Administration/Services/SelfService.cs @@ -10,6 +10,7 @@ using NadekoBot.Extensions; using NadekoBot.Core.Services; using NadekoBot.Core.Services.Impl; using NLog; +using StackExchange.Redis; namespace NadekoBot.Modules.Administration.Services { @@ -17,7 +18,8 @@ namespace NadekoBot.Modules.Administration.Services { public bool ForwardDMs => _bc.BotConfig.ForwardMessages; public bool ForwardDMsToAllOwners => _bc.BotConfig.ForwardToAllOwners; - + + private readonly ConnectionMultiplexer _redis; private readonly NadekoBot _bot; private readonly CommandHandler _cmdHandler; private readonly DbService _db; @@ -28,10 +30,13 @@ namespace NadekoBot.Modules.Administration.Services private readonly IBotCredentials _creds; private ImmutableArray> ownerChannels = new ImmutableArray>(); private readonly IBotConfigProvider _bc; + private readonly IImagesService _imgs; public SelfService(DiscordSocketClient client, NadekoBot bot, CommandHandler cmdHandler, DbService db, - IBotConfigProvider bc, ILocalization localization, NadekoStrings strings, IBotCredentials creds) + IBotConfigProvider bc, ILocalization localization, NadekoStrings strings, IBotCredentials creds, + IDataCache cache, IImagesService imgs) { + _redis = cache.Redis; _bot = bot; _cmdHandler = cmdHandler; _db = db; @@ -41,6 +46,11 @@ namespace NadekoBot.Modules.Administration.Services _client = client; _creds = creds; _bc = bc; + _imgs = imgs; + + var sub = _redis.GetSubscriber(); + sub.Subscribe(_creds.RedisKey() + "_reload_images", + delegate { _imgs.Reload(); }, CommandFlags.FireAndForget); Task.Run(async () => { diff --git a/NadekoBot.Core/Modules/Games/Games.cs b/NadekoBot.Core/Modules/Games/Games.cs index 9b894b72..6426abcf 100644 --- a/NadekoBot.Core/Modules/Games/Games.cs +++ b/NadekoBot.Core/Modules/Games/Games.cs @@ -24,7 +24,35 @@ namespace NadekoBot.Modules.Games { _images = images; } - +#if GLOBAL_NADEKO + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public async Task TrickOrTreat() + { + if (DateTime.UtcNow.Day != 31 || + DateTime.UtcNow.Month != 10 + || !_service.HalloweenAwardedUsers.Add(Context.User.Id) + ) + { + return; + } + if (await _service.GetTreat(Context.User.Id)) + { + await Context.Channel + .SendConfirmAsync($"You've got a treat of 10🍬! Happy Halloween!") + .ConfigureAwait(false); + } + else + { + await Context.Channel + .EmbedAsync(new EmbedBuilder() + .WithDescription("No treat for you :c Happy Halloween!") + .WithImageUrl("http://tinyurl.com/ybntddbb") + .WithErrorColor()) + .ConfigureAwait(false); + } + } +#endif [NadekoCommand, Usage, Description, Aliases] public async Task Choose([Remainder] string list = null) { diff --git a/NadekoBot.Core/Modules/Games/Services/GamesService.cs b/NadekoBot.Core/Modules/Games/Services/GamesService.cs index 25769715..ccb40b1f 100644 --- a/NadekoBot.Core/Modules/Games/Services/GamesService.cs +++ b/NadekoBot.Core/Modules/Games/Services/GamesService.cs @@ -13,7 +13,6 @@ using NadekoBot.Common.Collections; using NadekoBot.Extensions; using NadekoBot.Modules.Games.Common; using NadekoBot.Core.Services; -using NadekoBot.Core.Services.Database.Models; using NadekoBot.Core.Services.Impl; using Newtonsoft.Json; using NLog; @@ -30,6 +29,7 @@ namespace NadekoBot.Modules.Games.Services private readonly IBotConfigProvider _bc; public readonly ConcurrentDictionary GirlRatings = new ConcurrentDictionary(); + public readonly ImmutableArray EightBallResponses; private readonly Timer _t; @@ -37,7 +37,8 @@ namespace NadekoBot.Modules.Games.Services private readonly NadekoStrings _strings; private readonly IImagesService _images; private readonly Logger _log; - + private readonly NadekoRandom _rng; + private readonly CurrencyService _cs; public readonly string TypingArticlesPath = "data/typing_articles2.json"; private readonly CommandHandler _cmdHandler; @@ -56,7 +57,8 @@ namespace NadekoBot.Modules.Games.Services public ConcurrentDictionary NunchiGames { get; } = new ConcurrentDictionary(); public GamesService(CommandHandler cmd, IBotConfigProvider bc, NadekoBot bot, - NadekoStrings strings, IImagesService images, CommandHandler cmdHandler) + NadekoStrings strings, IImagesService images, CommandHandler cmdHandler, + CurrencyService cs) { _bc = bc; _cmd = cmd; @@ -64,6 +66,8 @@ namespace NadekoBot.Modules.Games.Services _images = images; _cmdHandler = cmdHandler; _log = LogManager.GetCurrentClassLogger(); + _rng = new NadekoRandom(); + _cs = cs; //8ball EightBallResponses = _bc.BotConfig.EightBallResponses.Select(ebr => ebr.Text).ToImmutableArray(); @@ -138,7 +142,8 @@ namespace NadekoBot.Modules.Games.Services public ConcurrentDictionary LastGenerations { get; } = new ConcurrentDictionary(); private ConcurrentDictionary _locks { get; } = new ConcurrentDictionary(); - + public ConcurrentHashSet HalloweenAwardedUsers { get; } = new ConcurrentHashSet(); + public (string Name, ImmutableArray Data) GetRandomCurrencyImage() { var rng = new NadekoRandom(); @@ -211,5 +216,17 @@ namespace NadekoBot.Modules.Games.Services }); return Task.CompletedTask; } + + public async Task GetTreat(ulong userId) + { + if (_rng.Next(0, 10) != 0) + { + await _cs.AddAsync(userId, "Halloween 2017 Treat", 10) + .ConfigureAwait(false); + return true; + } + + return false; + } } } diff --git a/NadekoBot.Core/Services/Impl/ImagesService.cs b/NadekoBot.Core/Services/Impl/ImagesService.cs index 3b8b2a43..50d432a8 100644 --- a/NadekoBot.Core/Services/Impl/ImagesService.cs +++ b/NadekoBot.Core/Services/Impl/ImagesService.cs @@ -6,6 +6,7 @@ using System.Linq; namespace NadekoBot.Core.Services.Impl { + //todo move everything to redis public class ImagesService : IImagesService { private readonly Logger _log;