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;
|
2017-10-19 21:43:37 +02:00
|
|
|
|
using Geekbot.net.Lib;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
|
|
|
|
namespace Geekbot.net.Commands
|
|
|
|
|
{
|
|
|
|
|
[Group("role")]
|
|
|
|
|
public class Role : ModuleBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
|
|
|
|
private readonly IDatabase _redis;
|
2018-02-19 22:31:40 +01:00
|
|
|
|
private readonly IReactionListener _reactionListener;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-02-19 22:31:40 +01:00
|
|
|
|
public Role(IErrorHandler errorHandler, IDatabase redis, IReactionListener reactionListener)
|
2017-10-19 21:43:37 +02:00
|
|
|
|
{
|
|
|
|
|
_errorHandler = errorHandler;
|
|
|
|
|
_redis = redis;
|
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)]
|
|
|
|
|
[Remarks(CommandCategories.Helpers)]
|
|
|
|
|
[Summary("Get a list of all available roles.")]
|
|
|
|
|
public async Task getAllRoles()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var roles = _redis.HashGetAll($"{Context.Guild.Id}:RoleWhitelist");
|
|
|
|
|
if (roles.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
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`");
|
2017-12-29 01:53:50 +01:00
|
|
|
|
foreach (var role in roles) sb.AppendLine($"- {role.Name}");
|
2017-10-19 21:43:37 +02:00
|
|
|
|
await ReplyAsync(sb.ToString());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command(RunMode = RunMode.Async)]
|
|
|
|
|
[Remarks(CommandCategories.Helpers)]
|
|
|
|
|
[Summary("Get a role by mentioning it.")]
|
2017-10-30 18:35:08 +01: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();
|
2017-10-19 21:43:37 +02:00
|
|
|
|
if (_redis.HashExists($"{Context.Guild.Id}:RoleWhitelist", roleName))
|
|
|
|
|
{
|
|
|
|
|
var guildUser = (IGuildUser) Context.User;
|
|
|
|
|
var roleId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:RoleWhitelist", roleName));
|
|
|
|
|
var role = Context.Guild.Roles.First(r => r.Id == roleId);
|
|
|
|
|
if (role == null)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("That role doesn't seem to exist");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-19 21:43:37 +02:00
|
|
|
|
if (guildUser.RoleIds.Contains(roleId))
|
|
|
|
|
{
|
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)
|
|
|
|
|
{
|
2017-11-27 22:07:05 +01:00
|
|
|
|
_errorHandler.HandleHttpException(e, Context);
|
2017-11-27 21:57:26 +01:00
|
|
|
|
}
|
2017-10-19 21:43:37 +02:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-19 22:31:40 +01:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
2017-10-19 21:43:37 +02:00
|
|
|
|
[Command("add", RunMode = RunMode.Async)]
|
|
|
|
|
[Remarks(CommandCategories.Admin)]
|
|
|
|
|
[Summary("Add a role to the whitelist.")]
|
|
|
|
|
public async Task addRole([Summary("@role")] IRole role, [Summary("alias")] string roleName)
|
|
|
|
|
{
|
|
|
|
|
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(
|
|
|
|
|
"Woah, i don't think you want to add that role to self service as it contains some dangerous permissions");
|
2017-10-19 23:55:09 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
|
|
|
|
_redis.HashSet($"{Context.Guild.Id}:RoleWhitelist",
|
|
|
|
|
new[] {new HashEntry(roleName.ToLower(), role.Id.ToString())});
|
2017-10-19 21:43:37 +02:00
|
|
|
|
await ReplyAsync($"Added {role.Name} to the whitelist");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-19 22:31:40 +01:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
2017-10-19 21:43:37 +02:00
|
|
|
|
[Command("remove", RunMode = RunMode.Async)]
|
|
|
|
|
[Remarks(CommandCategories.Admin)]
|
|
|
|
|
[Summary("Remove a role from the whitelist.")]
|
|
|
|
|
public async Task removeRole([Summary("roleNickname")] string roleName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-03 15:14:11 +01:00
|
|
|
|
|
|
|
|
|
var success = _redis.HashDelete($"{Context.Guild.Id}:RoleWhitelist", roleName.ToLower());
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync($"Removed {roleName} from the whitelist");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ReplyAsync("There is not whitelisted role with that name...");
|
|
|
|
|
|
2017-10-19 21:43:37 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-19 22:31:40 +01:00
|
|
|
|
|
|
|
|
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
|
|
|
|
[Remarks(CommandCategories.Admin)]
|
|
|
|
|
[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);
|
|
|
|
|
emote = (IEmote) emo;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|