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

191 lines
9.1 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;
using System;
using System.Collections.Concurrent;
2016-08-30 14:43:54 +00:00
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
public class VoicePlusTextCommands
{
2016-12-08 17:35:34 +00:00
private static Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
2016-10-20 03:35:00 +00:00
2016-12-08 17:35:34 +00:00
private static ConcurrentHashSet<ulong> voicePlusTextCache { get; }
static VoicePlusTextCommands()
2016-08-30 14:43:54 +00:00
{
2016-10-20 03:09:05 +00:00
using (var uow = DbHandler.UnitOfWork())
{
2016-10-26 14:30:49 +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-08-30 14:43:54 +00:00
}
2016-12-08 17:35:34 +00:00
private static Task UserUpdatedEventHandler(IUser iuser, IVoiceState before, IVoiceState after)
2016-08-30 14:43:54 +00:00
{
2016-10-20 03:09:05 +00:00
var user = (iuser as IGuildUser);
var guild = user?.Guild;
if (guild == null)
return Task.CompletedTask;
2016-09-04 15:23:58 +00:00
var task = Task.Run(async () =>
2016-08-30 14:43:54 +00:00
{
try
{
var botUserPerms = guild.GetCurrentUser().GuildPermissions;
2016-08-30 14:43:54 +00:00
if (before.VoiceChannel == after.VoiceChannel) return;
2016-10-20 03:09:05 +00:00
if (!voicePlusTextCache.Contains(guild.Id))
return;
2016-08-30 14:43:54 +00:00
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
{
try
{
2016-12-11 16:18:25 +00:00
await (await guild.GetOwnerAsync()).SendErrorAsync(
"⚠️ I don't have **manage server** and/or **manage channels** permission," +
$" so I cannot run `voice+text` on **{guild.Name}** server.").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
catch { }
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(guild.Id, set => set).VoicePlusTextEnabled = false;
2016-10-20 03:09:05 +00:00
voicePlusTextCache.TryRemove(guild.Id);
2016-10-18 01:03:46 +00:00
await uow.CompleteAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
return;
}
var beforeVch = before.VoiceChannel;
if (beforeVch != null)
{
var textChannel = guild.GetTextChannels().Where(t => t.Name == GetChannelName(beforeVch.Name).ToLowerInvariant()).FirstOrDefault();
2016-08-30 14:43:54 +00:00
if (textChannel != null)
2016-10-20 03:09:05 +00:00
await textChannel.AddPermissionOverwriteAsync(user,
2016-08-30 14:43:54 +00:00
new OverwritePermissions(readMessages: PermValue.Deny,
sendMessages: PermValue.Deny)).ConfigureAwait(false);
}
var afterVch = after.VoiceChannel;
if (afterVch != null && guild.AFKChannelId != afterVch.Id)
{
var textChannel = guild.GetTextChannels()
.Where(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant())
2016-08-30 14:43:54 +00:00
.FirstOrDefault();
if (textChannel == null)
{
textChannel = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false));
2016-08-30 14:43:54 +00:00
await textChannel.AddPermissionOverwriteAsync(guild.EveryoneRole,
new OverwritePermissions(readMessages: PermValue.Deny,
sendMessages: PermValue.Deny)).ConfigureAwait(false);
}
2016-10-20 03:09:05 +00:00
await textChannel.AddPermissionOverwriteAsync(user,
2016-08-30 14:43:54 +00:00
new OverwritePermissions(readMessages: PermValue.Allow,
sendMessages: PermValue.Allow)).ConfigureAwait(false);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
});
return Task.CompletedTask;
}
2016-12-08 17:35:34 +00:00
private static string GetChannelName(string voiceName) =>
2016-08-30 14:43:54 +00:00
channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice";
[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 channel = (ITextChannel)Context.Channel;
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)
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync("I require atleast **manage roles** and **manage channels permissions** to enable this feature. `(preffered Administration permission)`");
2016-08-30 14:43:54 +00:00
return;
}
2016-10-25 11:10:05 +00:00
if (!botUser.GuildPermissions.Administrator)
{
try
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync("⚠️ You are enabling this feature and **I do not have ADMINISTRATOR permissions**. " +
"`This may cause some issues, and you will have to clean up text channels yourself afterwards.`");
2016-10-25 11:10:05 +00:00
}
2016-10-25 11:10:55 +00:00
catch { }
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
{
2016-10-23 07:12:06 +00:00
voicePlusTextCache.TryRemove(guild.Id);
2016-08-30 14:43:54 +00:00
foreach (var textChannel in guild.GetTextChannels().Where(c => c.Name.EndsWith("-voice")))
{
try { await textChannel.DeleteAsync().ConfigureAwait(false); } catch { }
}
2016-12-16 21:44:26 +00:00
await Context.Channel.SendConfirmAsync(" Successfuly **removed** voice + text feature.").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
2016-10-23 07:12:06 +00:00
voicePlusTextCache.Add(guild.Id);
2016-12-16 21:44:26 +00:00
await Context.Channel.SendConfirmAsync("🆗 Successfuly **enabled** voice + text feature.").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)]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task CleanVPlusT()
2016-08-30 14:43:54 +00:00
{
2016-12-16 21:44:26 +00:00
//var channel = (ITextChannel)Context.Channel;
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
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync("I need **Administrator permission** to do that.").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
var allTxtChannels = guild.GetTextChannels().Where(c => c.Name.EndsWith("-voice"));
var validTxtChannelNames = guild.GetVoiceChannels().Select(c => GetChannelName(c.Name).ToLowerInvariant());
2016-08-30 14:43:54 +00:00
var invalidTxtChannels = allTxtChannels.Where(c => !validTxtChannelNames.Contains(c.Name));
foreach (var c in invalidTxtChannels)
{
try { await c.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500);
}
2016-12-16 21:44:26 +00:00
await Context.Channel.SendConfirmAsync("Cleaned v+t.").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
}
}
}