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 ;
2016-08-31 04:58:55 +00:00
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
{
2016-10-24 21:05:49 +00:00
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 ) )
2016-08-31 04:58:55 +00:00
return ;
2016-08-30 14:43:54 +00:00
if ( ! botUserPerms . ManageChannels | | ! botUserPerms . ManageRoles )
{
try
{
await ( await guild . GetOwnerAsync ( ) ) . SendMessageAsync (
2016-11-22 12:57:38 +00:00
"⚠️ 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 ( ) )
{
2016-12-03 13:14:37 +00:00
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 )
{
2016-10-25 11:06:37 +00:00
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 ( )
2016-10-25 11:06:37 +00:00
. Where ( t = > t . Name = = GetChannelName ( afterVch . Name ) . ToLowerInvariant ( ) )
2016-08-30 14:43:54 +00:00
. FirstOrDefault ( ) ;
if ( textChannel = = null )
{
2016-10-25 11:06:37 +00:00
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" ;
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-30 14:43:54 +00:00
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageRoles)]
[RequirePermission(GuildPermission.ManageChannels)]
2016-10-20 02:19:01 +00:00
public async Task VoicePlusText ( IUserMessage msg )
2016-08-30 14:43:54 +00:00
{
var channel = ( ITextChannel ) msg . Channel ;
var guild = channel . Guild ;
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-11-22 12:57:38 +00:00
await channel . SendMessageAsync ( "💢 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-11-22 12:57:38 +00:00
await channel . SendMessageAsync ( "⚠️ 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 ( ) )
{
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
{
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-11-22 12:57:38 +00:00
await channel . SendMessageAsync ( "ℹ ️ 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-11-22 12:57:38 +00:00
await channel . SendMessageAsync ( "🆗 Successfuly **enabled** voice + text feature." ) . ConfigureAwait ( false ) ;
2016-08-30 14:43:54 +00:00
}
catch ( Exception ex )
{
await channel . SendMessageAsync ( ex . ToString ( ) ) . ConfigureAwait ( false ) ;
}
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-30 14:43:54 +00:00
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageChannels)]
[RequirePermission(GuildPermission.ManageRoles)]
2016-10-20 02:19:01 +00:00
public async Task CleanVPlusT ( IUserMessage msg )
2016-08-30 14:43:54 +00:00
{
var channel = ( ITextChannel ) msg . Channel ;
var guild = channel . 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-11-22 12:57:38 +00:00
await channel . SendMessageAsync ( "⚠️ 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" ) ) ;
2016-10-25 11:06:37 +00:00
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-11-22 13:01:22 +00:00
await channel . SendMessageAsync ( "✅ Done." ) . ConfigureAwait ( false ) ;
2016-08-30 14:43:54 +00:00
}
}
}
2016-11-22 12:57:38 +00:00
}