public nadeko stuff

This commit is contained in:
Master Kwoth 2017-10-31 09:52:46 +01:00
parent 0eab51cfea
commit e604bbca50
7 changed files with 78 additions and 17 deletions

View File

@ -1,4 +1,6 @@
using Discord; 
#if !GLOBAL_NADEKO
using Discord;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Core.Services; using NadekoBot.Core.Services;
@ -14,7 +16,6 @@ using static NadekoBot.Modules.Administration.Services.LogCommandService;
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
{ {
#if !GLOBAL_NADEKO
public partial class Administration public partial class Administration
{ {
[Group] [Group]
@ -181,5 +182,5 @@ namespace NadekoBot.Modules.Administration
} }
} }
} }
#endif }
} #endif

View File

@ -482,9 +482,11 @@ namespace NadekoBot.Modules.Administration
public async Task ReloadImages() public async Task ReloadImages()
{ {
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
_images.Reload(); var sub = _cache.Redis.GetSubscriber();
sw.Stop(); sub.Publish(_creds.RedisKey() + "_reload_images",
await ReplyConfirmLocalized("images_loaded", sw.Elapsed.TotalSeconds.ToString("F3")).ConfigureAwait(false); "",
StackExchange.Redis.CommandFlags.FireAndForget);
await ReplyConfirmLocalized("images_loaded", 0).ConfigureAwait(false);
} }
private static UserStatus SettableUserStatusToUserStatus(SettableUserStatus sus) private static UserStatus SettableUserStatusToUserStatus(SettableUserStatus sus)

View File

@ -1,4 +1,6 @@
using System; 
#if !GLOBAL_NADEKO
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -15,7 +17,6 @@ using NLog;
namespace NadekoBot.Modules.Administration.Services namespace NadekoBot.Modules.Administration.Services
{ {
#if !GLOBAL_NADEKO
public class LogCommandService : INService public class LogCommandService : INService
{ {
@ -1023,5 +1024,6 @@ namespace NadekoBot.Modules.Administration.Services
} }
} }
} }
#endif
} }
#endif

View File

