Merge Rolebot into Geekbot
This commit is contained in:
parent
3e859e8533
commit
4c1cdc3612
5 changed files with 167 additions and 5 deletions
|
@ -15,11 +15,13 @@ namespace Geekbot.net.Commands
|
||||||
{
|
{
|
||||||
private readonly IErrorHandler _errorHandler;
|
private readonly IErrorHandler _errorHandler;
|
||||||
private readonly IDatabase _redis;
|
private readonly IDatabase _redis;
|
||||||
|
private readonly IReactionListener _reactionListener;
|
||||||
|
|
||||||
public Role(IErrorHandler errorHandler, IDatabase redis)
|
public Role(IErrorHandler errorHandler, IDatabase redis, IReactionListener reactionListener)
|
||||||
{
|
{
|
||||||
_errorHandler = errorHandler;
|
_errorHandler = errorHandler;
|
||||||
_redis = redis;
|
_redis = redis;
|
||||||
|
_reactionListener = reactionListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command(RunMode = RunMode.Async)]
|
[Command(RunMode = RunMode.Async)]
|
||||||
|
@ -91,7 +93,7 @@ namespace Geekbot.net.Commands
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[RequireUserPermission(GuildPermission.Administrator)]
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
||||||
[Command("add", RunMode = RunMode.Async)]
|
[Command("add", RunMode = RunMode.Async)]
|
||||||
[Remarks(CommandCategories.Admin)]
|
[Remarks(CommandCategories.Admin)]
|
||||||
[Summary("Add a role to the whitelist.")]
|
[Summary("Add a role to the whitelist.")]
|
||||||
|
@ -126,7 +128,7 @@ namespace Geekbot.net.Commands
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[RequireUserPermission(GuildPermission.Administrator)]
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
||||||
[Command("remove", RunMode = RunMode.Async)]
|
[Command("remove", RunMode = RunMode.Async)]
|
||||||
[Remarks(CommandCategories.Admin)]
|
[Remarks(CommandCategories.Admin)]
|
||||||
[Summary("Remove a role from the whitelist.")]
|
[Summary("Remove a role from the whitelist.")]
|
||||||
|
@ -150,5 +152,40 @@ namespace Geekbot.net.Commands
|
||||||
_errorHandler.HandleCommandException(e, Context);
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -19,8 +19,9 @@ namespace Geekbot.net
|
||||||
private readonly IServiceProvider _servicesProvider;
|
private readonly IServiceProvider _servicesProvider;
|
||||||
private readonly CommandService _commands;
|
private readonly CommandService _commands;
|
||||||
private readonly IUserRepository _userRepository;
|
private readonly IUserRepository _userRepository;
|
||||||
|
private readonly IReactionListener _reactionListener;
|
||||||
|
|
||||||
public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository)
|
public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
@ -28,6 +29,7 @@ namespace Geekbot.net
|
||||||
_servicesProvider = servicesProvider;
|
_servicesProvider = servicesProvider;
|
||||||
_commands = commands;
|
_commands = commands;
|
||||||
_userRepository = userRepository;
|
_userRepository = userRepository;
|
||||||
|
_reactionListener = reactionListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -180,5 +182,25 @@ namespace Geekbot.net
|
||||||
_logger.Error("Geekbot", "Failed to send delete message...", e);
|
_logger.Error("Geekbot", "Failed to send delete message...", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Reactions
|
||||||
|
//
|
||||||
|
|
||||||
|
public Task ReactionAdded(Cacheable<IUserMessage, ulong> cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
|
||||||
|
{
|
||||||
|
if (reaction.User.Value.IsBot) return Task.CompletedTask;
|
||||||
|
if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask;
|
||||||
|
_reactionListener.GiveRole(socketMessageChannel, reaction);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task ReactionRemoved(Cacheable<IUserMessage, ulong> cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
|
||||||
|
{
|
||||||
|
if (reaction.User.Value.IsBot) return Task.CompletedTask;
|
||||||
|
if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask;
|
||||||
|
_reactionListener.RemoveRole(socketMessageChannel, reaction);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ namespace Geekbot.net.Lib
|
||||||
manaDict.Add("{R}", "<:mtg_red:415216131322806272>");
|
manaDict.Add("{R}", "<:mtg_red:415216131322806272>");
|
||||||
manaDict.Add("{G}", "<:mtg_green:415216131180331009>");
|
manaDict.Add("{G}", "<:mtg_green:415216131180331009>");
|
||||||
manaDict.Add("{S}", "<:mtg_s:415216131293446144>");
|
manaDict.Add("{S}", "<:mtg_s:415216131293446144>");
|
||||||
|
manaDict.Add("{T}", "<:mtg_tap:415258392727257088>");
|
||||||
manaDict.Add("{2/W}", "<:mtg_2w:415216130446065664>");
|
manaDict.Add("{2/W}", "<:mtg_2w:415216130446065664>");
|
||||||
manaDict.Add("{2/U}", "<:mtg_2u:415216130429550592>");
|
manaDict.Add("{2/U}", "<:mtg_2u:415216130429550592>");
|
||||||
manaDict.Add("{2/B}", "<:mtg_2b:415216130160984065>");
|
manaDict.Add("{2/B}", "<:mtg_2b:415216130160984065>");
|
||||||
|
|
98
Geekbot.net/Lib/ReactionListener.cs
Normal file
98
Geekbot.net/Lib/ReactionListener.cs
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord;
|
||||||
|
using Discord.WebSocket;
|
||||||
|
using StackExchange.Redis;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Lib
|
||||||
|
{
|
||||||
|
public class ReactionListener : IReactionListener
|
||||||
|
{
|
||||||
|
private readonly IDatabase _database;
|
||||||
|
private Dictionary<string, Dictionary<IEmote, ulong>> _listener;
|
||||||
|
|
||||||
|
public ReactionListener(IDatabase database)
|
||||||
|
{
|
||||||
|
_database = database;
|
||||||
|
LoadListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task LoadListeners()
|
||||||
|
{
|
||||||
|
var ids = _database.SetMembers("MessageIds");
|
||||||
|
_listener = new Dictionary<string, Dictionary<IEmote, ulong>>();
|
||||||
|
foreach (var id in ids)
|
||||||
|
{
|
||||||
|
var reactions = _database.HashGetAll($"Messages:{id}");
|
||||||
|
var messageId = id;
|
||||||
|
var emojiDict = new Dictionary<IEmote, ulong>();
|
||||||
|
foreach (var r in reactions)
|
||||||
|
{
|
||||||
|
IEmote emote;
|
||||||
|
if (!r.Name.ToString().StartsWith('<'))
|
||||||
|
{
|
||||||
|
var emo = new Emoji(r.Name);
|
||||||
|
emote = (IEmote) emo;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
emote = Emote.Parse(r.Name);
|
||||||
|
}
|
||||||
|
emojiDict.Add(emote, ulong.Parse(r.Value));
|
||||||
|
}
|
||||||
|
_listener.Add(messageId, emojiDict);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsListener(ulong id)
|
||||||
|
{
|
||||||
|
return _listener.ContainsKey(id.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task AddRoleToListener(string messageId, IEmote emoji, IRole role)
|
||||||
|
{
|
||||||
|
if (_database.SetMembers("MessageIds").All(e => e.ToString() != messageId))
|
||||||
|
{
|
||||||
|
_database.SetAdd("MessageIds", messageId);
|
||||||
|
}
|
||||||
|
_database.HashSet($"Messages:{messageId}", new[] {new HashEntry(emoji.ToString(), role.Id.ToString())});
|
||||||
|
_database.SetAdd("MessageIds", messageId);
|
||||||
|
if (_listener.ContainsKey(messageId))
|
||||||
|
{
|
||||||
|
_listener[messageId].Add(emoji, role.Id);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
var dict = new Dictionary<IEmote, ulong>();
|
||||||
|
dict.Add(emoji, role.Id);
|
||||||
|
_listener.Add(messageId, dict);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction)
|
||||||
|
{
|
||||||
|
var roleId = _listener[reaction.MessageId.ToString()][reaction.Emote];
|
||||||
|
var guild = (SocketGuildChannel) channel;
|
||||||
|
var role = guild.Guild.GetRole(roleId);
|
||||||
|
await ((IGuildUser) reaction.User.Value).RemoveRoleAsync(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void GiveRole(ISocketMessageChannel channel, SocketReaction reaction)
|
||||||
|
{
|
||||||
|
var roleId = _listener[reaction.MessageId.ToString()][reaction.Emote];
|
||||||
|
var guild = (SocketGuildChannel) channel;
|
||||||
|
var role = guild.Guild.GetRole(roleId);
|
||||||
|
await ((IGuildUser) reaction.User.Value).AddRoleAsync(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IReactionListener
|
||||||
|
{
|
||||||
|
bool IsListener(ulong id);
|
||||||
|
Task AddRoleToListener(string messageId, IEmote emoji, IRole role);
|
||||||
|
void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction);
|
||||||
|
void GiveRole(ISocketMessageChannel message, SocketReaction reaction);
|
||||||
|
}
|
||||||
|
}
|
|
@ -135,14 +135,16 @@ namespace Geekbot.net
|
||||||
logger.Information("Geekbot", "Registering Stuff");
|
logger.Information("Geekbot", "Registering Stuff");
|
||||||
var translationHandler = new TranslationHandler(client.Guilds, redis, logger);
|
var translationHandler = new TranslationHandler(client.Guilds, redis, logger);
|
||||||
var errorHandler = new ErrorHandler(logger, translationHandler);
|
var errorHandler = new ErrorHandler(logger, translationHandler);
|
||||||
|
var reactionListener = new ReactionListener(redis);
|
||||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||||
services.AddSingleton(commands);
|
services.AddSingleton(commands);
|
||||||
services.AddSingleton<IErrorHandler>(errorHandler);
|
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||||
services.AddSingleton<ITranslationHandler>(translationHandler);
|
services.AddSingleton<ITranslationHandler>(translationHandler);
|
||||||
services.AddSingleton<DiscordSocketClient>(client);
|
services.AddSingleton<DiscordSocketClient>(client);
|
||||||
|
services.AddSingleton<IReactionListener>(reactionListener);
|
||||||
servicesProvider = services.BuildServiceProvider();
|
servicesProvider = services.BuildServiceProvider();
|
||||||
|
|
||||||
var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository);
|
var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository, reactionListener);
|
||||||
|
|
||||||
client.MessageReceived += handlers.RunCommand;
|
client.MessageReceived += handlers.RunCommand;
|
||||||
client.MessageReceived += handlers.UpdateStats;
|
client.MessageReceived += handlers.UpdateStats;
|
||||||
|
@ -150,6 +152,8 @@ namespace Geekbot.net
|
||||||
client.UserJoined += handlers.UserJoined;
|
client.UserJoined += handlers.UserJoined;
|
||||||
client.UserUpdated += handlers.UserUpdated;
|
client.UserUpdated += handlers.UserUpdated;
|
||||||
client.UserLeft += handlers.UserLeft;
|
client.UserLeft += handlers.UserLeft;
|
||||||
|
client.ReactionAdded += handlers.ReactionAdded;
|
||||||
|
client.ReactionRemoved += handlers.ReactionRemoved;
|
||||||
|
|
||||||
if (firstStart || args.Contains("--reset"))
|
if (firstStart || args.Contains("--reset"))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue