Packages can be loaded/unloaded. IUnloadableService interface added whose method Unload, if service implements it, will be called when the module is unloaded.

This commit is contained in:
Master Kwoth
2017-10-05 00:51:12 +02:00
parent 599245b1ca
commit 33ac43e1b5
74 changed files with 866 additions and 520 deletions

View File

@ -2,6 +2,13 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<OutputPath>..\src\NadekoBot\bin\$(Configuration)\netcoreapp2.0\modules\$(AssemblyName)\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
<ItemGroup>

View File

@ -13,7 +13,7 @@ using NLog;
namespace NadekoBot.Modules.Utility.Services
{
public class ConverterService : INService
public class ConverterService : INService, IUnloadableService
{
public List<ConvertUnit> Units { get; } = new List<ConvertUnit>();
private readonly Logger _log;
@ -122,6 +122,12 @@ namespace NadekoBot.Modules.Utility.Services
_log.Warn("Failed updating currency. Ignore this.");
}
}
public Task Unload()
{
_currencyUpdater.Change(Timeout.Infinite, Timeout.Infinite);
return Task.CompletedTask;
}
}
public class MeasurementUnit

View File

@ -33,7 +33,7 @@ namespace NadekoBot.Modules.Utility.Services
.Where(x => x.Guild != null)));
})
.Where(x => x.Item2 != null)
.ToDictionary(x => x.Item1, x => x.Item2));
.ToDictionary(x => x.GuildId, x => x.Item2));
RepeaterReady = true;
});
}

View File

@ -15,7 +15,7 @@ using NLog;
namespace NadekoBot.Modules.Utility.Services
{
public class PatreonRewardsService : INService
public class PatreonRewardsService : INService, IUnloadableService
{
private readonly SemaphoreSlim getPledgesLocker = new SemaphoreSlim(1, 1);
@ -33,15 +33,16 @@ namespace NadekoBot.Modules.Utility.Services
private readonly string cacheFileName = "./patreon-rewards.json";
public PatreonRewardsService(IBotCredentials creds, DbService db, CurrencyService currency,
public PatreonRewardsService(IBotCredentials creds, DbService db,
CurrencyService currency,
DiscordSocketClient client)
{
_log = LogManager.GetCurrentClassLogger();
_creds = creds;
_db = db;
_currency = currency;
if (string.IsNullOrWhiteSpace(creds.PatreonAccessToken))
return;
_log = LogManager.GetCurrentClassLogger();
Updater = new Timer(async (load) => await RefreshPledges((bool)load),
client.ShardId == 0, client.ShardId == 0 ? TimeSpan.Zero : TimeSpan.FromMinutes(2), Interval);
}
@ -171,5 +172,11 @@ namespace NadekoBot.Modules.Utility.Services
claimLockJustInCase.Release();
}
}
public Task Unload()
{
Updater.Change(Timeout.Infinite, Timeout.Infinite);
return Task.CompletedTask;
}
}
}

View File

@ -8,10 +8,10 @@ using Discord.WebSocket;
using NadekoBot.Common.Replacements;
using NadekoBot.Extensions;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Impl;
using NLog;
using System.Collections.Generic;
namespace NadekoBot.Modules.Utility.Services
{
@ -29,8 +29,10 @@ namespace NadekoBot.Modules.Utility.Services
private readonly DiscordSocketClient _client;
private readonly DbService _db;
public RemindService(DiscordSocketClient client, IBotConfigProvider config, DbService db,
StartingGuildsService guilds, IUnitOfWork uow)
public RemindService(DiscordSocketClient client,
IBotConfigProvider config,
DbService db,
StartingGuildsService guilds)
{
_config = config;
_client = client;
@ -39,8 +41,12 @@ namespace NadekoBot.Modules.Utility.Services
cancelSource = new CancellationTokenSource();
cancelAllToken = cancelSource.Token;
var reminders = uow.Reminders.GetIncludedReminders(guilds).ToList();
List<Reminder> reminders;
using (var uow = _db.UnitOfWork)
{
reminders = uow.Reminders.GetIncludedReminders(guilds).ToList();
}
RemindMessageFormat = _config.BotConfig.RemindMessageFormat;
foreach (var r in reminders)

View File

@ -17,22 +17,24 @@ using Discord.Net;
namespace NadekoBot.Modules.Utility.Services
{
public class StreamRoleService : INService
public class StreamRoleService : INService, IUnloadableService
{
private readonly DbService _db;
private readonly DiscordSocketClient _client;
private readonly ConcurrentDictionary<ulong, StreamRoleSettings> guildSettings;
private readonly Logger _log;
public StreamRoleService(DiscordSocketClient client, DbService db, IEnumerable<GuildConfig> gcs)
{
this._db = db;
this._log = LogManager.GetCurrentClassLogger();
this._db = db;
this._client = client;
guildSettings = gcs.ToDictionary(x => x.GuildId, x => x.StreamRole)
.Where(x => x.Value != null && x.Value.Enabled)
.ToConcurrent();
client.GuildMemberUpdated += Client_GuildMemberUpdated;
_client.GuildMemberUpdated += Client_GuildMemberUpdated;
var _ = Task.Run(async () =>
{
@ -47,6 +49,12 @@ namespace NadekoBot.Modules.Utility.Services
});
}
public Task Unload()
{
_client.GuildMemberUpdated -= Client_GuildMemberUpdated;
return Task.CompletedTask;
}
private Task Client_GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after)
{
var _ = Task.Run(async () =>

View File

@ -11,7 +11,7 @@ using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility.Services
{
public class VerboseErrorsService : INService
public class VerboseErrorsService : INService, IUnloadableService
{
private readonly ConcurrentHashSet<ulong> guildsEnabled;
private readonly DbService _db;
@ -24,11 +24,17 @@ namespace NadekoBot.Modules.Utility.Services
_ch = ch;
_hs = hs;
ch.CommandErrored += LogVerboseError;
_ch.CommandErrored += LogVerboseError;
guildsEnabled = new ConcurrentHashSet<ulong>(gcs.Where(x => x.VerboseErrors).Select(x => x.GuildId));
}
public Task Unload()
{
_ch.CommandErrored -= LogVerboseError;
return Task.CompletedTask;
}
private async Task LogVerboseError(CommandInfo cmd, ITextChannel channel, string reason)
{
if (channel == null || !guildsEnabled.Contains(channel.GuildId))
@ -73,6 +79,5 @@ namespace NadekoBot.Modules.Utility.Services
return enabled;
}
}
}