Almost done v+t

This commit is contained in:
Kwoth 2016-08-30 16:43:54 +02:00
parent 465b598286
commit a081f1c89c
3 changed files with 168 additions and 167 deletions

View File

@ -1,166 +0,0 @@
//using Discord;
//using Discord.Commands;
//using NadekoBot.Classes;
//using NadekoBot.Extensions;
//using NadekoBot.Modules.Permissions.Classes;
//using System;
//using System.Linq;
//using System.Text.RegularExpressions;
//using System.Threading.Tasks;
//using ChPermOverride = Discord.ChannelPermissionOverrides;
////todo DB
////todo rewrite
//namespace NadekoBot.Modules.Administration
//{
// internal class VoicePlusTextCommand : DiscordCommand
// {
// Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
// public VoicePlusTextCommand(DiscordModule module) : base(module)
// {
// // changing servers may cause bugs
// 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;
// if (!serverPerms.Value.ManageChannels || !serverPerms.Value.ManageRoles)
// {
// try
// {
// await e.Server.Owner.SendMessageAsync(
// "I don't have manage server and/or Manage Channels permission," +
// $" so I cannot run voice+text on **{e.Server.Name}** server.").ConfigureAwait(false);
// }
// catch { } // meh
// config.VoicePlusTextEnabled = false;
// return;
// }
// var beforeVch = e.Before.VoiceChannel;
// if (beforeVch != null)
// {
// var textChannel =
// e.Server.FindChannels(GetChannelName(beforeVch.Name), ChannelType.Text).FirstOrDefault();
// if (textChannel != null)
// await textChannel.AddPermissionsRule(e.Before,
// new ChPermOverride(readMessages: PermValue.Deny,
// sendMessages: PermValue.Deny)).ConfigureAwait(false);
// }
// var afterVch = e.After.VoiceChannel;
// if (afterVch != null && e.Server.AFKChannel != afterVch)
// {
// var textChannel = e.Server.FindChannels(
// GetChannelName(afterVch.Name),
// ChannelType.Text)
// .FirstOrDefault();
// if (textChannel == null)
// {
// textChannel = (await e.Server.CreateChannel(GetChannelName(afterVch.Name), ChannelType.Text).ConfigureAwait(false));
// await textChannel.AddPermissionsRule(e.Server.EveryoneRole,
// new ChPermOverride(readMessages: PermValue.Deny,
// sendMessages: PermValue.Deny)).ConfigureAwait(false);
// }
// await textChannel.AddPermissionsRule(e.After,
// new ChPermOverride(readMessages: PermValue.Allow,
// sendMessages: PermValue.Allow)).ConfigureAwait(false);
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex);
// }
// };
// }
// private string GetChannelName(string voiceName) =>
// channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice";
// internal override void Init(CommandGroupBuilder cgb)
// {
// cgb.CreateCommand(Module.Prefix + "cleanv+t")
// .Alias(Module.Prefix + "cv+t")
// .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 =>
// {
// if (!e.Server.CurrentUser.ServerPermissions.ManageChannels)
// {
// await channel.SendMessageAsync("`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));
// foreach (var c in invalidTxtChannels)
// {
// try
// {
// await c.Delete();
// }
// catch { }
// await Task.Delay(500);
// }
// await channel.SendMessageAsync("`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." +
// $"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)
// .Do(async e =>
// {
// try
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// if (config.VoicePlusTextEnabled == true)
// {
// config.VoicePlusTextEnabled = false;
// foreach (var textChannel in e.Server.TextChannels.Where(c => c.Name.EndsWith("-voice")))
// {
// try
// {
// await textChannel.Delete().ConfigureAwait(false);
// }
// catch
// {
// await channel.SendMessageAsync(
// ":anger: Error: Most likely i don't have permissions to do this.")
// .ConfigureAwait(false);
// return;
// }
// }
// await channel.SendMessageAsync("Successfuly removed voice + text feature.").ConfigureAwait(false);
// return;
// }
// config.VoicePlusTextEnabled = true;
// await channel.SendMessageAsync("Successfuly enabled voice + text feature. " +
// "**Make sure the bot has manage roles and manage channels permissions**")
// .ConfigureAwait(false);
// }
// catch (Exception ex)
// {
// await channel.SendMessageAsync(ex.ToString()).ConfigureAwait(false);
// }
// });
// }
// }
//}

