geekbot/Geekbot.net/Commands/Role.cs

154 lines
5.3 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;
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;
2017-12-29 01:53:50 +01:00
2017-10-19 21:43:37 +02:00
public Role(IErrorHandler errorHandler, IDatabase redis)
{
_errorHandler = errorHandler;
_redis = redis;
}
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))
{
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");
}
catch (HttpException e)
{
2017-11-27 22:07:05 +01:00
_errorHandler.HandleHttpException(e, Context);
}
2017-10-19 21:43:37 +02:00
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
[RequireUserPermission(GuildPermission.Administrator)]
[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
{
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
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");
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);
}
}
[RequireUserPermission(GuildPermission.Administrator)]
[Command("remove", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Admin)]
[Summary("Remove a role from the whitelist.")]
public async Task removeRole([Summary("roleNickname")] string roleName)
{
try
{
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);
}
}
}
}