Refactor log channel storage
This commit is contained in:
parent
4e98a51363
commit
46672d264c
@ -22,13 +22,15 @@ namespace PluralKit.Bot.Commands
|
|||||||
|
|
||||||
public async Task SetLogChannel(Context ctx)
|
public async Task SetLogChannel(Context ctx)
|
||||||
{
|
{
|
||||||
ctx.CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server").CheckGuildContext();
|
ctx.CheckGuildContext().CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server");
|
||||||
|
|
||||||
ITextChannel channel = null;
|
ITextChannel channel = null;
|
||||||
if (ctx.HasNext())
|
if (ctx.HasNext())
|
||||||
channel = ctx.MatchChannel() ?? throw new PKSyntaxError("You must pass a #channel to set.");
|
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)
|
if (channel != null)
|
||||||
await ctx.Reply($"{Emojis.Success} Proxy logging channel set to #{channel.Name.SanitizeMentions()}.");
|
await ctx.Reply($"{Emojis.Success} Proxy logging channel set to #{channel.Name.SanitizeMentions()}.");
|
||||||
|
@ -4,22 +4,17 @@ using Discord;
|
|||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace PluralKit.Bot {
|
namespace PluralKit.Bot {
|
||||||
public class ServerDefinition {
|
|
||||||
public ulong Id { get; set; }
|
|
||||||
public ulong? LogChannel { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class LogChannelService {
|
public class LogChannelService {
|
||||||
private IDiscordClient _client;
|
private IDiscordClient _client;
|
||||||
private DbConnectionFactory _conn;
|
|
||||||
private EmbedService _embed;
|
private EmbedService _embed;
|
||||||
|
private IDataStore _data;
|
||||||
private ILogger _logger;
|
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;
|
_client = client;
|
||||||
this._conn = conn;
|
_embed = embed;
|
||||||
this._embed = embed;
|
_data = data;
|
||||||
_logger = logger.ForContext<LogChannelService>();
|
_logger = logger.ForContext<LogChannelService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,31 +28,11 @@ namespace PluralKit.Bot {
|
|||||||
await logChannel.SendMessageAsync(text: url, embed: embed);
|
await logChannel.SendMessageAsync(text: url, embed: embed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ITextChannel> GetLogChannel(IGuild guild) {
|
private async Task<ITextChannel> GetLogChannel(IGuild guild)
|
||||||
using (var conn = await _conn.Obtain())
|
|
||||||
{
|
{
|
||||||
var server =
|
var guildCfg = await _data.GetGuildConfig(guild.Id);
|
||||||
await conn.QueryFirstOrDefaultAsync<ServerDefinition>("select * from servers where id = @Id",
|
if (guildCfg.LogChannel == null) return null;
|
||||||
new {Id = guild.Id});
|
return await _client.GetChannelAsync(guildCfg.LogChannel.Value) as ITextChannel;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -58,6 +58,12 @@ namespace PluralKit {
|
|||||||
public Instant Timestamp;
|
public Instant Timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public struct GuildConfig
|
||||||
|
{
|
||||||
|
public ulong Id { get; set; }
|
||||||
|
public ulong? LogChannel { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public interface IDataStore
|
public interface IDataStore
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -316,6 +322,17 @@ namespace PluralKit {
|
|||||||
/// Gets the total amount of messages in the data store.
|
/// Gets the total amount of messages in the data store.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<ulong> GetTotalMessages();
|
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 {
|
public class PostgresDataStore: IDataStore {
|
||||||
@ -568,6 +585,28 @@ namespace PluralKit {
|
|||||||
return await conn.ExecuteScalarAsync<ulong>("select count(mid) from messages");
|
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)
|
public async Task AddSwitch(PKSystem system, IEnumerable<PKMember> members)
|
||||||
{
|
{
|
||||||
// Use a transaction here since we're doing multiple executed commands in one
|
// Use a transaction here since we're doing multiple executed commands in one
|
||||||
|
Loading…
Reference in New Issue
Block a user