165 lines
7.8 KiB
C#
Raw Normal View History

2016-03-29 15:20:18 +02:00
using Discord;
using Discord.Commands;
using NadekoBot.Classes;
2016-05-03 06:28:55 +02:00
using NadekoBot.Extensions;
2016-04-15 00:17:29 +02:00
using NadekoBot.Modules.Permissions.Classes;
2016-03-29 15:20:18 +02:00
using System;
using System.Linq;
2016-05-03 06:28:55 +02:00
using System.Text.RegularExpressions;
2016-05-28 14:25:09 +02:00
using System.Threading.Tasks;
using ChPermOverride = Discord.ChannelPermissionOverrides;
2016-03-29 15:20:18 +02:00
namespace NadekoBot.Modules.Administration.Commands
{
internal class VoicePlusTextCommand : DiscordCommand
{
2016-05-03 06:28:55 +02:00
Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
2016-03-29 15:20:18 +02:00
public VoicePlusTextCommand(DiscordModule module) : base(module)
{
// changing servers may cause bugs
2016-03-29 15:20:18 +02:00
NadekoBot.Client.UserUpdated += async (sender, e) =>
{
try
{
if (e.Server == null)
return;
var config = SpecificConfigurations.Default.Of(e.Server.Id);
if (e.Before.VoiceChannel == e.After.VoiceChannel) return;
if (!config.VoicePlusTextEnabled)
return;
var serverPerms = e.Server.GetUser(NadekoBot.Client.CurrentUser.Id)?.ServerPermissions;
if (serverPerms == null)
return;
2016-03-29 15:20:18 +02:00
if (!serverPerms.Value.ManageChannels || !serverPerms.Value.ManageRoles)
{
2016-03-29 15:20:18 +02:00
try
{
await e.Server.Owner.SendMessage(
"I don't have manage server and/or Manage Channels permission," +
2016-04-18 23:38:19 +02:00
$" so I cannot run voice+text on **{e.Server.Name}** server.").ConfigureAwait(false);
2016-03-29 15:20:18 +02:00
}
catch { } // meh
config.VoicePlusTextEnabled = false;
return;
}
var beforeVch = e.Before.VoiceChannel;
2016-03-29 15:20:18 +02:00
if (beforeVch != null)
{
var textChannel =
2016-03-14 01:09:04 +01:00
e.Server.FindChannels(GetChannelName(beforeVch.Name), ChannelType.Text).FirstOrDefault();
if (textChannel != null)
await textChannel.AddPermissionsRule(e.Before,
new ChPermOverride(readMessages: PermValue.Deny,
2016-04-18 23:38:19 +02:00
sendMessages: PermValue.Deny)).ConfigureAwait(false);
}
var afterVch = e.After.VoiceChannel;
if (afterVch != null && e.Server.AFKChannel != afterVch)
2016-03-29 15:20:18 +02:00
{
var textChannel = e.Server.FindChannels(
2016-03-14 01:09:04 +01:00
GetChannelName(afterVch.Name),
ChannelType.Text)
.FirstOrDefault();
2016-03-29 15:20:18 +02:00
if (textChannel == null)
{
2016-04-18 23:38:19 +02:00
textChannel = (await e.Server.CreateChannel(GetChannelName(afterVch.Name), ChannelType.Text).ConfigureAwait(false));
await textChannel.AddPermissionsRule(e.Server.EveryoneRole,
new ChPermOverride(readMessages: PermValue.Deny,
2016-04-18 23:38:19 +02:00
sendMessages: PermValue.Deny)).ConfigureAwait(false);
}
await textChannel.AddPermissionsRule(e.After,
new ChPermOverride(readMessages: PermValue.Allow,
2016-04-18 23:38:19 +02:00
sendMessages: PermValue.Allow)).ConfigureAwait(false);
}
2016-03-29 15:20:18 +02:00
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
};
}
2016-03-14 01:09:04 +01:00
private string GetChannelName(string voiceName) =>
2016-05-03 06:28:55 +02:00
channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice";
2016-03-14 01:09:04 +01:00
2016-03-29 15:20:18 +02:00
internal override void Init(CommandGroupBuilder cgb)
{
cgb.CreateCommand(Module.Prefix + "cleanv+t")
.Alias(Module.Prefix + "cv+t")
2016-08-03 00:06:08 +10:00
.Description($"Deletes all text channels ending in `-voice` for which voicechannels are not found. **Use at your own risk.\nNeeds Manage Roles and Manage Channels Permissions.** | `{Prefix}cleanv+t`")
.AddCheck(SimpleCheckers.CanManageRoles)
.AddCheck(SimpleCheckers.ManageChannels())
.Do(async e =>
{
2016-05-28 14:25:09 +02:00
if (!e.Server.CurrentUser.ServerPermissions.ManageChannels)
{
await e.Channel.SendMessage("`I have insufficient permission to do that.`");
return;
}
var allTxtChannels = e.Server.TextChannels.Where(c => c.Name.EndsWith("-voice"));
var validTxtChannelNames = e.Server.VoiceChannels.Select(c => GetChannelName(c.Name));
var invalidTxtChannels = allTxtChannels.Where(c => !validTxtChannelNames.Contains(c.Name));
2016-05-28 14:25:09 +02:00
foreach (var c in invalidTxtChannels)
{
try
{
await c.Delete();
}
catch { }
await Task.Delay(500);
}
await e.Channel.SendMessage("`Done.`");
});
cgb.CreateCommand(Module.Prefix + "voice+text")
.Alias(Module.Prefix + "v+t")
.Description("Creates a text channel for each voice channel only users in that voice channel can see." +
2016-08-03 00:06:08 +10:00
$"If you are server owner, keep in mind you will see them all the time regardless. **Needs Manage Roles and Manage Channels Permissions.**| `{Prefix}voice+text`")
.AddCheck(SimpleCheckers.ManageChannels())
.AddCheck(SimpleCheckers.CanManageRoles)
2016-03-29 15:20:18 +02:00
.Do(async e =>
{
try
{
var config = SpecificConfigurations.Default.Of(e.Server.Id);
2016-03-29 15:20:18 +02:00
if (config.VoicePlusTextEnabled == true)
{
config.VoicePlusTextEnabled = false;
2016-03-29 15:20:18 +02:00
foreach (var textChannel in e.Server.TextChannels.Where(c => c.Name.EndsWith("-voice")))
{
try
{
2016-04-18 23:38:19 +02:00
await textChannel.Delete().ConfigureAwait(false);
2016-03-29 15:20:18 +02:00
}
catch
{
2016-04-18 23:38:19 +02:00
await e.Channel.SendMessage(
":anger: Error: Most likely i don't have permissions to do this.")
.ConfigureAwait(false);
return;
}
}
2016-04-18 23:38:19 +02:00
await e.Channel.SendMessage("Successfuly removed voice + text feature.").ConfigureAwait(false);
return;
}
config.VoicePlusTextEnabled = true;
await e.Channel.SendMessage("Successfuly enabled voice + text feature. " +
2016-04-18 23:38:19 +02:00
"**Make sure the bot has manage roles and manage channels permissions**")
.ConfigureAwait(false);
2016-03-29 15:20:18 +02:00
}
catch (Exception ex)
{
2016-04-18 23:38:19 +02:00
await e.Channel.SendMessage(ex.ToString()).ConfigureAwait(false);
}
});
}
}
}