.v+t fixed, persists restarts, etc

This commit is contained in:
Kwoth 2016-10-20 05:09:05 +02:00
parent 83fe5dbce1
commit 2848efcfb7

View File

@ -4,6 +4,7 @@ using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Database;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Linq; using System.Linq;
@ -19,26 +20,31 @@ namespace NadekoBot.Modules.Administration
{ {
Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled); Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
//guildid/voiceplustextenabled //guildid/voiceplustextenabled
private ConcurrentDictionary<ulong, bool> voicePlusTextCache; private ConcurrentHashSet<ulong> voicePlusTextCache;
public VoicePlusTextCommands() public VoicePlusTextCommands()
{ {
NadekoBot.Client.UserUpdated += UserUpdatedEventHandler; using (var uow = DbHandler.UnitOfWork())
voicePlusTextCache = new ConcurrentDictionary<ulong, bool>(); {
voicePlusTextCache = new ConcurrentHashSet<ulong>(uow.GuildConfigs.GetAll().Where(g => g.VoicePlusTextEnabled).Select(g => g.GuildId));
}
NadekoBot.Client.UserVoiceStateUpdated += UserUpdatedEventHandler;
} }
private Task UserUpdatedEventHandler(IGuildUser before, IGuildUser after) private Task UserUpdatedEventHandler(IUser iuser, IVoiceState before, IVoiceState after)
{ {
var user = (iuser as IGuildUser);
var guild = user?.Guild;
if (guild == null)
return Task.CompletedTask;
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
var guild = before.Guild ?? after.Guild;
var botUserPerms = guild.GetCurrentUser().GuildPermissions; var botUserPerms = guild.GetCurrentUser().GuildPermissions;
try try
{ {
if (before.VoiceChannel == after.VoiceChannel) return; if (before.VoiceChannel == after.VoiceChannel) return;
bool isEnabled; if (!voicePlusTextCache.Contains(guild.Id))
voicePlusTextCache.TryGetValue(guild.Id, out isEnabled);
if (!isEnabled)
return; return;
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles) if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
@ -52,8 +58,8 @@ namespace NadekoBot.Modules.Administration
catch { } catch { }
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled = false; uow.GuildConfigs.For(guild.Id).VoicePlusTextEnabled = false;
voicePlusTextCache.TryUpdate(guild.Id, false, true); voicePlusTextCache.TryRemove(guild.Id);
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }
return; return;
@ -65,7 +71,7 @@ namespace NadekoBot.Modules.Administration
{ {
var textChannel = guild.GetTextChannels().Where(t => t.Name == GetChannelName(beforeVch.Name)).FirstOrDefault(); var textChannel = guild.GetTextChannels().Where(t => t.Name == GetChannelName(beforeVch.Name)).FirstOrDefault();
if (textChannel != null) if (textChannel != null)
await textChannel.AddPermissionOverwriteAsync(before, await textChannel.AddPermissionOverwriteAsync(user,
new OverwritePermissions(readMessages: PermValue.Deny, new OverwritePermissions(readMessages: PermValue.Deny,
sendMessages: PermValue.Deny)).ConfigureAwait(false); sendMessages: PermValue.Deny)).ConfigureAwait(false);
} }
@ -82,7 +88,7 @@ namespace NadekoBot.Modules.Administration
new OverwritePermissions(readMessages: PermValue.Deny, new OverwritePermissions(readMessages: PermValue.Deny,
sendMessages: PermValue.Deny)).ConfigureAwait(false); sendMessages: PermValue.Deny)).ConfigureAwait(false);
} }
await textChannel.AddPermissionOverwriteAsync(after, await textChannel.AddPermissionOverwriteAsync(user,
new OverwritePermissions(readMessages: PermValue.Allow, new OverwritePermissions(readMessages: PermValue.Allow,
sendMessages: PermValue.Allow)).ConfigureAwait(false); sendMessages: PermValue.Allow)).ConfigureAwait(false);
} }
@ -122,8 +128,8 @@ namespace NadekoBot.Modules.Administration
isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled; isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled;
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }
voicePlusTextCache.AddOrUpdate(guild.Id, isEnabled, (id, val) => isEnabled); voicePlusTextCache.Add(guild.Id);
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")))
{ {