2019-04-19 18:48:37 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Dapper;
|
|
|
|
using Discord;
|
2019-07-18 15:13:42 +00:00
|
|
|
using Serilog;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2019-04-21 13:33:22 +00:00
|
|
|
namespace PluralKit.Bot {
|
2019-06-21 11:49:58 +00:00
|
|
|
public class ServerDefinition {
|
|
|
|
public ulong Id { get; set; }
|
2019-06-21 11:52:34 +00:00
|
|
|
public ulong? LogChannel { get; set; }
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 11:49:58 +00:00
|
|
|
public class LogChannelService {
|
2019-04-19 18:48:37 +00:00
|
|
|
private IDiscordClient _client;
|
2019-07-11 19:25:23 +00:00
|
|
|
private DbConnectionFactory _conn;
|
2019-04-22 15:10:18 +00:00
|
|
|
private EmbedService _embed;
|
2019-07-18 15:13:42 +00:00
|
|
|
private ILogger _logger;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2019-07-18 15:13:42 +00:00
|
|
|
public LogChannelService(IDiscordClient client, DbConnectionFactory conn, EmbedService embed, ILogger logger)
|
2019-04-19 18:48:37 +00:00
|
|
|
{
|
|
|
|
this._client = client;
|
2019-07-11 19:25:23 +00:00
|
|
|
this._conn = conn;
|
2019-04-22 15:10:18 +00:00
|
|
|
this._embed = embed;
|
2019-07-18 15:13:42 +00:00
|
|
|
_logger = logger.ForContext<LogChannelService>();
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:53:01 +00:00
|
|
|
public async Task LogMessage(PKSystem system, PKMember member, ulong messageId, IGuildChannel originalChannel, IUser sender, string content) {
|
|
|
|
var logChannel = await GetLogChannel(originalChannel.Guild);
|
|
|
|
if (logChannel == null) return;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2019-07-15 15:53:01 +00:00
|
|
|
var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, sender, content, originalChannel);
|
|
|
|
|
|
|
|
var url = $"https://discordapp.com/channels/{originalChannel.GuildId}/{originalChannel.Id}/{messageId}";
|
|
|
|
await logChannel.SendMessageAsync(text: url, embed: embed);
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<ITextChannel> GetLogChannel(IGuild guild) {
|
2019-07-14 03:23:27 +00:00
|
|
|
using (var conn = await _conn.Obtain())
|
2019-07-11 19:25:23 +00:00
|
|
|
{
|
|
|
|
var server =
|
|
|
|
await conn.QueryFirstOrDefaultAsync<ServerDefinition>("select * from servers where id = @Id",
|
|
|
|
new {Id = guild.Id});
|
|
|
|
if (server?.LogChannel == null) return null;
|
|
|
|
return await _client.GetChannelAsync(server.LogChannel.Value) as ITextChannel;
|
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task SetLogChannel(IGuild guild, ITextChannel newLogChannel) {
|
|
|
|
var def = new ServerDefinition {
|
|
|
|
Id = guild.Id,
|
2019-06-21 11:52:34 +00:00
|
|
|
LogChannel = newLogChannel?.Id
|
2019-04-19 18:48:37 +00:00
|
|
|
};
|
2019-06-21 11:49:58 +00:00
|
|
|
|
2019-07-14 03:23:27 +00:00
|
|
|
using (var conn = await _conn.Obtain())
|
2019-07-11 19:25:23 +00:00
|
|
|
{
|
|
|
|
await conn.QueryAsync(
|
|
|
|
"insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel",
|
|
|
|
def);
|
|
|
|
}
|
2019-07-18 15:13:42 +00:00
|
|
|
|
2019-07-19 00:29:08 +00:00
|
|
|
_logger.Information("Set guild {Guild} log channel to {Channel}", guild.Id, newLogChannel?.Id);
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|