From 4954d44b5ac23d150d10fb270076e9bd17ce24d1 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 12 Oct 2016 17:35:27 +0200 Subject: [PATCH] some small fixes --- .../Administration/Commands/Migration.cs | 203 ++++++++++-------- .../Utility/Commands/UnitConversion.cs | 2 +- src/NadekoBot/NadekoBot.xproj | 3 + .../Impl/GuildConfigRepository.cs | 21 +- src/NadekoBot/_Extensions/Extensions.cs | 9 + src/NadekoBot/{ => data}/units.json | 0 src/NadekoBot/project.json | 2 +- 7 files changed, 138 insertions(+), 102 deletions(-) rename src/NadekoBot/{ => data}/units.json (100%) diff --git a/src/NadekoBot/Modules/Administration/Commands/Migration.cs b/src/NadekoBot/Modules/Administration/Commands/Migration.cs index 992584ac..598d36ac 100644 --- a/src/NadekoBot/Modules/Administration/Commands/Migration.cs +++ b/src/NadekoBot/Modules/Administration/Commands/Migration.cs @@ -51,7 +51,7 @@ namespace NadekoBot.Modules.Administration switch (i) { case 0: - await Migrate0_9To1_0(); + Migrate0_9To1_0(); break; } } @@ -64,19 +64,20 @@ namespace NadekoBot.Modules.Administration } } - private async Task Migrate0_9To1_0() + private void Migrate0_9To1_0() { using (var uow = DbHandler.UnitOfWork()) { var botConfig = uow.BotConfig.GetOrCreate(); MigrateConfig0_9(uow, botConfig); - MigratePermissions0_9(uow); + //MigratePermissions0_9(uow); MigrateServerSpecificConfigs0_9(uow); - MigrateDb0_9(uow); + //MigrateDb0_9(uow); //NOW save it botConfig.MigrationVersion = 1; - await uow.CompleteAsync().ConfigureAwait(false); + _log.Warn("Writing to disc"); + uow.Complete(); } } @@ -95,6 +96,7 @@ namespace NadekoBot.Modules.Administration com.CommandText = "SELECT * FROM Announcement"; var reader = com.ExecuteReader(); + var i = 0; while (reader.Read()) { var gid = (ulong)(long)reader["ServerId"]; @@ -106,7 +108,7 @@ namespace NadekoBot.Modules.Administration var byeDM = (long)reader["ByePM"] == 1; var byeChannel = (ulong)(long)reader["ByeChannelId"]; var byeMsg = (string)reader["ByeText"]; - bool grdel = (long)reader["DeleteGreetMessages"] == 1; + var grdel = false; var byedel = grdel; var gc = uow.GuildConfigs.For(gid); @@ -122,14 +124,17 @@ namespace NadekoBot.Modules.Administration gc.ChannelByeMessageText = byeMsg; gc.AutoDeleteByeMessages = gc.AutoDeleteGreetMessages = grdel; + _log.Info(++i); } var com2 = db.CreateCommand(); com.CommandText = "SELECT * FROM CurrencyState"; + i = 0; var reader2 = com.ExecuteReader(); while (reader2.Read()) { + _log.Info(++i); uow.Currency.Add(new Currency() { Amount = (long)reader2["Value"], @@ -171,57 +176,68 @@ namespace NadekoBot.Modules.Administration _log.Warn(ex, "ServerSpecificConfig deserialization failed"); return; } - - foreach (var config in configs) - { - var guildId = config.Key; - var data = config.Value; - - var guildConfig = uow.GuildConfigs.For(guildId); - - guildConfig.AutoAssignRoleId = data.AutoAssignedRole; - guildConfig.DeleteMessageOnCommand = data.AutoDeleteMessagesOnCommand; - guildConfig.DefaultMusicVolume = data.DefaultMusicVolume; - guildConfig.ExclusiveSelfAssignedRoles = data.ExclusiveSelfAssignedRoles; - guildConfig.GenerateCurrencyChannelIds = new HashSet(data.GenerateCurrencyChannels.Select(gc => new GCChannelId() { ChannelId = gc.Key })); - uow.SelfAssignedRoles.AddRange(data.ListOfSelfAssignableRoles.Select(r => new SelfAssignedRole() { GuildId = guildId, RoleId = r }).ToArray()); - var logSetting = guildConfig.LogSetting; - guildConfig.LogSetting.IsLogging = data.LogChannel != null; - guildConfig.LogSetting.ChannelId = data.LogChannel ?? 0; - guildConfig.LogSetting.IgnoredChannels = new HashSet(data.LogserverIgnoreChannels.Select(id => new IgnoredLogChannel() { ChannelId = id })); - - guildConfig.LogSetting.LogUserPresence = data.LogPresenceChannel != null; - guildConfig.LogSetting.UserPresenceChannelId = data.LogPresenceChannel ?? 0; - - - guildConfig.FollowedStreams = new HashSet(data.ObservingStreams.Select(x => + var i = 0; + var selfAssRoles = new ConcurrentHashSet(); + configs + .Select(p => new { data = p.Value, gconfig = uow.GuildConfigs.For(p.Key) }) + .AsParallel() + .ForAll(config => { - FollowedStream.FollowedStreamType type = FollowedStream.FollowedStreamType.Twitch; - switch (x.Type) + try { - case StreamNotificationConfig0_9.StreamType.Twitch: - type = FollowedStream.FollowedStreamType.Twitch; - break; - case StreamNotificationConfig0_9.StreamType.Beam: - type = FollowedStream.FollowedStreamType.Beam; - break; - case StreamNotificationConfig0_9.StreamType.Hitbox: - type = FollowedStream.FollowedStreamType.Hitbox; - break; - default: - break; - } + var guildConfig = config.gconfig; + var data = config.data; - return new FollowedStream() + guildConfig.AutoAssignRoleId = data.AutoAssignedRole; + guildConfig.DeleteMessageOnCommand = data.AutoDeleteMessagesOnCommand; + guildConfig.DefaultMusicVolume = data.DefaultMusicVolume; + guildConfig.ExclusiveSelfAssignedRoles = data.ExclusiveSelfAssignedRoles; + guildConfig.GenerateCurrencyChannelIds = new HashSet(data.GenerateCurrencyChannels.Select(gc => new GCChannelId() { ChannelId = gc.Key })); + selfAssRoles.AddRange(data.ListOfSelfAssignableRoles.Select(r => new SelfAssignedRole() { GuildId = guildConfig.GuildId, RoleId = r }).ToArray()); + var logSetting = guildConfig.LogSetting; + guildConfig.LogSetting.IsLogging = data.LogChannel != null; + guildConfig.LogSetting.ChannelId = data.LogChannel ?? 0; + guildConfig.LogSetting.IgnoredChannels = new HashSet(data.LogserverIgnoreChannels.Select(id => new IgnoredLogChannel() { ChannelId = id })); + + guildConfig.LogSetting.LogUserPresence = data.LogPresenceChannel != null; + guildConfig.LogSetting.UserPresenceChannelId = data.LogPresenceChannel ?? 0; + + + guildConfig.FollowedStreams = new HashSet(data.ObservingStreams.Select(x => + { + FollowedStream.FollowedStreamType type = FollowedStream.FollowedStreamType.Twitch; + switch (x.Type) + { + case StreamNotificationConfig0_9.StreamType.Twitch: + type = FollowedStream.FollowedStreamType.Twitch; + break; + case StreamNotificationConfig0_9.StreamType.Beam: + type = FollowedStream.FollowedStreamType.Beam; + break; + case StreamNotificationConfig0_9.StreamType.Hitbox: + type = FollowedStream.FollowedStreamType.Hitbox; + break; + default: + break; + } + + return new FollowedStream() + { + ChannelId = x.ChannelId, + GuildId = guildConfig.GuildId, + Username = x.Username.ToLowerInvariant(), + Type = type + }; + })); + guildConfig.VoicePlusTextEnabled = data.VoicePlusTextEnabled; + _log.Info("Migrating SpecificConfig for {0} done ({1})", guildConfig.GuildId, ++i); + } + catch (Exception ex) { - ChannelId = x.ChannelId, - GuildId = guildId, - Username = x.Username.ToLowerInvariant(), - Type = type - }; - })); - guildConfig.VoicePlusTextEnabled = data.VoicePlusTextEnabled; - } + _log.Error(ex); + } + }); + uow.SelfAssignedRoles.AddRange(selfAssRoles.ToArray()); try { File.Move("data/ServerSpecificConfigs.json", "data/DELETE_ME_ServerSpecificCOnfigs.json"); } catch { } } @@ -245,48 +261,55 @@ namespace NadekoBot.Modules.Administration } catch { } } - foreach (var perms in PermissionsDict) - { - var guildId = perms.Key; - var data = perms.Value; + var i = 0; + PermissionsDict + .Select(p => new { data = p.Value, gconfig = uow.GuildConfigs.For(p.Key) }) + .AsParallel() + .ForAll(perms => + { + try + { + var data = perms.data; + var gconfig = perms.gconfig; - _log.Info("Migrating data from permissions folder for {0}", guildId); + gconfig.PermissionRole = data.PermissionsControllerRole; + gconfig.VerbosePermissions = data.Verbose; + gconfig.FilteredWords = new HashSet(data.Words.Select(w => w.ToLowerInvariant()) + .Distinct() + .Select(w => new FilteredWord() { Word = w })); + gconfig.FilterWords = data.Permissions.FilterWords; + gconfig.FilterInvites = data.Permissions.FilterInvites; - var gconfig = uow.GuildConfigs.For(guildId); + gconfig.FilterInvitesChannelIds = new HashSet(); + gconfig.FilterInvitesChannelIds.AddRange(data.ChannelPermissions.Where(kvp => kvp.Value.FilterInvites) + .Select(cp => new FilterChannelId() + { + ChannelId = cp.Key + })); - gconfig.PermissionRole = data.PermissionsControllerRole; - gconfig.VerbosePermissions = data.Verbose; - gconfig.FilteredWords = new HashSet(data.Words.Select(w => w.ToLowerInvariant()) - .Distinct() - .Select(w => new FilteredWord() { Word = w })); - gconfig.FilterWords = data.Permissions.FilterWords; - gconfig.FilterInvites = data.Permissions.FilterInvites; + gconfig.FilterWordsChannelIds = new HashSet(); + gconfig.FilterWordsChannelIds.AddRange(data.ChannelPermissions.Where(kvp => kvp.Value.FilterWords) + .Select(cp => new FilterChannelId() + { + ChannelId = cp.Key + })); - gconfig.FilterInvitesChannelIds = new HashSet(); - gconfig.FilterInvitesChannelIds.AddRange(data.ChannelPermissions.Where(kvp => kvp.Value.FilterInvites) - .Select(cp => new FilterChannelId() - { - ChannelId = cp.Key - })); + gconfig.CommandCooldowns = new HashSet(data.CommandCooldowns + .Where(cc => !string.IsNullOrWhiteSpace(cc.Key) && cc.Value > 0) + .Select(cc => new CommandCooldown() + { + CommandName = cc.Key, + Seconds = cc.Value + })); + _log.Info("Migrating data from permissions folder for {0} done ({1})", gconfig.GuildId, ++i); + } + catch (Exception ex) + { + _log.Error(ex); + } + }); - gconfig.FilterWordsChannelIds = new HashSet(); - gconfig.FilterWordsChannelIds.AddRange(data.ChannelPermissions.Where(kvp => kvp.Value.FilterWords) - .Select(cp => new FilterChannelId() - { - ChannelId = cp.Key - })); - - gconfig.CommandCooldowns = new HashSet(data.CommandCooldowns - .Where(cc => !string.IsNullOrWhiteSpace(cc.Key) && cc.Value > 0) - .Select(cc => new CommandCooldown() - { - CommandName = cc.Key, - Seconds = cc.Value - })); - var smodules = data.Permissions.Modules.Where(m => !m.Value); - - try { Directory.Move("data/permissions","data/DELETE_ME_permissions"); } catch { } - } + try { Directory.Move("data/permissions", "data/DELETE_ME_permissions"); } catch { } } diff --git a/src/NadekoBot/Modules/Utility/Commands/UnitConversion.cs b/src/NadekoBot/Modules/Utility/Commands/UnitConversion.cs index ead333ca..f9b05093 100644 --- a/src/NadekoBot/Modules/Utility/Commands/UnitConversion.cs +++ b/src/NadekoBot/Modules/Utility/Commands/UnitConversion.cs @@ -38,7 +38,7 @@ namespace NadekoBot.Modules.Utility //need to do this the first time if (uow.ConverterUnits.Empty()) { - var content = JsonConvert.DeserializeObject>(File.ReadAllText("units.json")).Select(u => new ConvertUnit() + var content = JsonConvert.DeserializeObject>(File.ReadAllText("data/units.json")).Select(u => new ConvertUnit() { Modifier = u.Modifier, UnitType = u.UnitType, diff --git a/src/NadekoBot/NadekoBot.xproj b/src/NadekoBot/NadekoBot.xproj index 3cf706f4..901e69fd 100644 --- a/src/NadekoBot/NadekoBot.xproj +++ b/src/NadekoBot/NadekoBot.xproj @@ -15,5 +15,8 @@ 2.0 + + + \ No newline at end of file diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs index 44d6a938..7c17af34 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs @@ -38,16 +38,17 @@ namespace NadekoBot.Services.Database.Repositories.Impl /// public GuildConfig For(ulong guildId) { - var config = _set.Include(gc => gc.FollowedStreams) - .Include(gc => gc.LogSetting) - .ThenInclude(ls => ls.IgnoredChannels) - .Include(gc => gc.LogSetting) - .ThenInclude(ls => ls.IgnoredVoicePresenceChannelIds) - .Include(gc => gc.FilterInvitesChannelIds) - .Include(gc => gc.FilterWordsChannelIds) - .Include(gc => gc.FilteredWords) - .Include(gc => gc.GenerateCurrencyChannelIds) - .Include(gc => gc.CommandCooldowns) + var config = _set + //.Include(gc => gc.FollowedStreams) + // .Include(gc => gc.LogSetting) + // .ThenInclude(ls => ls.IgnoredChannels) + //.Include(gc => gc.LogSetting) + // .ThenInclude(ls => ls.IgnoredVoicePresenceChannelIds) + //.Include(gc => gc.FilterInvitesChannelIds) + //.Include(gc => gc.FilterWordsChannelIds) + //.Include(gc => gc.FilteredWords) + //.Include(gc => gc.GenerateCurrencyChannelIds) + //.Include(gc => gc.CommandCooldowns) .FirstOrDefault(c => c.GuildId == guildId); if (config == null) diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index b1722e12..43283a64 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -2,6 +2,7 @@ using Discord.WebSocket; using ImageProcessorCore; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -31,6 +32,14 @@ namespace NadekoBot.Extensions } } + public static void AddRange(this ConcurrentHashSet target, IEnumerable elements) where T : class + { + foreach (var item in elements) + { + target.Add(item); + } + } + public static bool IsInteger(this decimal number) => number == Math.Truncate(number); public static string SanitizeMentions(this string str) => diff --git a/src/NadekoBot/units.json b/src/NadekoBot/data/units.json similarity index 100% rename from src/NadekoBot/units.json rename to src/NadekoBot/data/units.json diff --git a/src/NadekoBot/project.json b/src/NadekoBot/project.json index d7b2273a..67066f42 100644 --- a/src/NadekoBot/project.json +++ b/src/NadekoBot/project.json @@ -7,7 +7,7 @@ "emitEntryPoint": true, "allowUnsafe": true, "compile": { - "exclude": [ ], + "exclude": [ "data" ] }, "copyToOutput": { "include": [ "data", "libs" ],