geekbot/Geekbot.net/Handlers/StatsHandler.cs
runebaas 83dc2c8e49
Attempt to improve code quality with a fearless refactor
- Completely refactor Program.cs
- Split Handlers into separate files
- Split up the command handler into multiple functions
2020-06-19 03:34:37 +02:00

62 lines
No EOL
2.1 KiB
C#

using System;
using System.Threading.Tasks;
using Discord.WebSocket;
using Geekbot.net.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.Logger;
using Microsoft.EntityFrameworkCore;
namespace Geekbot.net.Handlers
{
public class StatsHandler
{
private readonly IGeekbotLogger _logger;
private readonly DatabaseContext _database;
public StatsHandler(IGeekbotLogger logger, DatabaseContext database)
{
_logger = logger;
_database = database;
}
public async Task UpdateStats(SocketMessage message)
{
try
{
if (message == null) return;
if (message.Channel.Name.StartsWith('@'))
{
_logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message, true));
return;
}
var channel = (SocketGuildChannel) message.Channel;
var rowId = await _database.Database.ExecuteSqlRawAsync(
"UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}",
channel.Guild.Id.AsLong(),
message.Author.Id.AsLong()
);
if (rowId == 0)
{
await _database.Messages.AddAsync(new MessagesModel
{
UserId = message.Author.Id.AsLong(),
GuildId = channel.Guild.Id.AsLong(),
MessageCount = 1
});
await _database.SaveChangesAsync();
}
if (message.Author.IsBot) return;
_logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message));
}
catch (Exception e)
{
_logger.Error(LogSource.Message, "Could not process message stats", e);
}
}
}
}