private static void LoadSharedDataFromDatabase() { Console.Write("\r[3/5] Loading data from database... "); // Placing performance-sensitive data into memory, instead of it being read from the database ConcurrentHashSet <ulong> blockedChannels; ConcurrentHashSet <ulong> blockedUsers; ConcurrentDictionary <ulong, CachedGuildConfig> guildConfigurations; ConcurrentDictionary <ulong, ConcurrentHashSet <Filter> > filters; ConcurrentDictionary <ulong, ConcurrentHashSet <TextReaction> > treactions; ConcurrentDictionary <ulong, ConcurrentHashSet <EmojiReaction> > ereactions; ConcurrentDictionary <ulong, int> msgcount; using (DatabaseContext db = GlobalDatabaseContextBuilder.CreateContext()) { blockedChannels = new ConcurrentHashSet <ulong>(db.BlockedChannels.Select(c => c.ChannelId)); blockedUsers = new ConcurrentHashSet <ulong>(db.BlockedUsers.Select(u => u.UserId)); guildConfigurations = new ConcurrentDictionary <ulong, CachedGuildConfig>(db.GuildConfig.Select( gcfg => new KeyValuePair <ulong, CachedGuildConfig>(gcfg.GuildId, new CachedGuildConfig { AntispamSettings = new AntispamSettings { Action = gcfg.AntispamAction, Enabled = gcfg.AntispamEnabled, Sensitivity = gcfg.AntispamSensitivity }, Currency = gcfg.Currency, LinkfilterSettings = new LinkfilterSettings { BlockBooterWebsites = gcfg.LinkfilterBootersEnabled, BlockDiscordInvites = gcfg.LinkfilterDiscordInvitesEnabled, BlockDisturbingWebsites = gcfg.LinkfilterDisturbingWebsitesEnabled, BlockIpLoggingWebsites = gcfg.LinkfilterIpLoggersEnabled, BlockUrlShorteners = gcfg.LinkfilterUrlShortenersEnabled, Enabled = gcfg.LinkfilterEnabled }, LogChannelId = gcfg.LogChannelId, Prefix = gcfg.Prefix, RatelimitSettings = new RatelimitSettings { Action = gcfg.RatelimitAction, Enabled = gcfg.RatelimitEnabled, Sensitivity = gcfg.RatelimitSensitivity }, ReactionResponse = gcfg.ReactionResponse, SuggestionsEnabled = gcfg.SuggestionsEnabled } ))); filters = new ConcurrentDictionary <ulong, ConcurrentHashSet <Filter> >( db.Filters .GroupBy(f => f.GuildId) .ToDictionary(g => g.Key, g => new ConcurrentHashSet <Filter>(g.Select(f => new Filter(f.Id, f.Trigger)))) ); msgcount = new ConcurrentDictionary <ulong, int>( db.MessageCount .GroupBy(ui => ui.UserId) .ToDictionary(g => g.Key, g => g.First().MessageCount) ); treactions = new ConcurrentDictionary <ulong, ConcurrentHashSet <TextReaction> >( db.TextReactions .Include(t => t.DbTriggers) .AsEnumerable() .GroupBy(tr => tr.GuildId) .ToDictionary(g => g.Key, g => new ConcurrentHashSet <TextReaction>(g.Select(tr => new TextReaction(tr.Id, tr.Triggers, tr.Response, true)))) ); ereactions = new ConcurrentDictionary <ulong, ConcurrentHashSet <EmojiReaction> >( db.EmojiReactions .Include(t => t.DbTriggers) .AsEnumerable() .GroupBy(er => er.GuildId) .ToDictionary(g => g.Key, g => new ConcurrentHashSet <EmojiReaction>(g.Select(er => new EmojiReaction(er.Id, er.Triggers, er.Reaction, true)))) ); } var logger = new Logger(BotConfiguration); foreach (Logger.SpecialLoggingRule rule in BotConfiguration.SpecialLoggerRules) { logger.ApplySpecialLoggingRule(rule); } SharedData = new SharedData { BlockedChannels = blockedChannels, BlockedUsers = blockedUsers, BotConfiguration = BotConfiguration, MainLoopCts = new CancellationTokenSource(), EmojiReactions = ereactions, Filters = filters, GuildConfigurations = guildConfigurations, LogProvider = logger, MessageCount = msgcount, TextReactions = treactions, UptimeInformation = new UptimeInformation(Process.GetCurrentProcess().StartTime) }; }
private static async Task LoadSharedDataFromDatabaseAsync() { Console.Write("\r[3/5] Loading data from database... "); // Placing performance-sensitive data into memory, instead of it being read from the database // Blocked users IReadOnlyList <(ulong, string)> blockedusr_db = await DatabaseService.GetAllBlockedUsersAsync(); var blockedusr = new ConcurrentHashSet <ulong>(); foreach ((ulong uid, string reason) in blockedusr_db) { blockedusr.Add(uid); } // Blocked channels IReadOnlyList <(ulong, string)> blockedchn_db = await DatabaseService.GetAllBlockedChannelsAsync(); var blockedchn = new ConcurrentHashSet <ulong>(); foreach ((ulong cid, string reason) in blockedchn_db) { blockedchn.Add(cid); } // Guild config IReadOnlyDictionary <ulong, CachedGuildConfig> gcfg_db = await DatabaseService.GetAllCachedGuildConfigurationsAsync(); var gcfg = new ConcurrentDictionary <ulong, CachedGuildConfig>(); foreach ((ulong gid, CachedGuildConfig cfg) in gcfg_db) { gcfg.TryAdd(gid, cfg); } // Guild filters IReadOnlyList <(ulong, Filter)> gfilters_db = await DatabaseService.GetAllFiltersAsync(); var gfilters = new ConcurrentDictionary <ulong, ConcurrentHashSet <Filter> >(); foreach ((ulong gid, Filter filter) in gfilters_db) { if (!gfilters.ContainsKey(gid)) { gfilters.TryAdd(gid, new ConcurrentHashSet <Filter>()); } gfilters[gid].Add(filter); } // Guild text reactions IReadOnlyDictionary <ulong, List <TextReaction> > gtextreactions_db = await DatabaseService.GetTextReactionsForAllGuildsAsync(); var gtextreactions = new ConcurrentDictionary <ulong, ConcurrentHashSet <TextReaction> >(); foreach ((ulong gid, List <TextReaction> reactions) in gtextreactions_db) { gtextreactions.TryAdd(gid, new ConcurrentHashSet <TextReaction>(reactions)); } // Guild emoji reactions IReadOnlyDictionary <ulong, List <EmojiReaction> > gemojireactions_db = await DatabaseService.GetEmojiReactionsForAllGuildsAsync(); var gemojireactions = new ConcurrentDictionary <ulong, ConcurrentHashSet <EmojiReaction> >(); foreach (KeyValuePair <ulong, List <EmojiReaction> > reaction in gemojireactions_db) { gemojireactions.TryAdd(reaction.Key, new ConcurrentHashSet <EmojiReaction>(reaction.Value)); } // User message count (XP) IReadOnlyDictionary <ulong, ulong> msgcount_db = await DatabaseService.GetXpForAllUsersAsync(); var msgcount = new ConcurrentDictionary <ulong, ulong>(); foreach (KeyValuePair <ulong, ulong> entry in msgcount_db) { msgcount.TryAdd(entry.Key, entry.Value); } SharedData = new SharedData() { BlockedChannels = blockedchn, BlockedUsers = blockedusr, BotConfiguration = BotConfiguration, MainLoopCts = new CancellationTokenSource(), EmojiReactions = gemojireactions, Filters = gfilters, GuildConfigurations = gcfg, LogProvider = new Logger(BotConfiguration), MessageCount = msgcount, TextReactions = gtextreactions }; }