Ability to notify mods when user left or deleted message

This commit is contained in:
Runebaas 2017-09-30 17:26:24 +02:00
parent 5771fcddba
commit 8d08b87d09
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
4 changed files with 166 additions and 9 deletions

View file

@ -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<IMessage, ulong> 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...");
}
}
}
}

View file

@ -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)
{

View file

@ -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;
@ -74,11 +74,21 @@ namespace Geekbot.net.Modules
[Command("popuserrepo", RunMode = RunMode.Async)]
[Summary("Set the game that the bot is playing")]
public async Task popUserRepoCommand()
{
try
{
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})");
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]`");
}
}
}

View file

@ -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")))
{