NadekoBot/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs

262 lines
12 KiB
C#
Raw Normal View History

2016-08-30 14:43:54 +00:00
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services;
2016-12-29 13:56:15 +00:00
using NLog;
2016-08-30 14:43:54 +00:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
2016-12-29 13:56:15 +00:00
using System.Diagnostics;
2016-08-30 14:43:54 +00:00
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
2016-08-30 14:43:54 +00:00
using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
public class VoicePlusTextCommands : NadekoSubmodule
2016-08-30 14:43:54 +00:00
{
private new static readonly Logger _log;
private static readonly Regex _channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
private static readonly ConcurrentHashSet<ulong> _voicePlusTextCache;
private static readonly ConcurrentDictionary<ulong, SemaphoreSlim> _guildLockObjects = new ConcurrentDictionary<ulong, SemaphoreSlim>();
2016-12-08 17:35:34 +00:00
static VoicePlusTextCommands()
2016-08-30 14:43:54 +00:00
{
_log = LogManager.GetCurrentClassLogger();
2016-12-29 13:56:15 +00:00
var sw = Stopwatch.StartNew();
2017-02-14 13:30:21 +00:00
_voicePlusTextCache = new ConcurrentHashSet<ulong>(NadekoBot.AllGuildConfigs.Where(g => g.VoicePlusTextEnabled).Select(g => g.GuildId));
2016-10-20 03:09:05 +00:00
NadekoBot.Client.UserVoiceStateUpdated += UserUpdatedEventHandler;
2016-12-29 13:56:15 +00:00
sw.Stop();
_log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
2016-08-30 14:43:54 +00:00
}
private static Task UserUpdatedEventHandler(SocketUser iuser, SocketVoiceState before, SocketVoiceState after)
2016-08-30 14:43:54 +00:00
{
2016-12-17 00:16:14 +00:00
var user = (iuser as SocketGuildUser);
2016-10-20 03:09:05 +00:00
var guild = user?.Guild;
if (guild == null)
return Task.CompletedTask;
var botUserPerms = guild.CurrentUser.GuildPermissions;
if (before.VoiceChannel == after.VoiceChannel)
return Task.CompletedTask;
2016-08-30 14:43:54 +00:00
if (!_voicePlusTextCache.Contains(guild.Id))
return Task.CompletedTask;
2016-08-30 14:43:54 +00:00
var _ = Task.Run(async () =>
{
try
2016-08-30 14:43:54 +00:00
{
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
2016-08-30 14:43:54 +00:00
{
try
{
await guild.Owner.SendErrorAsync(
GetTextStatic("vt_exit",
NadekoBot.Localization.GetCultureInfo(guild),
typeof(Administration).Name.ToLowerInvariant(),
Format.Bold(guild.Name))).ConfigureAwait(false);
}
catch
{
// ignored
}
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(guild.Id, set => set).VoicePlusTextEnabled = false;
_voicePlusTextCache.TryRemove(guild.Id);
await uow.CompleteAsync().ConfigureAwait(false);
}
return;
2016-08-30 14:43:54 +00:00
}
var semaphore = _guildLockObjects.GetOrAdd(guild.Id, (key) => new SemaphoreSlim(1, 1));
try
2016-08-30 14:43:54 +00:00
{
await semaphore.WaitAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
var beforeVch = before.VoiceChannel;
if (beforeVch != null)
{
var beforeRoleName = GetRoleName(beforeVch);
var beforeRole = guild.Roles.FirstOrDefault(x => x.Name == beforeRoleName);
if (beforeRole != null)
try
{
_log.Info("Removing role " + beforeRoleName + " from user " + user.Username);
2017-04-15 00:54:19 +00:00
await user.RemoveRoleAsync(beforeRole).ConfigureAwait(false);
await Task.Delay(200).ConfigureAwait(false);
}
catch (Exception ex)
{
_log.Warn(ex);
}
}
var afterVch = after.VoiceChannel;
if (afterVch != null && guild.AFKChannel?.Id != afterVch.Id)
{
var roleName = GetRoleName(afterVch);
var roleToAdd = guild.Roles.FirstOrDefault(x => x.Name == roleName) ??
(IRole) await guild.CreateRoleAsync(roleName, GuildPermissions.None).ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
ITextChannel textChannel = guild.TextChannels
.FirstOrDefault(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant());
if (textChannel == null)
{
var created = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false));
2017-04-15 00:54:19 +00:00
try { await guild.CurrentUser.AddRoleAsync(roleToAdd).ConfigureAwait(false); } catch {/*ignored*/}
await Task.Delay(50).ConfigureAwait(false);
await created.AddPermissionOverwriteAsync(roleToAdd, new OverwritePermissions(
readMessages: PermValue.Allow,
sendMessages: PermValue.Allow))
.ConfigureAwait(false);
await Task.Delay(50).ConfigureAwait(false);
await created.AddPermissionOverwriteAsync(guild.EveryoneRole, new OverwritePermissions(
readMessages: PermValue.Deny,
sendMessages: PermValue.Deny))
.ConfigureAwait(false);
await Task.Delay(50).ConfigureAwait(false);
}
_log.Warn("Adding role " + roleToAdd.Name + " to user " + user.Username);
2017-04-15 00:54:19 +00:00
await user.AddRoleAsync(roleToAdd).ConfigureAwait(false);
}
}
finally
{
semaphore.Release();
}
2016-08-30 14:43:54 +00:00
}
catch (Exception ex)
{
_log.Warn(ex);
}
});
return Task.CompletedTask;
2016-08-30 14:43:54 +00:00
}
2016-12-08 17:35:34 +00:00
private static string GetChannelName(string voiceName) =>
_channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice";
2016-08-30 14:43:54 +00:00
private static string GetRoleName(IVoiceChannel ch) =>
"nvoice-" + ch.Id;
[NadekoCommand, Usage, Description, Aliases]
2016-08-30 14:43:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireUserPermission(GuildPermission.ManageChannels)]
public async Task VoicePlusText()
2016-08-30 14:43:54 +00:00
{
2016-12-16 21:44:26 +00:00
var guild = Context.Guild;
2016-08-30 14:43:54 +00:00
2016-10-28 10:44:40 +00:00
var botUser = await guild.GetCurrentUserAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
if (!botUser.GuildPermissions.ManageRoles || !botUser.GuildPermissions.ManageChannels)
{
2017-03-18 08:33:13 +00:00
await ReplyErrorLocalized("vt_perms").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
2016-10-25 11:10:05 +00:00
if (!botUser.GuildPermissions.Administrator)
{
try
{
await ReplyErrorLocalized("vt_no_admin").ConfigureAwait(false);
}
catch
{
// ignored
2016-10-25 11:10:05 +00:00
}
}
2016-08-30 14:43:54 +00:00
try
{
bool isEnabled;
using (var uow = DbHandler.UnitOfWork())
{
var conf = uow.GuildConfigs.For(guild.Id, set => set);
2016-08-30 14:43:54 +00:00
isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled;
await uow.CompleteAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
2016-10-20 03:09:05 +00:00
if (!isEnabled)
2016-08-30 14:43:54 +00:00
{
_voicePlusTextCache.TryRemove(guild.Id);
2016-12-17 00:16:14 +00:00
foreach (var textChannel in (await guild.GetTextChannelsAsync().ConfigureAwait(false)).Where(c => c.Name.EndsWith("-voice")))
2016-08-30 14:43:54 +00:00
{
try { await textChannel.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
}
foreach (var role in guild.Roles.Where(c => c.Name.StartsWith("nvoice-")))
{
try { await role.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
await ReplyConfirmLocalized("vt_disabled").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
_voicePlusTextCache.Add(guild.Id);
await ReplyConfirmLocalized("vt_enabled").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
catch (Exception ex)
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync(ex.ToString()).ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-30 14:43:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
//[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task CleanVPlusT()
2016-08-30 14:43:54 +00:00
{
2016-12-16 21:44:26 +00:00
var guild = Context.Guild;
2016-10-28 10:44:40 +00:00
var botUser = await guild.GetCurrentUserAsync().ConfigureAwait(false);
if (!botUser.GuildPermissions.Administrator)
2016-08-30 14:43:54 +00:00
{
await ReplyErrorLocalized("need_admin").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
var textChannels = await guild.GetTextChannelsAsync().ConfigureAwait(false);
var voiceChannels = await guild.GetVoiceChannelsAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
var boundTextChannels = textChannels.Where(c => c.Name.EndsWith("-voice"));
var validTxtChannelNames = new HashSet<string>(voiceChannels.Select(c => GetChannelName(c.Name).ToLowerInvariant()));
var invalidTxtChannels = boundTextChannels.Where(c => !validTxtChannelNames.Contains(c.Name));
2016-08-30 14:43:54 +00:00
foreach (var c in invalidTxtChannels)
{
try { await c.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
}
var boundRoles = guild.Roles.Where(r => r.Name.StartsWith("nvoice-"));
var validRoleNames = new HashSet<string>(voiceChannels.Select(c => GetRoleName(c).ToLowerInvariant()));
var invalidRoles = boundRoles.Where(r => !validRoleNames.Contains(r.Name));
foreach (var r in invalidRoles)
{
try { await r.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
await ReplyConfirmLocalized("cleaned_up").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
}
}
}