diff --git a/src/NadekoBot/Modules/Administration/AutoAssignRoleCommands.cs b/src/NadekoBot/Modules/Administration/AutoAssignRoleCommands.cs index a2f53f4e..54d2e244 100644 --- a/src/NadekoBot/Modules/Administration/AutoAssignRoleCommands.cs +++ b/src/NadekoBot/Modules/Administration/AutoAssignRoleCommands.cs @@ -37,7 +37,7 @@ namespace NadekoBot.Modules.Administration if (role == null) { conf.AutoAssignRoleId = 0; - _service.AutoAssignedRoles.TryRemove(Context.Guild.Id, out ulong throwaway); + _service.AutoAssignedRoles.TryRemove(Context.Guild.Id, out _); } else { diff --git a/src/NadekoBot/Modules/Administration/ProtectionCommands.cs b/src/NadekoBot/Modules/Administration/ProtectionCommands.cs index 8468f270..72e68bf9 100644 --- a/src/NadekoBot/Modules/Administration/ProtectionCommands.cs +++ b/src/NadekoBot/Modules/Administration/ProtectionCommands.cs @@ -60,9 +60,8 @@ namespace NadekoBot.Modules.Administration await ReplyErrorLocalized("raid_time", 2, 300).ConfigureAwait(false); return; } - - AntiRaidStats throwaway; - if (_service.AntiRaidGuilds.TryRemove(Context.Guild.Id, out throwaway)) + + if (_service.AntiRaidGuilds.TryRemove(Context.Guild.Id, out _)) { using (var uow = _db.UnitOfWork) { @@ -117,11 +116,10 @@ namespace NadekoBot.Modules.Administration { if (messageCount < 2 || messageCount > 10) return; - - AntiSpamStats throwaway; - if (_service.AntiSpamGuilds.TryRemove(Context.Guild.Id, out throwaway)) + + if (_service.AntiSpamGuilds.TryRemove(Context.Guild.Id, out var removed)) { - throwaway.UserStats.ForEach(x => x.Value.Dispose()); + removed.UserStats.ForEach(x => x.Value.Dispose()); using (var uow = _db.UnitOfWork) { var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting) diff --git a/src/NadekoBot/Modules/Administration/RatelimitCommands.cs b/src/NadekoBot/Modules/Administration/RatelimitCommands.cs index 7be7acd2..40b37884 100644 --- a/src/NadekoBot/Modules/Administration/RatelimitCommands.cs +++ b/src/NadekoBot/Modules/Administration/RatelimitCommands.cs @@ -30,9 +30,9 @@ namespace NadekoBot.Modules.Administration [RequireUserPermission(GuildPermission.ManageMessages)] public async Task Slowmode() { - if (_service.RatelimitingChannels.TryRemove(Context.Channel.Id, out Ratelimiter throwaway)) + if (_service.RatelimitingChannels.TryRemove(Context.Channel.Id, out Ratelimiter removed)) { - throwaway.CancelSource.Cancel(); + removed.CancelSource.Cancel(); await ReplyConfirmLocalized("slowmode_disabled").ConfigureAwait(false); } } diff --git a/src/NadekoBot/Modules/Administration/Services/PlayingRotateService.cs b/src/NadekoBot/Modules/Administration/Services/PlayingRotateService.cs index d7b5fe2e..9786d085 100644 --- a/src/NadekoBot/Modules/Administration/Services/PlayingRotateService.cs +++ b/src/NadekoBot/Modules/Administration/Services/PlayingRotateService.cs @@ -18,17 +18,19 @@ namespace NadekoBot.Modules.Administration.Services private readonly Logger _log; private readonly Replacer _rep; private readonly DbService _db; - public BotConfig BotConfig { get; private set; } //todo load whole botconifg, not just for this service when you have the time + private readonly IBotConfigProvider _bcp; + + public BotConfig BotConfig => _bcp.BotConfig; private class TimerState { public int Index { get; set; } } - public PlayingRotateService(DiscordSocketClient client, BotConfig bc, MusicService music, DbService db) + public PlayingRotateService(DiscordSocketClient client, IBotConfigProvider bcp, MusicService music, DbService db) { _client = client; - BotConfig = bc; + _bcp = bcp; _music = music; _db = db; _log = LogManager.GetCurrentClassLogger(); @@ -42,11 +44,9 @@ namespace NadekoBot.Modules.Administration.Services { try { - using (var uow = _db.UnitOfWork) - { - BotConfig = uow.BotConfig.GetOrCreate(); - } - var state = (TimerState)objState; + bcp.Reload(); + + var state = (TimerState)objState; if (!BotConfig.RotatingStatuses) return; if (state.Index >= BotConfig.RotatingStatusMessages.Count) diff --git a/src/NadekoBot/Modules/Administration/Services/SelfService.cs b/src/NadekoBot/Modules/Administration/Services/SelfService.cs index c5ad3ce9..93699858 100644 --- a/src/NadekoBot/Modules/Administration/Services/SelfService.cs +++ b/src/NadekoBot/Modules/Administration/Services/SelfService.cs @@ -15,7 +15,6 @@ namespace NadekoBot.Modules.Administration.Services { public class SelfService : ILateExecutor, INService { - //todo bot config public bool ForwardDMs => _bc.BotConfig.ForwardMessages; public bool ForwardDMsToAllOwners => _bc.BotConfig.ForwardToAllOwners; diff --git a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs index ffead6e5..b466b158 100644 --- a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -433,8 +433,7 @@ namespace NadekoBot.Modules.CustomReactions } else { - uint throwaway; - if (_service.ReactionStats.TryRemove(trigger, out throwaway)) + if (_service.ReactionStats.TryRemove(trigger, out _)) { await ReplyErrorLocalized("stats_cleared", Format.Bold(trigger)).ConfigureAwait(false); } diff --git a/src/NadekoBot/Modules/Gambling/AnimalRacingCommands.cs b/src/NadekoBot/Modules/Gambling/AnimalRacingCommands.cs index f322a517..f4777bef 100644 --- a/src/NadekoBot/Modules/Gambling/AnimalRacingCommands.cs +++ b/src/NadekoBot/Modules/Gambling/AnimalRacingCommands.cs @@ -158,8 +158,7 @@ namespace NadekoBot.Modules.Gambling private void End() { - AnimalRace throwaway; - AnimalRaces.TryRemove(_serverId, out throwaway); + AnimalRaces.TryRemove(_serverId, out _); } private async Task StartRace() diff --git a/src/NadekoBot/Modules/Games/CleverBotCommands.cs b/src/NadekoBot/Modules/Games/CleverBotCommands.cs index a1534800..3373a98a 100644 --- a/src/NadekoBot/Modules/Games/CleverBotCommands.cs +++ b/src/NadekoBot/Modules/Games/CleverBotCommands.cs @@ -28,7 +28,7 @@ namespace NadekoBot.Modules.Games { var channel = (ITextChannel)Context.Channel; - if (_service.ChatterBotGuilds.TryRemove(channel.Guild.Id, out Lazy throwaway)) + if (_service.ChatterBotGuilds.TryRemove(channel.Guild.Id, out _)) { using (var uow = _db.UnitOfWork) { diff --git a/src/NadekoBot/Modules/Games/Games.cs b/src/NadekoBot/Modules/Games/Games.cs index 27ddd19e..c5cc0b13 100644 --- a/src/NadekoBot/Modules/Games/Games.cs +++ b/src/NadekoBot/Modules/Games/Games.cs @@ -11,6 +11,14 @@ using NadekoBot.Modules.Games.Services; namespace NadekoBot.Modules.Games { + /*todo more games + - Blackjack + - Shiritori + - Simple RPG adventure + - The nunchi game + - Wheel of fortune + - Connect 4 + */ public partial class Games : NadekoTopLevelModule { private readonly IImagesService _images; diff --git a/src/NadekoBot/Modules/Games/HangmanCommands.cs b/src/NadekoBot/Modules/Games/HangmanCommands.cs index f3e6a32a..5ebd979a 100644 --- a/src/NadekoBot/Modules/Games/HangmanCommands.cs +++ b/src/NadekoBot/Modules/Games/HangmanCommands.cs @@ -45,7 +45,7 @@ namespace NadekoBot.Modules.Games hm.OnEnded += g => { - HangmanGames.TryRemove(g.GameChannel.Id, out HangmanGame throwaway); + HangmanGames.TryRemove(g.GameChannel.Id, out _); }; try { @@ -54,8 +54,8 @@ namespace NadekoBot.Modules.Games catch (Exception ex) { try { await Context.Channel.SendErrorAsync(GetText("hangman_start_errored") + " " + ex.Message).ConfigureAwait(false); } catch { } - HangmanGames.TryRemove(Context.Channel.Id, out HangmanGame throwaway); - throwaway.Dispose(); + if(HangmanGames.TryRemove(Context.Channel.Id, out var removed)) + removed.Dispose(); return; } @@ -66,9 +66,9 @@ namespace NadekoBot.Modules.Games [RequireContext(ContextType.Guild)] public async Task HangmanStop() { - if (HangmanGames.TryRemove(Context.Channel.Id, out HangmanGame throwaway)) + if (HangmanGames.TryRemove(Context.Channel.Id, out var removed)) { - throwaway.Dispose(); + removed.Dispose(); await ReplyConfirmLocalized("hangman_stopped").ConfigureAwait(false); } } diff --git a/src/NadekoBot/Modules/Permissions/CommandCostCommands.cs b/src/NadekoBot/Modules/Permissions/CommandCostCommands.cs index 13651ca3..ed31eb9e 100644 --- a/src/NadekoBot/Modules/Permissions/CommandCostCommands.cs +++ b/src/NadekoBot/Modules/Permissions/CommandCostCommands.cs @@ -74,8 +74,7 @@ namespace NadekoBot.Modules.Permissions // else // { // bc.CommandCosts.RemoveAt(bc.CommandCosts.IndexOf(cmdPrice)); - // int throwaway; - // _commandCosts.TryRemove(cmdName, out throwaway); + // _commandCosts.TryRemove(cmdName, out _); // } // await uow.CompleteAsync().ConfigureAwait(false); diff --git a/src/NadekoBot/Modules/Utility/BotConfigCommands.cs b/src/NadekoBot/Modules/Utility/BotConfigCommands.cs index 6262bf98..cc91dd9f 100644 --- a/src/NadekoBot/Modules/Utility/BotConfigCommands.cs +++ b/src/NadekoBot/Modules/Utility/BotConfigCommands.cs @@ -10,8 +10,15 @@ namespace NadekoBot.Modules.Utility { public partial class Utility { - public class BotConfigCommands : NadekoSubmodule + public class BotConfigCommands : NadekoSubmodule { + private readonly IBotConfigProvider _service; + + public BotConfigCommands(IBotConfigProvider service) + { + _service = service; + } + [NadekoCommand, Usage, Description, Aliases] [OwnerOnly] public async Task BotConfigEdit() diff --git a/src/NadekoBot/Modules/Utility/CommandMapCommands.cs b/src/NadekoBot/Modules/Utility/CommandMapCommands.cs index 872c43b6..9903dedf 100644 --- a/src/NadekoBot/Modules/Utility/CommandMapCommands.cs +++ b/src/NadekoBot/Modules/Utility/CommandMapCommands.cs @@ -42,10 +42,8 @@ namespace NadekoBot.Modules.Utility if (string.IsNullOrWhiteSpace(mapping)) { - ConcurrentDictionary maps; - string throwaway; - if (!_service.AliasMaps.TryGetValue(Context.Guild.Id, out maps) || - !maps.TryRemove(trigger, out throwaway)) + if (!_service.AliasMaps.TryGetValue(Context.Guild.Id, out var maps) || + !maps.TryRemove(trigger, out _)) { await ReplyErrorLocalized("alias_remove_fail", Format.Code(trigger)).ConfigureAwait(false); return; @@ -112,9 +110,8 @@ namespace NadekoBot.Modules.Utility if (page < 0) return; - - ConcurrentDictionary maps; - if (!_service.AliasMaps.TryGetValue(Context.Guild.Id, out maps) || !maps.Any()) + + if (!_service.AliasMaps.TryGetValue(Context.Guild.Id, out var maps) || !maps.Any()) { await ReplyErrorLocalized("aliases_none").ConfigureAwait(false); return; diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index b49b0562..a92380ca 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -32,7 +32,7 @@ namespace NadekoBot public DiscordSocketClient Client { get; } public CommandService CommandService { get; } - public DbService Db { get; } + private readonly DbService _db; public ImmutableArray AllGuildConfigs { get; private set; } /* I don't know how to make this not be static @@ -64,7 +64,7 @@ namespace NadekoBot TerribleElevatedPermissionCheck(); Credentials = new BotCredentials(); - Db = new DbService(Credentials); + _db = new DbService(Credentials); Client = new DiscordSocketClient(new DiscordSocketConfig { MessageCacheSize = 10, @@ -83,7 +83,7 @@ namespace NadekoBot port = port ?? Credentials.ShardRunPort; _comClient = new ShardComClient(port.Value); - using (var uow = Db.UnitOfWork) + using (var uow = _db.UnitOfWork) { _botConfig = uow.BotConfig.GetOrCreate(); OkColor = new Color(Convert.ToUInt32(_botConfig.OkColor, 16)); @@ -120,20 +120,22 @@ namespace NadekoBot var startingGuildIdList = Client.Guilds.Select(x => (long)x.Id).ToList(); //this unit of work will be used for initialization of all modules too, to prevent multiple queries from running - using (var uow = Db.UnitOfWork) + using (var uow = _db.UnitOfWork) { AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray(); - var localization = new Localization(_botConfig.Locale, AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.Locale), Db); + IBotConfigProvider botConfigProvider = new BotConfigProvider(_db, _botConfig); + + //var localization = new Localization(_botConfig.Locale, AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.Locale), Db); //initialize Services Services = new NServiceProvider.ServiceProviderBuilder() .AddManual(Credentials) - .AddManual(Db) - .AddManual(_botConfig) + .AddManual(_db) .AddManual(Client) .AddManual(CommandService) - .AddManual(localization) + .AddManual(botConfigProvider) + //.AddManual(localization) .AddManual>(AllGuildConfigs) //todo wrap this .AddManual(this) .AddManual(uow) diff --git a/src/NadekoBot/Services/CommandHandler.cs b/src/NadekoBot/Services/CommandHandler.cs index b1a25463..21309283 100644 --- a/src/NadekoBot/Services/CommandHandler.cs +++ b/src/NadekoBot/Services/CommandHandler.cs @@ -261,10 +261,11 @@ namespace NadekoBot.Services } } var prefix = GetPrefix(guild?.Id); + var isPrefixCommand = messageContent == ".prefix"; // execute the command and measure the time it took - if (messageContent.StartsWith(prefix)) + if (messageContent.StartsWith(prefix) || isPrefixCommand) { - var result = await ExecuteCommandAsync(new CommandContext(_client, usrMsg), messageContent, prefix.Length, _services, MultiMatchHandling.Best); + var result = await ExecuteCommandAsync(new CommandContext(_client, usrMsg), messageContent, isPrefixCommand ? 1 : prefix.Length, _services, MultiMatchHandling.Best); execTime = Environment.TickCount - execTime; if (result.Success) diff --git a/src/NadekoBot/Services/IBotConfigProvider.cs b/src/NadekoBot/Services/IBotConfigProvider.cs index 1e65f202..6ef54970 100644 --- a/src/NadekoBot/Services/IBotConfigProvider.cs +++ b/src/NadekoBot/Services/IBotConfigProvider.cs @@ -3,7 +3,7 @@ using NadekoBot.Services.Database.Models; namespace NadekoBot.Services { - public interface IBotConfigProvider : INService + public interface IBotConfigProvider { BotConfig BotConfig { get; } void Reload(); diff --git a/src/NadekoBot/Services/ILocalization.cs b/src/NadekoBot/Services/ILocalization.cs index b9fa898d..c3cfbe94 100644 --- a/src/NadekoBot/Services/ILocalization.cs +++ b/src/NadekoBot/Services/ILocalization.cs @@ -4,7 +4,7 @@ using Discord; namespace NadekoBot.Services { - public interface ILocalization + public interface ILocalization : INService { CultureInfo DefaultCultureInfo { get; } ConcurrentDictionary GuildCultureInfos { get; } diff --git a/src/NadekoBot/Services/Impl/Localization.cs b/src/NadekoBot/Services/Impl/Localization.cs index a1ac9043..d781054b 100644 --- a/src/NadekoBot/Services/Impl/Localization.cs +++ b/src/NadekoBot/Services/Impl/Localization.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using Discord; using NLog; +using NadekoBot.Services.Database.Models; namespace NadekoBot.Services.Impl { @@ -16,10 +17,15 @@ namespace NadekoBot.Services.Impl public CultureInfo DefaultCultureInfo { get; private set; } = CultureInfo.CurrentCulture; private Localization() { } - public Localization(string defaultCulture, IDictionary cultureInfoNames, DbService db) + public Localization(IBotConfigProvider bcp, IEnumerable gcs, DbService db) { _log = LogManager.GetCurrentClassLogger(); + + var cultureInfoNames = gcs.ToDictionary(x => x.GuildId, x => x.Locale); + var defaultCulture = bcp.BotConfig.Locale; + _db = db; + if (string.IsNullOrWhiteSpace(defaultCulture)) DefaultCultureInfo = new CultureInfo("en-US"); else @@ -74,8 +80,7 @@ namespace NadekoBot.Services.Impl public void RemoveGuildCulture(ulong guildId) { - CultureInfo throwaway; - if (GuildCultureInfos.TryRemove(guildId, out throwaway)) + if (GuildCultureInfos.TryRemove(guildId, out var _)) { using (var uow = _db.UnitOfWork) {