From 8d08b87d090ceba200dcd1ae76430ca1f0002ee3 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 30 Sep 2017 17:26:24 +0200 Subject: [PATCH] Ability to notify mods when user left or deleted message --- Geekbot.net/Handlers.cs | 60 +++++++++++++++++++ Geekbot.net/Lib/UserRepository.cs | 10 +++- Geekbot.net/Modules/AdminCmd.cs | 99 +++++++++++++++++++++++++++++-- Geekbot.net/Program.cs | 6 +- 4 files changed, 166 insertions(+), 9 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 6c25104..bf62cac 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -96,5 +97,64 @@ namespace Geekbot.net _userRepository.Update(newUser); return Task.CompletedTask; } + + public async Task UserLeft(SocketGuildUser user) + { + try + { + var sendLeftEnabled = _redis.HashGet($"{user.Guild.Id}:Settings", "ShowLeave"); + if (sendLeftEnabled.ToString() == "1") + { + var modChannel = _redis.HashGet($"{user.Guild.Id}:Settings", "ModChannel"); + if (!string.IsNullOrEmpty(modChannel)) + { + var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync((ulong)modChannel); + await modChannelSocket.SendMessageAsync($"{user.Username}#{user.Discriminator} left the server"); + } + } + } + catch (Exception e) + { + _logger.Error(e, "Failed to send leave message..."); + } + _logger.Information($"[Geekbot] {user.Id} ({user.Username}) left {user.Guild.Id} ({user.Guild.Name})"); + } + + // + // Message Stuff + // + + public async Task MessageDeleted(Cacheable message, ISocketMessageChannel channel) + { + try + { + var guild = ((IGuildChannel) channel).Guild; + var sendLeftEnabled = _redis.HashGet($"{guild.Id}:Settings", "ShowDelete"); + if (sendLeftEnabled.ToString() == "1") + { + var modChannel = _redis.HashGet($"{guild.Id}:Settings", "ModChannel"); + if (!string.IsNullOrEmpty(modChannel)) + { + var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync((ulong)modChannel); + var sb = new StringBuilder(); + if (message.Value != null) + { + sb.AppendLine( + $"{message.Value.Author.Username}#{message.Value.Author.Discriminator} deleted the following message from <#{channel.Id}>"); + sb.AppendLine(message.Value.Content); + } + else + { + sb.AppendLine("Someone deleted a message, the message was not cached..."); + } + await modChannelSocket.SendMessageAsync(sb.ToString()); + } + } + } + catch (Exception e) + { + _logger.Error(e, "Failed to send delete message..."); + } + } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/UserRepository.cs b/Geekbot.net/Lib/UserRepository.cs index 71faba3..9c85936 100644 --- a/Geekbot.net/Lib/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository.cs @@ -40,9 +40,15 @@ namespace Geekbot.net.Lib public UserRepositoryUser Get(ulong userId) { - var user = _redis.HashGetAll($"Users:{userId}").ToDictionary(); + var user = _redis.HashGetAll($"Users:{userId}"); + for (int i = 1; i < 6; i++) + { + if (user.Length != 0) break; + user = _redis.HashGetAll($"Users:{userId + (ulong)i}"); + + } var dto = new UserRepositoryUser(); - foreach (var a in user) + foreach (var a in user.ToDictionary()) { switch (a.Key) { diff --git a/Geekbot.net/Modules/AdminCmd.cs b/Geekbot.net/Modules/AdminCmd.cs index ea2bc05..f4a91de 100644 --- a/Geekbot.net/Modules/AdminCmd.cs +++ b/Geekbot.net/Modules/AdminCmd.cs @@ -1,10 +1,10 @@ using System; +using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; -using Google.Apis.YouTube.v3.Data; using Serilog; using StackExchange.Redis; @@ -75,10 +75,20 @@ namespace Geekbot.net.Modules [Summary("Set the game that the bot is playing")] public async Task popUserRepoCommand() { - var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result; - if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString())) + try { - await ReplyAsync($"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})"); + var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result; + if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString())) + { + await ReplyAsync( + $"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})"); + return; + } + } + catch (Exception e) + { + await ReplyAsync( + $"Sorry, only the botowner can do this"); return; } var success = 0; @@ -101,8 +111,85 @@ namespace Geekbot.net.Modules } catch (Exception e) { - await ReplyAsync("Couldn't complete User Repository, see console for more info"); - _errorHandler.HandleCommandException(e, Context); + _errorHandler.HandleCommandException(e, Context, "Couldn't complete User Repository, see console for more info"); + } + } + + [RequireUserPermission(GuildPermission.Administrator)] + [Command("modchannel", RunMode = RunMode.Async)] + [Summary("Set the game that the bot is playing")] + public async Task selectModChannel([Summary("ChannelId")] ulong channelId) + { + try + { + var channel = (ISocketMessageChannel)_client.GetChannel(channelId); + if (string.IsNullOrEmpty(channel.Name)) + { + await ReplyAsync("I couldn't find that channel..."); + return; + } + var sb = new StringBuilder(); + sb.AppendLine("Successfully saved mod channel, you can now do the following"); + sb.AppendLine("- `!admin showleave true` - send message to mod channel when someone leaves"); + sb.AppendLine("- `!admin showdel true` - send message to mod channel when someone deletes a message"); + await channel.SendMessageAsync(sb.ToString()); + _redis.HashSet($"{Context.Guild.Id}:Settings", new HashEntry[] {new HashEntry("ModChannel", channel.Id.ToString())}); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to be valid"); + } + } + + [RequireUserPermission(GuildPermission.Administrator)] + [Command("showleave", RunMode = RunMode.Async)] + [Summary("Notify modchannel when someone leaves")] + public async Task showLeave([Summary("true/false")] bool enabled) + { + var modChannelId = (ulong)_redis.HashGet($"{Context.Guild.Id}:Settings", "ModChannel"); + try + { + var modChannel = (ISocketMessageChannel) _client.GetChannel(modChannelId); + if (enabled) + { + await modChannel.SendMessageAsync("Saved - now sending messages here when someone leaves"); + _redis.HashSet($"{Context.Guild.Id}:Settings", new HashEntry[] {new HashEntry("ShowLeave", true)}); + } + else + { + await modChannel.SendMessageAsync("Saved - stopping sending messages here when someone leaves"); + _redis.HashSet($"{Context.Guild.Id}:Settings", new HashEntry[] {new HashEntry("ShowLeave", false)}); + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context, "Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`"); + } + } + + [RequireUserPermission(GuildPermission.Administrator)] + [Command("showdel", RunMode = RunMode.Async)] + [Summary("Notify modchannel when someone leaves")] + public async Task showDelete([Summary("true/false")] bool enabled) + { + var modChannelId = (ulong)_redis.HashGet($"{Context.Guild.Id}:Settings", "ModChannel"); + try + { + var modChannel = (ISocketMessageChannel) _client.GetChannel(modChannelId); + if (enabled) + { + await modChannel.SendMessageAsync("Saved - now sending messages here when someone deletes a message"); + _redis.HashSet($"{Context.Guild.Id}:Settings", new HashEntry[] {new HashEntry("ShowDelete", true)}); + } + else + { + await modChannel.SendMessageAsync("Saved - stopping sending messages here when someone deletes a message"); + _redis.HashSet($"{Context.Guild.Id}:Settings", new HashEntry[] {new HashEntry("ShowDelete", false)}); + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context, "Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`"); } } } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 12bdb73..cd43c43 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -53,7 +53,9 @@ namespace Geekbot.net client = new DiscordSocketClient(new DiscordSocketConfig { - LogLevel = LogSeverity.Verbose + LogLevel = LogSeverity.Verbose, + MessageCacheSize = 1000, + AlwaysDownloadUsers = true }); client.Log += DiscordLogger; commands = new CommandService(); @@ -147,8 +149,10 @@ namespace Geekbot.net client.MessageReceived += handlers.RunCommand; client.MessageReceived += handlers.UpdateStats; + client.MessageDeleted += handlers.MessageDeleted; client.UserJoined += handlers.UserJoined; client.UserUpdated += handlers.UserUpdated; + client.UserLeft += handlers.UserLeft; if (firstStart || (args.Length != 0 && args.Contains("--reset"))) {