Done v+t, using local cache to avoid querying db every user update

This commit is contained in:
Kwoth 2016-08-31 06:58:55 +02:00
parent a081f1c89c
commit e847c02f41

View File

@ -5,6 +5,7 @@ using NadekoBot.Attributes;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using System; using System;
using System.Collections.Concurrent;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -17,9 +18,10 @@ namespace NadekoBot.Modules.Administration
public class VoicePlusTextCommands public class VoicePlusTextCommands
{ {
Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled); Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
//guildid/voiceplustextenabled
private ConcurrentDictionary<ulong, bool> voicePlusTextCache;
public VoicePlusTextCommands() public VoicePlusTextCommands()
{ {
// changing servers may cause bugs
NadekoBot.Client.UserUpdated += UserUpdatedEventHandler; NadekoBot.Client.UserUpdated += UserUpdatedEventHandler;
} }
@ -33,12 +35,11 @@ namespace NadekoBot.Modules.Administration
{ {
if (before.VoiceChannel == after.VoiceChannel) return; if (before.VoiceChannel == after.VoiceChannel) return;
//todo This is WAY TOO MUCH bool isEnabled;
using (var uow = DbHandler.UnitOfWork()) voicePlusTextCache.TryGetValue(guild.Id, out isEnabled);
{ if (!isEnabled)
if (!uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled)
return; return;
}
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles) if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
{ {
try try
@ -51,6 +52,7 @@ namespace NadekoBot.Modules.Administration
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled = false; uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled = false;
voicePlusTextCache.TryUpdate(guild.Id, false, true);
} }
return; return;
} }
@ -117,6 +119,7 @@ namespace NadekoBot.Modules.Administration
var conf = uow.GuildConfigs.For(guild.Id); var conf = uow.GuildConfigs.For(guild.Id);
isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled; isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled;
} }
voicePlusTextCache.AddOrUpdate(guild.Id, isEnabled, (id, val) => isEnabled);
if (isEnabled) if (isEnabled)
{ {
foreach (var textChannel in guild.GetTextChannels().Where(c => c.Name.EndsWith("-voice"))) foreach (var textChannel in guild.GetTextChannels().Where(c => c.Name.EndsWith("-voice")))