View File

@ -0,0 +1,166 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
public class VoicePlusTextCommands
{
Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
public VoicePlusTextCommands()
{
// changing servers may cause bugs
NadekoBot.Client.UserUpdated += UserUpdatedEventHandler;
}
private Task UserUpdatedEventHandler(IGuildUser before, IGuildUser after)
{
Task.Run(async () =>
{
var guild = before.Guild ?? after.Guild;
var botUserPerms = guild.GetCurrentUser().GuildPermissions;
try
{
if (before.VoiceChannel == after.VoiceChannel) return;
//todo This is WAY TOO MUCH
using (var uow = DbHandler.UnitOfWork())
{
if (!uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled)
return;
}
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
{
try
{
await (await guild.GetOwnerAsync()).SendMessageAsync(
"I don't have manage server and/or Manage Channels permission," +
$" so I cannot run voice+text on **{guild.Name}** server.").ConfigureAwait(false);
}
catch { }
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled = false;
}
return;
}
var beforeVch = before.VoiceChannel;
if (beforeVch != null)
{
var textChannel = guild.GetTextChannels().Where(t => t.Name == GetChannelName(beforeVch.Name)).FirstOrDefault();
if (textChannel != null)
await textChannel.AddPermissionOverwriteAsync(before,
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))
.FirstOrDefault();
if (textChannel == null)
{
textChannel = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name)).ConfigureAwait(false));
await textChannel.AddPermissionOverwriteAsync(guild.EveryoneRole,
new OverwritePermissions(readMessages: PermValue.Deny,
sendMessages: PermValue.Deny)).ConfigureAwait(false);
}
await textChannel.AddPermissionOverwriteAsync(after,
new OverwritePermissions(readMessages: PermValue.Allow,
sendMessages: PermValue.Allow)).ConfigureAwait(false);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
});
return Task.CompletedTask;
}
private string GetChannelName(string voiceName) =>
channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice";
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageRoles)]
[RequirePermission(GuildPermission.ManageChannels)]
public async Task VPlusT(IUserMessage msg, [Remainder] string arg)
{
var channel = (ITextChannel)msg.Channel;
var guild = channel.Guild;
var botUser = guild.GetCurrentUser();
if (!botUser.GuildPermissions.ManageRoles || !botUser.GuildPermissions.ManageChannels)
{
await channel.SendMessageAsync(":anger: `I require manage roles and manage channels permissions to enable this feature.`");
return;
}
try
{
bool isEnabled;
using (var uow = DbHandler.UnitOfWork())
{
var conf = uow.GuildConfigs.For(guild.Id);
isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled;
}
if (isEnabled)
{
foreach (var textChannel in guild.GetTextChannels().Where(c => c.Name.EndsWith("-voice")))
{
try { await textChannel.DeleteAsync().ConfigureAwait(false); } catch { }
}
await channel.SendMessageAsync("Successfuly removed voice + text feature.").ConfigureAwait(false);
return;
}
await channel.SendMessageAsync("Successfuly enabled voice + text feature.").ConfigureAwait(false);
}
catch (Exception ex)
{
await channel.SendMessageAsync(ex.ToString()).ConfigureAwait(false);
}
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageChannels)]
[RequirePermission(GuildPermission.ManageRoles)]
public async Task CleanVPlusT(IUserMessage msg, [Remainder] string arg)
{
var channel = (ITextChannel)msg.Channel;
var guild = channel.Guild;
if (!guild.GetCurrentUser().GuildPermissions.ManageChannels)
{
await channel.SendMessageAsync("`I have insufficient permission to do that.`");
return;
}
var allTxtChannels = guild.GetTextChannels().Where(c => c.Name.EndsWith("-voice"));
var validTxtChannelNames = guild.GetVoiceChannels().Select(c => GetChannelName(c.Name));
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);
}
await channel.SendMessageAsync("`Done.`");
}
}
}
}

View File

@ -31,6 +31,7 @@ namespace NadekoBot.Services.Database.Models
//self assignable roles
public bool ExclusiveSelfAssignedRoles { get; set; }
public bool AutoDeleteSelfAssignedRoleMessages { get; set; }
public float DefaultMusicVolume { get; set; }
public float DefaultMusicVolume { get; set; } = 1.0f;
public bool VoicePlusTextEnabled { get; set; }
}
}