NadekoBot/NadekoBot.Core/Modules/Administration/SelfAssignedRolesCommands.cs

279 lines
11 KiB
C#
Raw Normal View History

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;
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]
public class SelfAssignedRolesCommands : NadekoSubmodule
2016-08-26 17:25:54 +00:00
{
private readonly DbService _db;
public SelfAssignedRolesCommands(DbService db)
{
_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;
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);
}
[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;
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;
var error = false;
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
{
msg = GetText("role_in_list", Format.Bold(role.Name));
error = true;
2016-08-26 17:25:54 +00:00
}
else
{
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();
msg = GetText("role_added", Format.Bold(role.Name));
2016-08-26 17:25:54 +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
}
[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
{
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;
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();
}
if (!success)
2016-08-26 17:25:54 +00:00
{
await ReplyErrorLocalized("self_assign_not").ConfigureAwait(false);
2016-08-26 17:25:54 +00:00
return;
}
await ReplyConfirmLocalized("self_assign_rem", Format.Bold(role.Name)).ConfigureAwait(false);
2016-08-26 17:25:54 +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;
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;
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)
{
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));
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
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
}
[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;
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();
}
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
}
[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;
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
}
if (roles.FirstOrDefault(r=>r.RoleId == role.Id) == null)
2016-08-26 17:25:54 +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
{
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)
{
var sameRoles = guildUser.RoleIds.Where(r => roleIds.Contains(r));
foreach (var roleId in sameRoles)
2016-08-26 17:25:54 +00:00
{
var sameRole = Context.Guild.GetRole(roleId);
if (sameRole != null)
2017-03-09 03:00:25 +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
{
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;
}
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
}
}
[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
bool autoDeleteSelfAssignedRoleMessages;
2016-08-26 17:25:54 +00:00
IEnumerable<SelfAssignedRole> roles;
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
}
if (roles.FirstOrDefault(r => r.RoleId == role.Id) == null)
2016-08-26 17:25:54 +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
{
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)
{
await ReplyErrorLocalized("self_assign_perms").ConfigureAwait(false);
2016-08-26 17:25:54 +00:00
return;
}
var msg = await ReplyConfirmLocalized("self_assign_remove", Format.Bold(role.Name)).ConfigureAwait(false);
2016-08-26 17:25:54 +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
}
}
}
}
}