geekbot/Geekbot.net/Commands/Admin/Role.cs

204 lines
7.9 KiB
C#
Raw Normal View History

2017-10-19 21:43:37 +02:00
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Net;
using Geekbot.net.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.CommandPreconditions;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
2019-05-12 13:47:15 +02:00
using Geekbot.net.Lib.Localization;
using Geekbot.net.Lib.ReactionListener;
2017-10-19 21:43:37 +02:00
namespace Geekbot.net.Commands.Admin
2017-10-19 21:43:37 +02:00
{
[Group("role")]
[DisableInDirectMessage]
2017-10-19 21:43:37 +02:00
public class Role : ModuleBase
{
private readonly DatabaseContext _database;
2017-10-19 21:43:37 +02:00
private readonly IErrorHandler _errorHandler;
2018-02-19 22:31:40 +01:00
private readonly IReactionListener _reactionListener;
2019-05-12 15:29:22 +02:00
private readonly ITranslationHandler _translationHandler;
2017-12-29 01:53:50 +01:00
2019-05-12 15:29:22 +02:00
public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, ITranslationHandler translationHandler)
2017-10-19 21:43:37 +02:00
{
_database = database;
2017-10-19 21:43:37 +02:00
_errorHandler = errorHandler;
2018-02-19 22:31:40 +01:00
_reactionListener = reactionListener;
2019-05-12 13:47:15 +02:00
_translationHandler = translationHandler;
2017-10-19 21:43:37 +02:00
}
2017-12-29 01:53:50 +01:00
2017-10-19 21:43:37 +02:00
[Command(RunMode = RunMode.Async)]
[Summary("Get a list of all available roles.")]
2018-04-30 23:44:19 +02:00
public async Task GetAllRoles()
2017-10-19 21:43:37 +02:00
{
try
{
2019-05-12 13:47:15 +02:00
var transContext = await _translationHandler.GetGuildContext(Context);
var roles = _database.RoleSelfService.Where(g => g.GuildId.Equals(Context.Guild.Id.AsLong())).ToList();
if (roles.Count == 0)
2017-10-19 21:43:37 +02:00
{
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("NoRolesConfigured"));
2017-10-19 21:43:37 +02:00
return;
}
2017-12-29 01:53:50 +01:00
2017-10-19 21:43:37 +02:00
var sb = new StringBuilder();
2019-05-12 13:47:15 +02:00
sb.AppendLine(transContext.GetString("ListHeader", Context.Guild.Name));
sb.AppendLine(transContext.GetString("ListInstruction"));
foreach (var role in roles) sb.AppendLine($"- {role.WhiteListName}");
2017-10-19 21:43:37 +02:00
await ReplyAsync(sb.ToString());
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-10-19 21:43:37 +02:00
}
}
[Command(RunMode = RunMode.Async)]
[Summary("Get a role by mentioning it.")]
public async Task GiveRole([Summary("role-nickname")] string roleNameRaw)
2017-10-19 21:43:37 +02:00
{
try
{
2019-05-12 13:47:15 +02:00
var transContext = await _translationHandler.GetGuildContext(Context);
2017-10-30 18:35:08 +01:00
var roleName = roleNameRaw.ToLower();
var roleFromDb = _database.RoleSelfService.FirstOrDefault(e =>
e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName));
if (roleFromDb != null)
2017-10-19 21:43:37 +02:00
{
var guildUser = (IGuildUser) Context.User;
var role = Context.Guild.Roles.First(r => r.Id == roleFromDb.RoleId.AsUlong());
2017-10-19 21:43:37 +02:00
if (role == null)
{
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("RoleNotFound"));
2017-10-19 21:43:37 +02:00
return;
}
2017-12-29 01:53:50 +01:00
if (guildUser.RoleIds.Contains(role.Id))
2017-10-19 21:43:37 +02:00
{
await guildUser.RemoveRoleAsync(role);
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("RemovedUserFromRole", role.Name));
2017-10-19 21:43:37 +02:00
return;
}
2017-12-29 01:53:50 +01:00
2017-10-19 21:43:37 +02:00
await guildUser.AddRoleAsync(role);
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("AddedUserFromRole", role.Name));
2017-10-19 21:43:37 +02:00
return;
}
2017-12-29 01:53:50 +01:00
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("RoleNotFound"));
2017-10-19 21:43:37 +02:00
}
catch (HttpException e)
{
await _errorHandler.HandleHttpException(e, Context);
}
2017-10-19 21:43:37 +02:00
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-10-19 21:43:37 +02:00
}
}
2018-02-19 22:31:40 +01:00
[RequireUserPermission(GuildPermission.ManageRoles)]
2017-10-19 21:43:37 +02:00
[Command("add", RunMode = RunMode.Async)]
[Summary("Add a role to the whitelist.")]
2018-04-30 23:44:19 +02:00
public async Task AddRole([Summary("@role")] IRole role, [Summary("alias")] string roleName)
2017-10-19 21:43:37 +02:00
{
try
{
2019-05-12 13:47:15 +02:00
var transContext = await _translationHandler.GetGuildContext(Context);
if (role.IsManaged)
{
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("CannotAddManagedRole"));
return;
}
2017-12-29 01:53:50 +01:00
if (role.Permissions.ManageRoles
|| role.Permissions.Administrator
|| role.Permissions.ManageGuild
|| role.Permissions.BanMembers
|| role.Permissions.KickMembers)
{
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("CannotAddDangerousRole"));
return;
}
2017-12-29 01:53:50 +01:00
_database.RoleSelfService.Add(new RoleSelfServiceModel
{
GuildId = Context.Guild.Id.AsLong(),
RoleId = role.Id.AsLong(),
WhiteListName = roleName
});
2018-05-14 18:57:07 +02:00
await _database.SaveChangesAsync();
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("CannotAddDangerousRole", role.Name));
2017-10-19 21:43:37 +02:00
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-10-19 21:43:37 +02:00
}
}
2018-02-19 22:31:40 +01:00
[RequireUserPermission(GuildPermission.ManageRoles)]
2017-10-19 21:43:37 +02:00
[Command("remove", RunMode = RunMode.Async)]
[Summary("Remove a role from the whitelist.")]
public async Task RemoveRole([Summary("role-nickname")] string roleName)
2017-10-19 21:43:37 +02:00
{
try
{
2019-05-12 13:47:15 +02:00
var transContext = await _translationHandler.GetGuildContext(Context);
var roleFromDb = _database.RoleSelfService.FirstOrDefault(e =>
e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName));
if (roleFromDb != null)
{
_database.RoleSelfService.Remove(roleFromDb);
2018-05-14 18:57:07 +02:00
await _database.SaveChangesAsync();
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("RemovedRoleFromWhitelist", roleName));
return;
}
2019-05-12 13:47:15 +02:00
await ReplyAsync(transContext.GetString("RoleNotFound"));
2017-10-19 21:43:37 +02:00
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-10-19 21:43:37 +02:00
}
}
2018-02-19 22:31:40 +01:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[Summary("Give a role by clicking on an emoji")]
[Command("listen", RunMode = RunMode.Async)]
public async Task AddListener([Summary("message-ID")] string messageId, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role)
2018-02-19 22:31:40 +01:00
{
try
{
var message = (IUserMessage) await Context.Channel.GetMessageAsync(ulong.Parse(messageId));
IEmote emote;
if (!emoji.StartsWith('<'))
{
var emo = new Emoji(emoji);
2018-04-30 23:44:19 +02:00
emote = emo;
2018-02-19 22:31:40 +01:00
}
else
{
emote = Emote.Parse(emoji);
}
await message.AddReactionAsync(emote);
await _reactionListener.AddRoleToListener(messageId, emote, role);
await Context.Message.DeleteAsync();
}
catch (HttpException e)
{
await Context.Channel.SendMessageAsync("Custom emojis from other servers are not supported");
Console.WriteLine(e);
}
catch (Exception e)
{
await Context.Channel.SendMessageAsync("Something went wrong... please try again on a new message");
Console.WriteLine(e);
}
}
2017-10-19 21:43:37 +02:00
}
}