2016-08-26 17:25:54 +00:00
using Discord ;
using Discord.Commands ;
2017-06-14 15:19:27 +00:00
using Discord.WebSocket ;
2016-12-11 16:18:25 +00:00
using NadekoBot.Extensions ;
2017-10-13 04:14:54 +00:00
using NadekoBot.Core.Services ;
using NadekoBot.Core.Services.Database.Models ;
2016-08-26 17:25:54 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes ;
using NadekoBot.Common.Collections ;
2016-08-30 01:11:25 +00:00
2016-08-26 17:25:54 +00:00
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
2017-02-14 14:41:23 +00:00
public class SelfAssignedRolesCommands : NadekoSubmodule
2016-08-26 17:25:54 +00:00
{
2017-05-29 04:13:22 +00:00
private readonly DbService _db ;
2017-05-27 17:42:23 +00:00
2017-05-29 04:13:22 +00:00
public SelfAssignedRolesCommands ( DbService db )
2017-05-27 17:42:23 +00:00
{
_db = db ;
}
2016-10-20 02:45:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task AdSarm ( )
2016-10-20 02:45:44 +00:00
{
bool newval ;
2017-05-27 17:42:23 +00:00
using ( var uow = _db . UnitOfWork )
2016-10-20 02:45:44 +00:00
{
2016-12-16 21:44:26 +00:00
var config = uow . GuildConfigs . For ( Context . Guild . Id , set = > set ) ;
2016-10-20 02:45:44 +00:00
newval = config . AutoDeleteSelfAssignedRoleMessages = ! config . AutoDeleteSelfAssignedRoleMessages ;
await uow . CompleteAsync ( ) . ConfigureAwait ( false ) ;
}
2016-12-16 21:44:26 +00:00
await Context . Channel . SendConfirmAsync ( $"ℹ ️ Automatic deleting of `iam` and `iamn` confirmations has been {(newval ? " * * enabled * * " : " * * disabled * * ")}." )
2016-10-20 02:45:44 +00:00
. ConfigureAwait ( false ) ;
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-26 17:25:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
2016-12-17 00:16:14 +00:00
public async Task Asar ( [ Remainder ] IRole role )
2016-08-26 17:25:54 +00:00
{
IEnumerable < SelfAssignedRole > roles ;
2017-04-03 21:18:18 +00:00
var guser = ( IGuildUser ) Context . User ;
if ( Context . User . Id ! = guser . Guild . OwnerId & & guser . GetRoles ( ) . Max ( x = > x . Position ) < = role . Position )
return ;
2016-08-26 17:25:54 +00:00
string msg ;
2017-02-14 20:47:46 +00:00
var error = false ;
2017-05-27 17:42:23 +00:00
using ( var uow = _db . UnitOfWork )
2016-08-26 17:25:54 +00:00
{
2016-12-16 21:44:26 +00:00
roles = uow . SelfAssignedRoles . GetFromGuild ( Context . Guild . Id ) ;
2016-12-17 00:16:14 +00:00
if ( roles . Any ( s = > s . RoleId = = role . Id & & s . GuildId = = role . Guild . Id ) )
2016-08-26 17:25:54 +00:00
{
2017-02-14 20:47:46 +00:00
msg = GetText ( "role_in_list" , Format . Bold ( role . Name ) ) ;
error = true ;
2016-08-26 17:25:54 +00:00
}
else
{
2017-02-14 20:47:46 +00:00
uow . SelfAssignedRoles . Add ( new SelfAssignedRole
{
2016-08-26 17:25:54 +00:00
RoleId = role . Id ,
2016-12-17 00:16:14 +00:00
GuildId = role . Guild . Id
2016-08-26 17:25:54 +00:00
} ) ;
await uow . CompleteAsync ( ) ;
2017-02-14 20:47:46 +00:00
msg = GetText ( "role_added" , Format . Bold ( role . Name ) ) ;
2016-08-26 17:25:54 +00:00
}
}
2017-02-14 20:47:46 +00:00
if ( error )
await Context . Channel . SendErrorAsync ( msg ) . ConfigureAwait ( false ) ;
else
await Context . Channel . SendConfirmAsync ( msg ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-26 17:25:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
2016-12-17 00:16:14 +00:00
public async Task Rsar ( [ Remainder ] IRole role )
2016-08-26 17:25:54 +00:00
{
2017-04-03 21:18:18 +00:00
var guser = ( IGuildUser ) Context . User ;
if ( Context . User . Id ! = guser . Guild . OwnerId & & guser . GetRoles ( ) . Max ( x = > x . Position ) < = role . Position )
return ;
2016-08-26 17:25:54 +00:00
bool success ;
2017-05-27 17:42:23 +00:00
using ( var uow = _db . UnitOfWork )
2016-08-26 17:25:54 +00:00
{
2016-12-17 00:16:14 +00:00
success = uow . SelfAssignedRoles . DeleteByGuildAndRoleId ( role . Guild . Id , role . Id ) ;
2016-08-26 17:25:54 +00:00
await uow . CompleteAsync ( ) ;
}
2016-08-28 00:46:36 +00:00
if ( ! success )
2016-08-26 17:25:54 +00:00
{
2017-02-14 20:47:46 +00:00
await ReplyErrorLocalized ( "self_assign_not" ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
return ;
}
2017-02-14 20:47:46 +00:00
await ReplyConfirmLocalized ( "self_assign_rem" , Format . Bold ( role . Name ) ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-26 17:25:54 +00:00
[RequireContext(ContextType.Guild)]
2017-06-14 15:19:27 +00:00
public async Task Lsar ( int page = 1 )
2016-08-26 17:25:54 +00:00
{
2017-06-14 15:19:27 +00:00
if ( - - page < 0 )
return ;
2016-10-10 04:12:22 +00:00
var toRemove = new ConcurrentHashSet < SelfAssignedRole > ( ) ;
2016-08-26 17:25:54 +00:00
var removeMsg = new StringBuilder ( ) ;
2017-06-13 01:09:01 +00:00
var roles = new List < string > ( ) ;
2016-12-11 16:18:25 +00:00
var roleCnt = 0 ;
2017-05-27 17:42:23 +00:00
using ( var uow = _db . UnitOfWork )
2016-08-26 17:25:54 +00:00
{
2016-12-16 21:44:26 +00:00
var roleModels = uow . SelfAssignedRoles . GetFromGuild ( Context . Guild . Id ) . ToList ( ) ;
2016-08-26 17:25:54 +00:00
foreach ( var roleModel in roleModels )
{
2016-12-16 21:44:26 +00:00
var role = Context . Guild . Roles . FirstOrDefault ( r = > r . Id = = roleModel . RoleId ) ;
2016-08-26 17:25:54 +00:00
if ( role = = null )
{
2017-05-16 20:50:30 +00:00
toRemove . Add ( roleModel ) ;
2016-08-26 17:25:54 +00:00
uow . SelfAssignedRoles . Remove ( roleModel ) ;
}
else
{
2017-06-13 01:09:01 +00:00
roles . Add ( Format . Bold ( role . Name ) ) ;
2017-05-16 20:50:30 +00:00
roleCnt + + ;
2016-08-26 17:25:54 +00:00
}
}
foreach ( var role in toRemove )
{
2017-06-14 15:19:27 +00:00
roles . Add ( GetText ( "role_clean" , role . RoleId ) ) ;
2016-08-26 17:25:54 +00:00
}
await uow . CompleteAsync ( ) ;
}
2017-06-14 15:19:27 +00:00
2017-06-19 13:42:10 +00:00
await Context . Channel . SendPaginatedConfirmAsync ( ( DiscordSocketClient ) Context . Client , page , ( curPage ) = >
2017-06-14 15:19:27 +00:00
{
return new EmbedBuilder ( )
. WithTitle ( GetText ( "self_assign_list" , roleCnt ) )
. WithDescription ( string . Join ( "\n" , roles . Skip ( curPage * 10 ) . Take ( 10 ) ) )
. WithOkColor ( ) ;
} , roles . Count / 10 ) ;
2016-08-26 17:25:54 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-26 17:25:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task Tesar ( )
2016-08-26 17:25:54 +00:00
{
bool areExclusive ;
2017-05-27 17:42:23 +00:00
using ( var uow = _db . UnitOfWork )
2016-08-26 17:25:54 +00:00
{
2016-12-16 21:44:26 +00:00
var config = uow . GuildConfigs . For ( Context . Guild . Id , set = > set ) ;
2016-08-26 17:25:54 +00:00
areExclusive = config . ExclusiveSelfAssignedRoles = ! config . ExclusiveSelfAssignedRoles ;
await uow . CompleteAsync ( ) ;
}
2017-02-14 20:47:46 +00:00
if ( areExclusive )
await ReplyConfirmLocalized ( "self_assign_excl" ) . ConfigureAwait ( false ) ;
else
await ReplyConfirmLocalized ( "self_assign_no_excl" ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-26 17:25:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Iam ( [ Remainder ] IRole role )
2016-08-26 17:25:54 +00:00
{
2016-12-16 18:43:57 +00:00
var guildUser = ( IGuildUser ) Context . User ;
2016-08-26 17:25:54 +00:00
GuildConfig conf ;
2017-03-09 03:00:25 +00:00
SelfAssignedRole [ ] roles ;
2017-05-27 17:42:23 +00:00
using ( var uow = _db . UnitOfWork )
2016-08-26 17:25:54 +00:00
{
2016-12-16 21:44:26 +00:00
conf = uow . GuildConfigs . For ( Context . Guild . Id , set = > set ) ;
2017-03-09 03:00:25 +00:00
roles = uow . SelfAssignedRoles . GetFromGuild ( Context . Guild . Id ) . ToArray ( ) ;
2016-08-26 17:25:54 +00:00
}
2017-02-14 20:47:46 +00:00
if ( roles . FirstOrDefault ( r = > r . RoleId = = role . Id ) = = null )
2016-08-26 17:25:54 +00:00
{
2017-02-14 20:47:46 +00:00
await ReplyErrorLocalized ( "self_assign_not" ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
return ;
}
2016-12-17 00:16:14 +00:00
if ( guildUser . RoleIds . Contains ( role . Id ) )
2016-08-26 17:25:54 +00:00
{
2017-02-14 20:47:46 +00:00
await ReplyErrorLocalized ( "self_assign_already" , Format . Bold ( role . Name ) ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
return ;
}
2017-03-09 03:00:25 +00:00
var roleIds = roles . Select ( x = > x . RoleId ) . ToArray ( ) ;
2016-08-26 17:25:54 +00:00
if ( conf . ExclusiveSelfAssignedRoles )
{
2017-08-06 15:43:15 +00:00
var sameRoles = guildUser . RoleIds . Where ( r = > roleIds . Contains ( r ) ) ;
2017-03-09 02:50:47 +00:00
2017-08-06 15:43:15 +00:00
foreach ( var roleId in sameRoles )
2016-08-26 17:25:54 +00:00
{
2017-08-06 15:43:15 +00:00
var sameRole = Context . Guild . GetRole ( roleId ) ;
2017-03-09 02:50:47 +00:00
if ( sameRole ! = null )
2017-03-09 03:00:25 +00:00
{
2017-08-06 15:43:15 +00:00
try
{
await guildUser . RemoveRoleAsync ( sameRole ) . ConfigureAwait ( false ) ;
await Task . Delay ( 300 ) . ConfigureAwait ( false ) ;
}
catch ( Exception ex )
{
_log . Warn ( ex ) ;
}
2017-03-09 03:00:25 +00:00
}
2016-08-26 17:25:54 +00:00
}
}
try
{
2017-04-15 00:54:19 +00:00
await guildUser . AddRoleAsync ( role ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
}
2016-10-25 14:55:34 +00:00
catch ( Exception ex )
2016-08-26 17:25:54 +00:00
{
2017-02-14 20:47:46 +00:00
await ReplyErrorLocalized ( "self_assign_perms" ) . ConfigureAwait ( false ) ;
2017-06-15 13:29:41 +00:00
_log . Info ( ex ) ;
2016-08-26 17:25:54 +00:00
return ;
}
2017-02-14 20:47:46 +00:00
var msg = await ReplyConfirmLocalized ( "self_assign_success" , Format . Bold ( role . Name ) ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
if ( conf . AutoDeleteSelfAssignedRoleMessages )
{
2016-12-22 04:32:34 +00:00
msg . DeleteAfter ( 3 ) ;
2016-12-31 10:55:12 +00:00
Context . Message . DeleteAfter ( 3 ) ;
2016-08-26 17:25:54 +00:00
}
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-26 17:25:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Iamnot ( [ Remainder ] IRole role )
2016-08-26 17:25:54 +00:00
{
2016-12-16 18:43:57 +00:00
var guildUser = ( IGuildUser ) Context . User ;
2016-08-26 17:25:54 +00:00
2016-12-03 13:14:37 +00:00
bool autoDeleteSelfAssignedRoleMessages ;
2016-08-26 17:25:54 +00:00
IEnumerable < SelfAssignedRole > roles ;
2017-05-27 17:42:23 +00:00
using ( var uow = _db . UnitOfWork )
2016-08-26 17:25:54 +00:00
{
2016-12-16 21:44:26 +00:00
autoDeleteSelfAssignedRoleMessages = uow . GuildConfigs . For ( Context . Guild . Id , set = > set ) . AutoDeleteSelfAssignedRoleMessages ;
roles = uow . SelfAssignedRoles . GetFromGuild ( Context . Guild . Id ) ;
2016-08-26 17:25:54 +00:00
}
2017-02-14 20:47:46 +00:00
if ( roles . FirstOrDefault ( r = > r . RoleId = = role . Id ) = = null )
2016-08-26 17:25:54 +00:00
{
2017-02-14 20:47:46 +00:00
await ReplyErrorLocalized ( "self_assign_not" ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
return ;
}
2016-12-17 00:16:14 +00:00
if ( ! guildUser . RoleIds . Contains ( role . Id ) )
2016-08-26 17:25:54 +00:00
{
2017-02-14 20:47:46 +00:00
await ReplyErrorLocalized ( "self_assign_not_have" , Format . Bold ( role . Name ) ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
return ;
}
try
{
2017-04-15 00:54:19 +00:00
await guildUser . RemoveRoleAsync ( role ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
}
catch ( Exception )
{
2017-02-14 20:47:46 +00:00
await ReplyErrorLocalized ( "self_assign_perms" ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
return ;
}
2017-02-14 20:47:46 +00:00
var msg = await ReplyConfirmLocalized ( "self_assign_remove" , Format . Bold ( role . Name ) ) . ConfigureAwait ( false ) ;
2016-08-26 17:25:54 +00:00
2016-12-03 13:14:37 +00:00
if ( autoDeleteSelfAssignedRoleMessages )
2016-08-26 17:25:54 +00:00
{
2016-12-22 04:32:34 +00:00
msg . DeleteAfter ( 3 ) ;
2016-12-31 10:55:12 +00:00
Context . Message . DeleteAfter ( 3 ) ;
2016-08-26 17:25:54 +00:00
}
}
}
}
2016-11-22 11:57:39 +00:00
}