Refactor log channel storage
This commit is contained in:
parent
4e98a51363
commit
46672d264c
@ -22,14 +22,16 @@ namespace PluralKit.Bot.Commands
|
||||
|
||||
public async Task SetLogChannel(Context ctx)
|
||||
{
|
||||
ctx.CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server").CheckGuildContext();
|
||||
ctx.CheckGuildContext().CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server");
|
||||
|
||||
ITextChannel channel = null;
|
||||
if (ctx.HasNext())
|
||||
channel = ctx.MatchChannel() ?? throw new PKSyntaxError("You must pass a #channel to set.");
|
||||
|
||||
await _logChannels.SetLogChannel(ctx.Guild, channel);
|
||||
|
||||
var cfg = await _data.GetGuildConfig(ctx.Guild.Id);
|
||||
cfg.LogChannel = channel?.Id;
|
||||
await _data.SaveGuildConfig(cfg);
|
||||
|
||||
if (channel != null)
|
||||
await ctx.Reply($"{Emojis.Success} Proxy logging channel set to #{channel.Name.SanitizeMentions()}.");
|
||||
else
|
||||
|
@ -4,22 +4,17 @@ using Discord;
|
||||
using Serilog;
|
||||
|
||||
namespace PluralKit.Bot {
|
||||
public class ServerDefinition {
|
||||
public ulong Id { get; set; }
|
||||
public ulong? LogChannel { get; set; }
|
||||
}
|
||||
|
||||
public class LogChannelService {
|
||||
private IDiscordClient _client;
|
||||
private DbConnectionFactory _conn;
|
||||
private EmbedService _embed;
|
||||
private IDataStore _data;
|
||||
private ILogger _logger;
|
||||
|
||||
public LogChannelService(IDiscordClient client, DbConnectionFactory conn, EmbedService embed, ILogger logger)
|
||||
public LogChannelService(IDiscordClient client, EmbedService embed, ILogger logger, IDataStore data)
|
||||
{
|
||||
this._client = client;
|
||||
this._conn = conn;
|
||||
this._embed = embed;
|
||||
_client = client;
|
||||
_embed = embed;
|
||||
_data = data;
|
||||
_logger = logger.ForContext<LogChannelService>();
|
||||
}
|
||||
|
||||
@ -33,31 +28,11 @@ namespace PluralKit.Bot {
|
||||
await logChannel.SendMessageAsync(text: url, embed: embed);
|
||||
}
|
||||
|
||||
public async Task<ITextChannel> GetLogChannel(IGuild guild) {
|
||||
using (var conn = await _conn.Obtain())
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetLogChannel(IGuild guild, ITextChannel newLogChannel) {
|
||||
var def = new ServerDefinition {
|
||||
Id = guild.Id,
|
||||
LogChannel = newLogChannel?.Id
|
||||
};
|
||||
|
||||
using (var conn = await _conn.Obtain())
|
||||
{
|
||||
await conn.QueryAsync(
|
||||
"insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel",
|
||||
def);
|
||||
}
|
||||
|
||||
_logger.Information("Set guild {Guild} log channel to {Channel}", guild.Id, newLogChannel?.Id);
|
||||
private async Task<ITextChannel> GetLogChannel(IGuild guild)
|
||||
{
|
||||
var guildCfg = await _data.GetGuildConfig(guild.Id);
|
||||
if (guildCfg.LogChannel == null) return null;
|
||||
return await _client.GetChannelAsync(guildCfg.LogChannel.Value) as ITextChannel;
|
||||
}
|
||||
}
|
||||
}
|
@ -58,6 +58,12 @@ namespace PluralKit {
|
||||
public Instant Timestamp;
|
||||
}
|
||||
|
||||
public struct GuildConfig
|
||||
{
|
||||
public ulong Id { get; set; }
|
||||
public ulong? LogChannel { get; set; }
|
||||
}
|
||||
|
||||
public interface IDataStore
|
||||
{
|
||||
/// <summary>
|
||||
@ -316,6 +322,17 @@ namespace PluralKit {
|
||||
/// Gets the total amount of messages in the data store.
|
||||
/// </summary>
|
||||
Task<ulong> GetTotalMessages();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the guild configuration struct for a given guild.
|
||||
/// </summary>
|
||||
/// <returns>The guild's configuration struct, or a default struct if no guild was found in the data store.</returns>
|
||||
Task<GuildConfig> GetGuildConfig(ulong guild);
|
||||
|
||||
/// <summary>
|
||||
/// Saves the given guild configuration struct to the data store.
|
||||
/// </summary>
|
||||
Task SaveGuildConfig(GuildConfig cfg);
|
||||
}
|
||||
|
||||
public class PostgresDataStore: IDataStore {
|
||||
@ -568,6 +585,28 @@ namespace PluralKit {
|
||||
return await conn.ExecuteScalarAsync<ulong>("select count(mid) from messages");
|
||||
}
|
||||
|
||||
public async Task<GuildConfig> GetGuildConfig(ulong guild)
|
||||
{
|
||||
using (var conn = await _conn.Obtain())
|
||||
{
|
||||
var cfg = await conn.QuerySingleOrDefaultAsync<GuildConfig>("select * from servers where id = @Id",
|
||||
new {Id = guild});
|
||||
|
||||
if (cfg.Id == 0)
|
||||
// No entry was found in the db, this is the default entry returned
|
||||
cfg.Id = guild;
|
||||
|
||||
return cfg;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveGuildConfig(GuildConfig cfg)
|
||||
{
|
||||
using (var conn = await _conn.Obtain())
|
||||
await conn.ExecuteAsync("insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel", cfg);
|
||||
_logger.Information("Updated guild configuration {@GuildCfg}", cfg);
|
||||
}
|
||||
|
||||
public async Task AddSwitch(PKSystem system, IEnumerable<PKMember> members)
|
||||
{
|
||||
// Use a transaction here since we're doing multiple executed commands in one
|
||||
|
Loading…
Reference in New Issue
Block a user