mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 17:58:26 -04:00
- Started cleanup of command handler
- Removed IUnloadableService - Started removing INService (removed it from services which implement behavior interfaces) - wip - Added scrutor for better service registration - wip
This commit is contained in:
100
src/NadekoBot/Services/Impl/BehaviorExecutor.cs
Normal file
100
src/NadekoBot/Services/Impl/BehaviorExecutor.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Extensions;
|
||||
using Serilog;
|
||||
|
||||
namespace NadekoBot.Services
|
||||
{
|
||||
public sealed class BehaviorExecutor : IBehaviourExecutor
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IEnumerable<ILateExecutor> _lateExecutors;
|
||||
private readonly IEnumerable<ILateBlocker> _lateBlockers;
|
||||
private readonly IEnumerable<IEarlyBehavior> _earlyBehaviors;
|
||||
private readonly IEnumerable<IInputTransformer> _transformers;
|
||||
|
||||
public BehaviorExecutor(
|
||||
DiscordSocketClient client,
|
||||
IEnumerable<ILateExecutor> lateExecutors,
|
||||
IEnumerable<ILateBlocker> lateBlockers,
|
||||
IEnumerable<IEarlyBehavior> earlyBehaviors,
|
||||
IEnumerable<IInputTransformer> transformers)
|
||||
{
|
||||
_client = client;
|
||||
_lateExecutors = lateExecutors;
|
||||
_lateBlockers = lateBlockers;
|
||||
_earlyBehaviors = earlyBehaviors;
|
||||
_transformers = transformers;
|
||||
}
|
||||
|
||||
// todo early behaviors should print for themselves
|
||||
public async Task<bool> RunEarlyBehavioursAsync(SocketGuild guild, IUserMessage usrMsg)
|
||||
{
|
||||
foreach (var beh in _earlyBehaviors)
|
||||
{
|
||||
if (await beh.RunBehavior(guild, usrMsg))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<string> RunInputTransformersAsync(SocketGuild guild, IUserMessage usrMsg)
|
||||
{
|
||||
var messageContent = usrMsg.Content;
|
||||
foreach (var exec in _transformers)
|
||||
{
|
||||
string newContent;
|
||||
if ((newContent = await exec.TransformInput(guild, usrMsg.Channel, usrMsg.Author, messageContent))
|
||||
!= messageContent.ToLowerInvariant())
|
||||
{
|
||||
messageContent = newContent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return messageContent;
|
||||
}
|
||||
|
||||
public async Task<bool> RunLateBlockersAsync(ICommandContext ctx, CommandInfo cmd)
|
||||
{
|
||||
foreach (var exec in _lateBlockers)
|
||||
{
|
||||
if (await exec.TryBlockLate(ctx, cmd.Module.GetTopLevelModule().Name, cmd))
|
||||
{
|
||||
Log.Information("Late blocking User [{0}] Command: [{1}] in [{2}]",
|
||||
ctx.User,
|
||||
cmd.Aliases[0],
|
||||
exec.GetType().Name);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task RunLateExecutorsAsync(SocketGuild guild, IUserMessage usrMsg)
|
||||
{
|
||||
foreach (var exec in _lateExecutors)
|
||||
{
|
||||
try
|
||||
{
|
||||
await exec.LateExecute(guild, usrMsg).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error in {TypeName} late executor: {ErrorMessage}",
|
||||
exec.GetType().Name,
|
||||
ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -24,7 +24,6 @@ namespace NadekoBot.Services
|
||||
private IDatabase _db => _con.GetDatabase();
|
||||
|
||||
private const string _basePath = "data/";
|
||||
private const string _oldBasePath = "data/images/";
|
||||
private const string _cardsPath = "data/images/cards";
|
||||
|
||||
public ImageUrls ImageUrls { get; private set; }
|
||||
@@ -79,112 +78,11 @@ namespace NadekoBot.Services
|
||||
_con = con;
|
||||
_creds = creds;
|
||||
_http = new HttpClient();
|
||||
|
||||
Migrate();
|
||||
|
||||
ImageUrls = JsonConvert.DeserializeObject<ImageUrls>(
|
||||
File.ReadAllText(Path.Combine(_basePath, "images.json")));
|
||||
}
|
||||
|
||||
private void Migrate()
|
||||
{
|
||||
try
|
||||
{
|
||||
Migrate1();
|
||||
Migrate2();
|
||||
Migrate3();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning(ex.Message);
|
||||
Log.Error("Something has been incorrectly formatted in your 'images.json' file.\n" +
|
||||
"Use the 'images_example.json' file as reference to fix it and restart the bot.");
|
||||
}
|
||||
}
|
||||
|
||||
private void Migrate1()
|
||||
{
|
||||
if (!File.Exists(Path.Combine(_oldBasePath, "images.json")))
|
||||
return;
|
||||
Log.Information("Migrating images v0 to images v1.");
|
||||
// load old images
|
||||
var oldUrls = JsonConvert.DeserializeObject<ImageUrls>(
|
||||
File.ReadAllText(Path.Combine(_oldBasePath, "images.json")));
|
||||
// load new images
|
||||
var newUrls = JsonConvert.DeserializeObject<ImageUrls>(
|
||||
File.ReadAllText(Path.Combine(_basePath, "images.json")));
|
||||
|
||||
//swap new links with old ones if set. Also update old links.
|
||||
newUrls.Coins = oldUrls.Coins;
|
||||
|
||||
newUrls.Currency = oldUrls.Currency;
|
||||
newUrls.Dice = oldUrls.Dice;
|
||||
newUrls.Rategirl = oldUrls.Rategirl;
|
||||
newUrls.Xp = oldUrls.Xp;
|
||||
newUrls.Version = 1;
|
||||
|
||||
File.WriteAllText(Path.Combine(_basePath, "images.json"),
|
||||
JsonConvert.SerializeObject(newUrls, Formatting.Indented));
|
||||
File.Delete(Path.Combine(_oldBasePath, "images.json"));
|
||||
}
|
||||
|
||||
private void Migrate2()
|
||||
{
|
||||
// load new images
|
||||
var urls = JsonConvert.DeserializeObject<ImageUrls>(File.ReadAllText(Path.Combine(_basePath, "images.json")));
|
||||
|
||||
if (urls.Version >= 2)
|
||||
return;
|
||||
Log.Information("Migrating images v1 to images v2.");
|
||||
urls.Version = 2;
|
||||
|
||||
var prefix = $"{_creds.RedisKey()}_localimg_";
|
||||
_db.KeyDelete(new[] {
|
||||
prefix + "heads",
|
||||
prefix + "tails",
|
||||
prefix + "dice",
|
||||
prefix + "slot_background",
|
||||
prefix + "slotnumbers",
|
||||
prefix + "slotemojis",
|
||||
prefix + "wife_matrix",
|
||||
prefix + "rategirl_dot",
|
||||
prefix + "xp_card",
|
||||
prefix + "rip",
|
||||
prefix + "rip_overlay" }
|
||||
.Select(x => (RedisKey)x).ToArray());
|
||||
|
||||
File.WriteAllText(Path.Combine(_basePath, "images.json"), JsonConvert.SerializeObject(urls, Formatting.Indented));
|
||||
}
|
||||
|
||||
private void Migrate3()
|
||||
{
|
||||
var urls = JsonConvert.DeserializeObject<ImageUrls>(
|
||||
File.ReadAllText(Path.Combine(_basePath, "images.json")));
|
||||
|
||||
if (urls.Version >= 3)
|
||||
return;
|
||||
urls.Version = 3;
|
||||
Log.Information("Migrating images v2 to images v3.");
|
||||
|
||||
var baseStr = "https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/currency/";
|
||||
|
||||
var replacementTable = new Dictionary<Uri, Uri>()
|
||||
{
|
||||
{new Uri(baseStr + "0.jpg"), new Uri(baseStr + "0.png") },
|
||||
{new Uri(baseStr + "1.jpg"), new Uri(baseStr + "1.png") },
|
||||
{new Uri(baseStr + "2.jpg"), new Uri(baseStr + "2.png") }
|
||||
};
|
||||
|
||||
if (replacementTable.Keys.Any(x => urls.Currency.Contains(x)))
|
||||
{
|
||||
urls.Currency = urls.Currency.Select(x => replacementTable.TryGetValue(x, out var newUri)
|
||||
? newUri
|
||||
: x).Append(new Uri(baseStr + "3.png"))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
File.WriteAllText(Path.Combine(_basePath, "images.json"), JsonConvert.SerializeObject(urls, Formatting.Indented));
|
||||
}
|
||||
|
||||
public async Task<bool> AllKeysExist()
|
||||
{
|
||||
try
|
||||
|
Reference in New Issue
Block a user