@ -10,6 +10,7 @@ using NadekoBot.Extensions;
using NadekoBot.Core.Services; using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Impl; using NadekoBot.Core.Services.Impl;
using NLog; using NLog;
using StackExchange.Redis;
namespace NadekoBot.Modules.Administration.Services namespace NadekoBot.Modules.Administration.Services
{ {
@ -17,7 +18,8 @@ namespace NadekoBot.Modules.Administration.Services
{ {
public bool ForwardDMs => _bc.BotConfig.ForwardMessages; public bool ForwardDMs => _bc.BotConfig.ForwardMessages;
public bool ForwardDMsToAllOwners => _bc.BotConfig.ForwardToAllOwners; public bool ForwardDMsToAllOwners => _bc.BotConfig.ForwardToAllOwners;
private readonly ConnectionMultiplexer _redis;
private readonly NadekoBot _bot; private readonly NadekoBot _bot;
private readonly CommandHandler _cmdHandler; private readonly CommandHandler _cmdHandler;
private readonly DbService _db; private readonly DbService _db;
@ -28,10 +30,13 @@ namespace NadekoBot.Modules.Administration.Services
private readonly IBotCredentials _creds; private readonly IBotCredentials _creds;
private ImmutableArray<AsyncLazy<IDMChannel>> ownerChannels = new ImmutableArray<AsyncLazy<IDMChannel>>(); private ImmutableArray<AsyncLazy<IDMChannel>> ownerChannels = new ImmutableArray<AsyncLazy<IDMChannel>>();
private readonly IBotConfigProvider _bc; private readonly IBotConfigProvider _bc;
private readonly IImagesService _imgs;
public SelfService(DiscordSocketClient client, NadekoBot bot, CommandHandler cmdHandler, DbService db, 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; _bot = bot;
_cmdHandler = cmdHandler; _cmdHandler = cmdHandler;
_db = db; _db = db;
@ -41,6 +46,11 @@ namespace NadekoBot.Modules.Administration.Services
_client = client; _client = client;
_creds = creds; _creds = creds;
_bc = bc; _bc = bc;
_imgs = imgs;
var sub = _redis.GetSubscriber();
sub.Subscribe(_creds.RedisKey() + "_reload_images",
delegate { _imgs.Reload(); }, CommandFlags.FireAndForget);
Task.Run(async () => Task.Run(async () =>
{ {

View File

@ -24,7 +24,35 @@ namespace NadekoBot.Modules.Games
{ {
_images = images; _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] [NadekoCommand, Usage, Description, Aliases]
public async Task Choose([Remainder] string list = null) public async Task Choose([Remainder] string list = null)
{ {

View File

@ -13,7 +13,6 @@ using NadekoBot.Common.Collections;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Modules.Games.Common; using NadekoBot.Modules.Games.Common;
using NadekoBot.Core.Services; using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services.Impl; using NadekoBot.Core.Services.Impl;
using Newtonsoft.Json; using Newtonsoft.Json;
using NLog; using NLog;
@ -30,6 +29,7 @@ namespace NadekoBot.Modules.Games.Services
private readonly IBotConfigProvider _bc; private readonly IBotConfigProvider _bc;
public readonly ConcurrentDictionary<ulong, GirlRating> GirlRatings = new ConcurrentDictionary<ulong, GirlRating>(); public readonly ConcurrentDictionary<ulong, GirlRating> GirlRatings = new ConcurrentDictionary<ulong, GirlRating>();
public readonly ImmutableArray<string> EightBallResponses; public readonly ImmutableArray<string> EightBallResponses;
private readonly Timer _t; private readonly Timer _t;
@ -37,7 +37,8 @@ namespace NadekoBot.Modules.Games.Services
private readonly NadekoStrings _strings; private readonly NadekoStrings _strings;
private readonly IImagesService _images; private readonly IImagesService _images;
private readonly Logger _log; private readonly Logger _log;
private readonly NadekoRandom _rng;
private readonly CurrencyService _cs;
public readonly string TypingArticlesPath = "data/typing_articles2.json"; public readonly string TypingArticlesPath = "data/typing_articles2.json";
private readonly CommandHandler _cmdHandler; private readonly CommandHandler _cmdHandler;
@ -56,7 +57,8 @@ namespace NadekoBot.Modules.Games.Services
public ConcurrentDictionary<ulong, Nunchi> NunchiGames { get; } = new ConcurrentDictionary<ulong, Common.Nunchi.Nunchi>(); public ConcurrentDictionary<ulong, Nunchi> NunchiGames { get; } = new ConcurrentDictionary<ulong, Common.Nunchi.Nunchi>();
public GamesService(CommandHandler cmd, IBotConfigProvider bc, NadekoBot bot, public GamesService(CommandHandler cmd, IBotConfigProvider bc, NadekoBot bot,
NadekoStrings strings, IImagesService images, CommandHandler cmdHandler) NadekoStrings strings, IImagesService images, CommandHandler cmdHandler,
CurrencyService cs)
{ {
_bc = bc; _bc = bc;
_cmd = cmd; _cmd = cmd;
@ -64,6 +66,8 @@ namespace NadekoBot.Modules.Games.Services
_images = images; _images = images;
_cmdHandler = cmdHandler; _cmdHandler = cmdHandler;
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
_rng = new NadekoRandom();
_cs = cs;
//8ball //8ball
EightBallResponses = _bc.BotConfig.EightBallResponses.Select(ebr => ebr.Text).ToImmutableArray(); EightBallResponses = _bc.BotConfig.EightBallResponses.Select(ebr => ebr.Text).ToImmutableArray();
@ -138,7 +142,8 @@ namespace NadekoBot.Modules.Games.Services
public ConcurrentDictionary<ulong, DateTime> LastGenerations { get; } = new ConcurrentDictionary<ulong, DateTime>(); public ConcurrentDictionary<ulong, DateTime> LastGenerations { get; } = new ConcurrentDictionary<ulong, DateTime>();
private ConcurrentDictionary<ulong, object> _locks { get; } = new ConcurrentDictionary<ulong, object>(); private ConcurrentDictionary<ulong, object> _locks { get; } = new ConcurrentDictionary<ulong, object>();
public ConcurrentHashSet<ulong> HalloweenAwardedUsers { get; } = new ConcurrentHashSet<ulong>();
public (string Name, ImmutableArray<byte> Data) GetRandomCurrencyImage() public (string Name, ImmutableArray<byte> Data) GetRandomCurrencyImage()
{ {
var rng = new NadekoRandom(); var rng = new NadekoRandom();
@ -211,5 +216,17 @@ namespace NadekoBot.Modules.Games.Services
}); });
return Task.CompletedTask; return Task.CompletedTask;
} }
public async Task<bool> GetTreat(ulong userId)
{
if (_rng.Next(0, 10) != 0)
{
await _cs.AddAsync(userId, "Halloween 2017 Treat", 10)
.ConfigureAwait(false);
return true;
}
return false;
}
} }
} }

View File

@ -6,6 +6,7 @@ using System.Linq;
namespace NadekoBot.Core.Services.Impl namespace NadekoBot.Core.Services.Impl
{ {
//todo move everything to redis
public class ImagesService : IImagesService public class ImagesService : IImagesService
{ {
private readonly Logger _log; private readonly Logger _log;