2017-09-30 01:38:10 +02:00
|
|
|
|
using System;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using System.Linq;
|
2017-09-30 17:26:24 +02:00
|
|
|
|
using System.Text;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.WebSocket;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using Geekbot.net.Database;
|
2018-08-29 21:16:01 +02:00
|
|
|
|
using Geekbot.net.Database.Models;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
using Geekbot.net.Lib.AlmostRedis;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using Geekbot.net.Lib.Extensions;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Logger;
|
|
|
|
|
using Geekbot.net.Lib.ReactionListener;
|
|
|
|
|
using Geekbot.net.Lib.UserRepository;
|
2018-06-13 22:18:57 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
|
|
|
|
|
namespace Geekbot.net
|
|
|
|
|
{
|
|
|
|
|
public class Handlers
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
private readonly DatabaseContext _database;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
private readonly IDiscordClient _client;
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
private readonly IAlmostRedis _redis;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
private readonly IServiceProvider _servicesProvider;
|
|
|
|
|
private readonly CommandService _commands;
|
|
|
|
|
private readonly IUserRepository _userRepository;
|
2018-02-19 22:31:40 +01:00
|
|
|
|
private readonly IReactionListener _reactionListener;
|
2018-08-29 21:16:01 +02:00
|
|
|
|
private readonly DatabaseContext _messageCounterDatabaseContext;
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-08-29 21:16:01 +02:00
|
|
|
|
public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis,
|
2018-05-15 01:18:26 +02:00
|
|
|
|
IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository,
|
|
|
|
|
IReactionListener reactionListener)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2018-08-29 21:16:01 +02:00
|
|
|
|
_database = databaseInitializer.Initialize();
|
|
|
|
|
_messageCounterDatabaseContext = databaseInitializer.Initialize();
|
2017-09-30 01:38:10 +02:00
|
|
|
|
_client = client;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_redis = redis;
|
|
|
|
|
_servicesProvider = servicesProvider;
|
|
|
|
|
_commands = commands;
|
|
|
|
|
_userRepository = userRepository;
|
2018-02-19 22:31:40 +01:00
|
|
|
|
_reactionListener = reactionListener;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-09-30 01:38:10 +02:00
|
|
|
|
//
|
|
|
|
|
// Incoming Messages
|
|
|
|
|
//
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-09-30 01:38:10 +02:00
|
|
|
|
public Task RunCommand(SocketMessage messageParam)
|
|
|
|
|
{
|
2017-10-30 18:51:57 +01:00
|
|
|
|
try
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2017-11-11 16:20:26 +01:00
|
|
|
|
if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask;
|
2017-10-30 18:51:57 +01:00
|
|
|
|
if (message.Author.IsBot) return Task.CompletedTask;
|
|
|
|
|
var argPos = 0;
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-10-30 18:51:57 +01:00
|
|
|
|
var lowCaseMsg = message.ToString().ToLower();
|
|
|
|
|
if (lowCaseMsg.StartsWith("hui"))
|
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Hui ?? false;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
if (hasPing)
|
|
|
|
|
{
|
|
|
|
|
message.Channel.SendMessageAsync("hui!!!");
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2017-10-30 18:51:57 +01:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-03-02 23:05:14 +01:00
|
|
|
|
if (lowCaseMsg.StartsWith("ping ") || lowCaseMsg.Equals("ping"))
|
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Ping ?? false;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
if (hasPing)
|
2018-03-02 23:05:14 +01:00
|
|
|
|
{
|
|
|
|
|
message.Channel.SendMessageAsync("pong");
|
2018-07-28 16:31:18 +02:00
|
|
|
|
return Task.CompletedTask;
|
2018-03-02 23:05:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-10-30 18:51:57 +01:00
|
|
|
|
if (!(message.HasCharPrefix('!', ref argPos) ||
|
|
|
|
|
message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask;
|
|
|
|
|
var context = new CommandContext(_client, message);
|
|
|
|
|
var commandExec = _commands.ExecuteAsync(context, argPos, _servicesProvider);
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Command,
|
2018-02-13 23:42:31 +01:00
|
|
|
|
context.Message.Content.Split(" ")[0].Replace("!", ""),
|
|
|
|
|
SimpleConextConverter.ConvertContext(context));
|
2017-09-30 01:38:10 +02:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2017-10-30 18:51:57 +01:00
|
|
|
|
catch (Exception e)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Failed to Process Message", e);
|
2017-09-30 01:38:10 +02:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 22:18:57 +02:00
|
|
|
|
public async Task UpdateStats(SocketMessage message)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2017-11-11 16:20:26 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
if (message == null) return;
|
2017-11-11 16:20:26 +01:00
|
|
|
|
if (message.Channel.Name.StartsWith('@'))
|
|
|
|
|
{
|
2018-08-26 17:54:26 +02:00
|
|
|
|
_logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message, true));
|
2018-06-13 22:18:57 +02:00
|
|
|
|
return;
|
2017-11-11 16:20:26 +01:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-11-11 16:20:26 +01:00
|
|
|
|
var channel = (SocketGuildChannel) message.Channel;
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-08-29 23:01:10 +02:00
|
|
|
|
// just testing, redis will remain the source of truth for now
|
|
|
|
|
var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlCommandAsync(
|
|
|
|
|
"UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}",
|
|
|
|
|
channel.Guild.Id.AsLong(),
|
|
|
|
|
message.Author.Id.AsLong()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (rowId == 0)
|
|
|
|
|
{
|
|
|
|
|
_messageCounterDatabaseContext.Messages.Add(new MessagesModel
|
|
|
|
|
{
|
|
|
|
|
UserId = message.Author.Id.AsLong(),
|
|
|
|
|
GuildId = channel.Guild.Id.AsLong(),
|
|
|
|
|
MessageCount = 1
|
|
|
|
|
});
|
|
|
|
|
_messageCounterDatabaseContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString());
|
|
|
|
|
await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString());
|
2017-09-30 01:38:10 +02:00
|
|
|
|
|
2018-06-13 22:18:57 +02:00
|
|
|
|
if (message.Author.IsBot) return;
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message));
|
2017-11-11 16:20:26 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Message, "Could not process message stats", e);
|
2017-11-11 16:20:26 +01:00
|
|
|
|
}
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-09-30 01:38:10 +02:00
|
|
|
|
//
|
|
|
|
|
// User Stuff
|
|
|
|
|
//
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-05-10 02:00:26 +02:00
|
|
|
|
public async Task UserJoined(SocketGuildUser user)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
try
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
if (!user.IsBot)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
var message = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(user.Guild.Id.AsLong()))?.WelcomeMessage;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(message))
|
2017-10-26 00:55:04 +02:00
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
message = message.Replace("$user", user.Mention);
|
2018-05-10 02:00:26 +02:00
|
|
|
|
await user.Guild.DefaultChannel.SendMessageAsync(message);
|
2017-10-26 00:55:04 +02:00
|
|
|
|
}
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-05-10 02:00:26 +02:00
|
|
|
|
await _userRepository.Update(user);
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})");
|
2017-10-26 00:55:04 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Failed to send welcome message", e);
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 02:00:26 +02:00
|
|
|
|
public async Task UserUpdated(SocketUser oldUser, SocketUser newUser)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2018-05-10 02:00:26 +02:00
|
|
|
|
await _userRepository.Update(newUser);
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
2017-09-30 17:26:24 +02:00
|
|
|
|
|
|
|
|
|
public async Task UserLeft(SocketGuildUser user)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = _database.GuildSettings.FirstOrDefault(g =>
|
|
|
|
|
g.GuildId.Equals(user.Guild.Id.AsLong()));
|
|
|
|
|
if (guild?.ShowLeave ?? false)
|
2017-09-30 17:26:24 +02:00
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong());
|
|
|
|
|
await modChannelSocket.SendMessageAsync($"{user.Username}#{user.Discriminator} left the server");
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Failed to send leave message", e);
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})");
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-09-30 17:26:24 +02:00
|
|
|
|
//
|
|
|
|
|
// Message Stuff
|
|
|
|
|
//
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2017-09-30 17:26:24 +02:00
|
|
|
|
public async Task MessageDeleted(Cacheable<IMessage, ulong> message, ISocketMessageChannel channel)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guildSocketData = ((IGuildChannel) channel).Guild;
|
2018-08-25 23:26:46 +02:00
|
|
|
|
var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildSocketData.Id.AsLong()));
|
2018-08-26 18:05:50 +02:00
|
|
|
|
if ((guild?.ShowDelete ?? false) && guild?.ModChannel != 0)
|
2017-09-30 17:26:24 +02:00
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong());
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
if (message.Value != null)
|
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
sb.AppendLine($"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>");
|
2018-05-13 17:49:13 +02:00
|
|
|
|
sb.AppendLine(message.Value.Content);
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-09-30 17:26:24 +02:00
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
sb.AppendLine("Someone deleted a message, the message was not cached...");
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-05-13 17:49:13 +02:00
|
|
|
|
await modChannelSocket.SendMessageAsync(sb.ToString());
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Failed to send delete message...", e);
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-02-19 22:31:40 +01:00
|
|
|
|
//
|
|
|
|
|
// Reactions
|
|
|
|
|
//
|
2018-07-28 16:31:18 +02:00
|
|
|
|
|
2018-02-19 22:31:40 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
2018-07-28 16:31:18 +02:00
|
|
|
|
}
|