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

304 lines
12 KiB
C#
Raw Normal View History

2016-08-26 17:25:54 +00:00
using Discord;
using Discord.Commands;
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)]
2017-10-21 10:13:31 +00:00
[Priority(1)]
public Task Asar([Remainder] IRole role) =>
Asar(0, role);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageRoles)]
[Priority(0)]
public async Task Asar(int group, [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
{
2017-10-21 10:13:31 +00:00
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
.SelectMany(x => x);
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
{
2017-10-21 10:13:31 +00:00
Group = group,
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-10-21 10:13:31 +00:00
msg = GetText("role_added", Format.Bold(role.Name), Format.Bold(group.ToString()));
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-10-21 10:13:31 +00:00
var rolesStr = new StringBuilder();
2016-12-11 16:18:25 +00:00
var roleCnt = 0;
2017-10-21 10:13:31 +00:00
var exclusive = false;
using (var uow = _db.UnitOfWork)
2016-08-26 17:25:54 +00:00
{
2017-10-21 10:13:31 +00:00
exclusive = uow.GuildConfigs.For(Context.Guild.Id, set => set)
.ExclusiveSelfAssignedRoles;
var roleModels = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
.ToDictionary(x => x.Key, x => x.AsEnumerable().ToArray())
.OrderBy(x => x.Key);
foreach (var kvp in roleModels)
2016-08-26 17:25:54 +00:00
{
2017-10-21 10:13:31 +00:00
rolesStr.AppendLine("\t\t\t\t『" + Format.Bold(GetText("self_assign_group", kvp.Key)) + "』");
foreach (var roleModel in kvp.Value)
2016-08-26 17:25:54 +00:00
{
2017-10-21 10:13:31 +00:00
var role = Context.Guild.Roles.FirstOrDefault(r => r.Id == roleModel.RoleId);
if (role == null)
{
toRemove.Add(roleModel);
uow.SelfAssignedRoles.Remove(roleModel);
}
else
{
rolesStr.AppendLine(Format.Bold(role.Name));
roleCnt++;
}
2016-08-26 17:25:54 +00:00
}
}
2017-10-21 10:13:31 +00:00
if(toRemove.Any())
rolesStr.AppendLine("\t\t\t\t『』");
2016-08-26 17:25:54 +00:00
foreach (var role in toRemove)
{
2017-10-21 10:13:31 +00:00
rolesStr.AppendLine(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-10-21 10:13:31 +00:00
await Context.Channel.SendConfirmAsync("",
Format.Bold(GetText("self_assign_list", roleCnt))
+ "\n\n" + rolesStr.ToString(),
footer: exclusive
? GetText("self_assign_are_exclusive")
: GetText("self_assign_are_not_exclusive")).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)]
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();
}
2017-10-21 10:13:31 +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
}
[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-10-21 10:13:31 +00:00
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
.SelectMany(x => x)
.ToArray();
2016-08-26 17:25:54 +00:00
}
2017-10-21 10:13:31 +00:00
var theRoleYouWant = roles.FirstOrDefault(r => r.RoleId == role.Id);
if (theRoleYouWant == 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-10-21 10:13:31 +00:00
var roleIds = roles
.Where(x => x.Group == theRoleYouWant.Group)
.Select(x => x.RoleId).ToArray();
2016-08-26 17:25:54 +00:00
if (conf.ExclusiveSelfAssignedRoles)
{
2017-10-21 10:13:31 +00:00
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;
}
2017-10-21 10:13:31 +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
}
}
[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;
2017-10-21 10:13:31 +00:00
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
2017-10-23 17:55:36 +00:00
.SelectMany(x => x)
.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
{
2017-10-21 10:13:31 +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
}
}
}
}
2017-10-21 10:13:31 +00:00
}