2016-03-29 15:20:18 +02:00
using Discord ;
2016-03-09 23:58:54 +01:00
using Discord.Commands ;
2016-03-10 17:46:58 +01:00
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 ;
2016-03-09 23:58:54 +01:00
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 )
{
2016-03-10 17:46:58 +01:00
// changing servers may cause bugs
2016-03-29 15:20:18 +02:00
NadekoBot . Client . UserUpdated + = async ( sender , e ) = >
{
try
{
2016-03-19 20:07:51 +01:00
if ( e . Server = = null )
return ;
2016-03-10 17:46:58 +01:00
var config = SpecificConfigurations . Default . Of ( e . Server . Id ) ;
2016-03-09 23:58:54 +01:00
if ( e . Before . VoiceChannel = = e . After . VoiceChannel ) return ;
2016-03-14 14:13:21 +01:00
if ( ! config . VoicePlusTextEnabled )
2016-03-10 17:46:58 +01:00
return ;
2016-03-19 20:07:51 +01:00
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-19 20:07:51 +01:00
2016-03-29 15:20:18 +02:00
try
{
2016-03-19 20:07:51 +01:00
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
2016-03-19 20:07:51 +01:00
config . VoicePlusTextEnabled = false ;
return ;
}
2016-03-09 23:58:54 +01:00
var beforeVch = e . Before . VoiceChannel ;
2016-03-29 15:20:18 +02:00
if ( beforeVch ! = null )
{
2016-03-09 23:58:54 +01:00
var textChannel =
2016-03-14 01:09:04 +01:00
e . Server . FindChannels ( GetChannelName ( beforeVch . Name ) , ChannelType . Text ) . FirstOrDefault ( ) ;
2016-03-10 17:46:58 +01:00
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 ) ;
2016-03-09 23:58:54 +01:00
}
var afterVch = e . After . VoiceChannel ;
2016-07-11 11:50:38 +02:00
if ( afterVch ! = null & & e . Server . AFKChannel ! = afterVch )
2016-03-29 15:20:18 +02:00
{
2016-03-10 10:06:36 +01:00
var textChannel = e . Server . FindChannels (
2016-03-14 01:09:04 +01:00
GetChannelName ( afterVch . Name ) ,
2016-03-10 10:06:36 +01:00
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 ) ) ;
2016-03-10 10:06:36 +01:00
await textChannel . AddPermissionsRule ( e . Server . EveryoneRole ,
new ChPermOverride ( readMessages : PermValue . Deny ,
2016-04-18 23:38:19 +02:00
sendMessages : PermValue . Deny ) ) . ConfigureAwait ( false ) ;
2016-03-10 10:06:36 +01:00
}
2016-03-09 23:58:54 +01:00
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-09 23:58:54 +01:00
}
2016-03-29 15:20:18 +02:00
}
catch ( Exception ex )
{
2016-03-09 23:58:54 +01:00
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 )
{
2016-05-24 23:58:13 +02:00
cgb . CreateCommand ( Module . Prefix + "cleanv+t" )
2016-06-18 00:02:03 +02:00
. 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`" )
2016-05-24 23:58:13 +02:00
. 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 ;
}
2016-05-24 23:58:13 +02:00
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 ) ;
}
2016-05-24 23:58:13 +02:00
await e . Channel . SendMessage ( "`Done.`" ) ;
} ) ;
2016-06-18 00:02:03 +02:00
cgb . CreateCommand ( Module . Prefix + "voice+text" )
. Alias ( Module . Prefix + "v+t" )
2016-03-10 17:46:58 +01:00
. 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`" )
2016-03-09 23:58:54 +01:00
. AddCheck ( SimpleCheckers . ManageChannels ( ) )
. AddCheck ( SimpleCheckers . CanManageRoles )
2016-03-29 15:20:18 +02:00
. Do ( async e = >
{
try
{
2016-03-10 17:46:58 +01:00
var config = SpecificConfigurations . Default . Of ( e . Server . Id ) ;
2016-03-29 15:20:18 +02:00
if ( config . VoicePlusTextEnabled = = true )
{
2016-03-10 17:46:58 +01:00
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 ) ;
2016-03-10 17:46:58 +01:00
return ;
}
2016-03-09 23:58:54 +01:00
}
2016-04-18 23:38:19 +02:00
await e . Channel . SendMessage ( "Successfuly removed voice + text feature." ) . ConfigureAwait ( false ) ;
2016-03-10 17:46:58 +01:00
return ;
2016-03-09 23:58:54 +01:00
}
2016-03-10 17:46:58 +01:00
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-10 17:46:58 +01:00
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 ) ;
2016-03-09 23:58:54 +01:00
}
} ) ;
}
}
}