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;
|
2017-11-27 21:57:26 +01:00
|
|
|
|
using Discord.Net;
|
2018-05-13 21:06:41 +02:00
|
|
|
|
using Geekbot.net.Database;
|
|
|
|
|
using Geekbot.net.Database.Models;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
2018-05-13 21:06:41 +02:00
|
|
|
|
using Geekbot.net.Lib.Extensions;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ReactionListener;
|
2017-10-19 21:43:37 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Commands.Admin
|
2017-10-19 21:43:37 +02:00
|
|
|
|
{
|
|
|
|
|
[Group("role")]
|
|
|
|
|
public class Role : ModuleBase
|
|
|
|
|
{
|
2018-05-13 21:06:41 +02:00
|
|
|
|
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;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-13 21:06:41 +02:00
|
|
|
|
public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener)
|
2017-10-19 21:43:37 +02:00
|
|
|
|
{
|
2018-05-13 21:06:41 +02:00
|
|
|
|
_database = database;
|
2017-10-19 21:43:37 +02:00
|
|
|
|
_errorHandler = errorHandler;
|
2018-02-19 22:31:40 +01:00
|
|
|
|
_reactionListener = reactionListener;
|
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
|
|
|
|
|
{
|
2018-05-13 21:06:41 +02:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("There are no roles configured for this server");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-19 21:43:37 +02:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.AppendLine($"**Self Service Roles on {Context.Guild.Name}**");
|
|
|
|
|
sb.AppendLine("To get a role, use `!role name`");
|
2018-05-13 21:06:41 +02:00
|
|
|
|
foreach (var role in roles) sb.AppendLine($"- {role.WhiteListName}");
|
2017-10-19 21:43:37 +02:00
|
|
|
|
await ReplyAsync(sb.ToString());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _errorHandler.HandleCommandException(e, Context);
|
2017-10-19 21:43:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command(RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Get a role by mentioning it.")]
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async Task GiveRole([Summary("roleNickname")] string roleNameRaw)
|
2017-10-19 21:43:37 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-10-30 18:35:08 +01:00
|
|
|
|
var roleName = roleNameRaw.ToLower();
|
2018-05-13 21:06:41 +02:00
|
|
|
|
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;
|
2018-05-13 21:06:41 +02:00
|
|
|
|
var role = Context.Guild.Roles.First(r => r.Id == roleFromDb.RoleId.AsUlong());
|
2017-10-19 21:43:37 +02:00
|
|
|
|
if (role == null)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("That role doesn't seem to exist");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-13 21:06:41 +02:00
|
|
|
|
if (guildUser.RoleIds.Contains(role.Id))
|
2017-10-19 21:43:37 +02:00
|
|
|
|
{
|
2017-10-19 22:01:42 +02:00
|
|
|
|
await guildUser.RemoveRoleAsync(role);
|
2017-10-19 21:43:37 +02:00
|
|
|
|
await ReplyAsync($"Removed you from {role.Name}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-19 21:43:37 +02:00
|
|
|
|
await guildUser.AddRoleAsync(role);
|
|
|
|
|
await ReplyAsync($"Added you to {role.Name}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-19 21:43:37 +02:00
|
|
|
|
await ReplyAsync("That role doesn't seem to exist");
|
|
|
|
|
}
|
2017-11-27 21:57:26 +01:00
|
|
|
|
catch (HttpException e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _errorHandler.HandleHttpException(e, Context);
|
2017-11-27 21:57:26 +01:00
|
|
|
|
}
|
2017-10-19 21:43:37 +02:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
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
|
|
|
|
|
{
|
2017-10-19 23:55:09 +02:00
|
|
|
|
if (role.IsManaged)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("You can't add a role that is managed by discord");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-19 23:55:09 +02:00
|
|
|
|
if (role.Permissions.ManageRoles
|
|
|
|
|
|| role.Permissions.Administrator
|
|
|
|
|
|| role.Permissions.ManageGuild
|
|
|
|
|
|| role.Permissions.BanMembers
|
|
|
|
|
|| role.Permissions.KickMembers)
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(
|
2018-05-13 21:06:41 +02:00
|
|
|
|
"You cannot add that role to self service because it contains one or more dangerous permissions");
|
2017-10-19 23:55:09 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-13 21:06:41 +02: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();
|
2017-10-19 21:43:37 +02:00
|
|
|
|
await ReplyAsync($"Added {role.Name} to the whitelist");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
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.")]
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async Task RemoveRole([Summary("roleNickname")] string roleName)
|
2017-10-19 21:43:37 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 21:06:41 +02:00
|
|
|
|
var roleFromDb = _database.RoleSelfService.FirstOrDefault(e =>
|
|
|
|
|
e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName));
|
|
|
|
|
if (roleFromDb != null)
|
2018-02-03 15:14:11 +01:00
|
|
|
|
{
|
2018-05-13 21:06:41 +02:00
|
|
|
|
_database.RoleSelfService.Remove(roleFromDb);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-02-03 15:14:11 +01:00
|
|
|
|
await ReplyAsync($"Removed {roleName} from the whitelist");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 21:06:41 +02:00
|
|
|
|
await ReplyAsync("There is not whitelisted role with that name");
|
2017-10-19 21:43:37 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
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("messageID")] string messageId, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|