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;
|
2016-08-31 04:58:55 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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;
|
2017-02-02 20:00:38 +00:00
|
|
|
|
using System.Threading;
|
2016-08-30 14:43:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Administration
|
|
|
|
|
{
|
|
|
|
|
public partial class Administration
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-02-14 14:41:23 +00:00
|
|
|
|
public class VoicePlusTextCommands : NadekoSubmodule
|
2016-08-30 14:43:54 +00:00
|
|
|
|
{
|
2017-02-14 14:41:23 +00:00
|
|
|
|
private new static readonly Logger _log;
|
2016-12-28 05:31:39 +00:00
|
|
|
|
|
2017-02-14 14:41:23 +00:00
|
|
|
|
private static readonly Regex _channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
|
2017-02-02 20:00:38 +00:00
|
|
|
|
|
2017-02-14 14:41:23 +00:00
|
|
|
|
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
|
|
|
|
{
|
2017-02-14 14:41:23 +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
|
|
|
|
|
2017-02-14 14:41:23 +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
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 20:00:38 +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)
|
2017-02-02 20:00:38 +00:00
|
|
|
|
return Task.CompletedTask;
|
2016-12-28 05:31:39 +00:00
|
|
|
|
|
2017-02-02 20:00:38 +00:00
|
|
|
|
var botUserPerms = guild.CurrentUser.GuildPermissions;
|
2016-12-28 05:31:39 +00:00
|
|
|
|
|
2017-02-02 20:00:38 +00:00
|
|
|
|
if (before.VoiceChannel == after.VoiceChannel)
|
|
|
|
|
return Task.CompletedTask;
|
2016-08-30 14:43:54 +00:00
|
|
|
|
|
2017-02-14 14:41:23 +00:00
|
|
|
|
if (!_voicePlusTextCache.Contains(guild.Id))
|
2017-02-02 20:00:38 +00:00
|
|
|
|
return Task.CompletedTask;
|
2016-08-30 14:43:54 +00:00
|
|
|
|
|
2017-02-02 20:00:38 +00:00
|
|
|
|
var _ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
2016-08-30 14:43:54 +00:00
|
|
|
|
{
|
2017-02-02 20:00:38 +00:00
|
|
|
|
|
|
|
|
|
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
|
2016-08-30 14:43:54 +00:00
|
|
|
|
{
|
2017-02-02 20:00:38 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await guild.Owner.SendErrorAsync(
|
2017-02-14 21:48:53 +00:00
|
|
|
|
GetTextStatic("vt_exit",
|
|
|
|
|
NadekoBot.Localization.GetCultureInfo(guild),
|
|
|
|
|
typeof(Administration).Name.ToLowerInvariant(),
|
|
|
|
|
Format.Bold(guild.Name))).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
2017-02-02 20:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
|
|
|
|
uow.GuildConfigs.For(guild.Id, set => set).VoicePlusTextEnabled = false;
|
2017-02-14 14:41:23 +00:00
|
|
|
|
_voicePlusTextCache.TryRemove(guild.Id);
|
2017-02-02 20:00:38 +00:00
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2016-08-30 14:43:54 +00:00
|
|
|
|
}
|
2017-02-02 20:00:38 +00:00
|
|
|
|
|
2017-02-14 14:41:23 +00:00
|
|
|
|
var semaphore = _guildLockObjects.GetOrAdd(guild.Id, (key) => new SemaphoreSlim(1, 1));
|
2017-02-02 20:00:38 +00:00
|
|
|
|
|
|
|
|
|
try
|
2016-08-30 14:43:54 +00:00
|
|
|
|
{
|
2017-02-02 20:00:38 +00:00
|
|
|
|
await semaphore.WaitAsync().ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
|
2017-02-02 20:00:38 +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
|
|
|
|
|
{
|
2017-02-02 21:05:53 +00:00
|
|
|
|
_log.Info("Removing role " + beforeRoleName + " from user " + user.Username);
|
2017-04-15 00:54:19 +00:00
|
|
|
|
await user.RemoveRoleAsync(beforeRole).ConfigureAwait(false);
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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);
|
2017-02-14 21:48:53 +00:00
|
|
|
|
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
|
|
|
|
|
2017-02-02 20:00:38 +00:00
|
|
|
|
ITextChannel textChannel = guild.TextChannels
|
2017-02-14 14:41:23 +00:00
|
|
|
|
.FirstOrDefault(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant());
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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*/}
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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);
|
2017-02-02 20:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
2016-12-31 12:21:18 +00:00
|
|
|
|
{
|
2017-02-02 20:00:38 +00:00
|
|
|
|
semaphore.Release();
|
2016-12-31 12:21:18 +00:00
|
|
|
|
}
|
2016-08-30 14:43:54 +00:00
|
|
|
|
}
|
2017-02-02 20:00:38 +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) =>
|
2017-02-14 14:41:23 +00:00
|
|
|
|
_channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice";
|
2016-08-30 14:43:54 +00:00
|
|
|
|
|
2017-02-02 20:00:38 +00:00
|
|
|
|
private static string GetRoleName(IVoiceChannel ch) =>
|
|
|
|
|
"nvoice-" + ch.Id;
|
|
|
|
|
|
2016-10-05 03:09:44 +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.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
|
|
|
|
|
{
|
2017-02-14 21:48:53 +00:00
|
|
|
|
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())
|
|
|
|
|
{
|
2016-12-03 13:14:37 +00:00
|
|
|
|
var conf = uow.GuildConfigs.For(guild.Id, set => set);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled;
|
2016-10-20 02:19:01 +00:00
|
|
|
|
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
|
|
|
|
{
|
2017-02-14 14:41:23 +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 { }
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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
|
|
|
|
}
|
2017-02-14 21:48:53 +00:00
|
|
|
|
await ReplyConfirmLocalized("vt_disabled").ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-02-14 14:41:23 +00:00
|
|
|
|
_voicePlusTextCache.Add(guild.Id);
|
2017-02-14 21:48:53 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-05 03:09:44 +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)]
|
2017-02-02 20:00:38 +00:00
|
|
|
|
[RequireBotPermission(GuildPermission.ManageChannels)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
2017-02-02 20:00:38 +00:00
|
|
|
|
//[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
|
|
|
|
{
|
2017-02-14 21:48:53 +00:00
|
|
|
|
await ReplyErrorLocalized("need_admin").ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 20:00:38 +00:00
|
|
|
|
var textChannels = await guild.GetTextChannelsAsync().ConfigureAwait(false);
|
|
|
|
|
var voiceChannels = await guild.GetVoiceChannelsAsync().ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
|
2017-02-02 20:00:38 +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 { }
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 21:48:53 +00:00
|
|
|
|
await ReplyConfirmLocalized("cleaned_up").ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 05:31:39 +00:00
|
|
|
|
}
|