2019-04-19 18:48:37 +00:00
using System.Data ;
using System.Threading.Tasks ;
using Dapper ;
using Discord ;
2019-04-21 13:33:22 +00:00
namespace PluralKit.Bot {
2019-04-19 18:48:37 +00:00
class ServerDefinition {
public ulong Id ;
public ulong LogChannel ;
}
class LogChannelService {
private IDiscordClient _client ;
private IDbConnection _connection ;
2019-04-22 15:10:18 +00:00
private EmbedService _embed ;
2019-04-19 18:48:37 +00:00
2019-04-22 15:10:18 +00:00
public LogChannelService ( IDiscordClient client , IDbConnection connection , EmbedService embed )
2019-04-19 18:48:37 +00:00
{
this . _client = client ;
this . _connection = connection ;
2019-04-22 15:10:18 +00:00
this . _embed = embed ;
2019-04-19 18:48:37 +00:00
}
public async Task LogMessage ( PKSystem system , PKMember member , IMessage message , IUser sender ) {
var channel = await GetLogChannel ( ( message . Channel as IGuildChannel ) . Guild ) ;
if ( channel = = null ) return ;
2019-04-22 15:10:18 +00:00
var embed = _embed . CreateLoggedMessageEmbed ( system , member , message , sender ) ;
2019-04-19 18:48:37 +00:00
await channel . SendMessageAsync ( text : message . GetJumpUrl ( ) , embed : embed ) ;
}
public async Task < ITextChannel > GetLogChannel ( IGuild guild ) {
var server = await _connection . QueryFirstAsync < ServerDefinition > ( "select * from servers where id = @Id" , new { Id = guild . Id } ) ;
if ( server = = null ) return null ;
return await _client . GetChannelAsync ( server . LogChannel ) as ITextChannel ;
}
public async Task SetLogChannel ( IGuild guild , ITextChannel newLogChannel ) {
var def = new ServerDefinition {
Id = guild . Id ,
LogChannel = newLogChannel . Id
} ;
await _connection . ExecuteAsync ( "insert into servers(id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel" , def ) ;
}
}
}