- Add storing bot response messages in postgres
- Add scheduled task to clean up said store
This commit is contained in:
parent
9282d5e9fb
commit
05cc30279a
@ -9,6 +9,8 @@ using App.Metrics;
|
||||
|
||||
using Autofac;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using DSharpPlus;
|
||||
using DSharpPlus.Entities;
|
||||
using DSharpPlus.EventArgs;
|
||||
@ -34,18 +36,21 @@ namespace PluralKit.Bot
|
||||
private readonly PeriodicStatCollector _collector;
|
||||
private readonly IMetrics _metrics;
|
||||
private readonly ErrorMessageService _errorMessageService;
|
||||
private readonly IDatabase _db;
|
||||
|
||||
private bool _hasReceivedReady = false;
|
||||
private Timer _periodicTask; // Never read, just kept here for GC reasons
|
||||
|
||||
public Bot(DiscordShardedClient client, ILifetimeScope services, ILogger logger, PeriodicStatCollector collector, IMetrics metrics, ErrorMessageService errorMessageService)
|
||||
public Bot(DiscordShardedClient client, ILifetimeScope services, ILogger logger, PeriodicStatCollector collector, IMetrics metrics,
|
||||
ErrorMessageService errorMessageService, IDatabase db)
|
||||
{
|
||||
_client = client;
|
||||
_logger = logger.ForContext<Bot>();
|
||||
_services = services;
|
||||
_collector = collector;
|
||||
_metrics = metrics;
|
||||
_errorMessageService = errorMessageService;
|
||||
_logger = logger.ForContext<Bot>();
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
@ -177,6 +182,9 @@ namespace PluralKit.Bot
|
||||
|
||||
await UpdateBotStatus();
|
||||
|
||||
// Clean up message cache in postgres
|
||||
await _db.Execute(conn => conn.QueryAsync("select from cleanup_command_message()"));
|
||||
|
||||
// Collect some stats, submit them to the metrics backend
|
||||
await _collector.CollectStats();
|
||||
await Task.WhenAll(((IMetricsRoot) _metrics).ReportRunner.RunAllAsync());
|
||||
|
@ -64,7 +64,7 @@ namespace PluralKit.Bot
|
||||
internal IDatabase Database => _db;
|
||||
internal ModelRepository Repository => _repo;
|
||||
|
||||
public Task<DiscordMessage> Reply(string text = null, DiscordEmbed embed = null, IEnumerable<IMention> mentions = null)
|
||||
public async Task<DiscordMessage> Reply(string text = null, DiscordEmbed embed = null, IEnumerable<IMention> mentions = null)
|
||||
{
|
||||
if (!this.BotHasAllPermissions(Permissions.SendMessages))
|
||||
// Will be "swallowed" during the error handler anyway, this message is never shown.
|
||||
@ -72,7 +72,12 @@ namespace PluralKit.Bot
|
||||
|
||||
if (embed != null && !this.BotHasAllPermissions(Permissions.EmbedLinks))
|
||||
throw new PKError("PluralKit does not have permission to send embeds in this channel. Please ensure I have the **Embed Links** permission enabled.");
|
||||
return Channel.SendMessageFixedAsync(text, embed: embed, mentions: mentions);
|
||||
var msg = await Channel.SendMessageFixedAsync(text, embed: embed, mentions: mentions);
|
||||
if (embed != null)
|
||||
// Sensitive information that might want to be deleted by :x: reaction is typically in an embed format (member cards, for example)
|
||||
// This may need to be changed at some point but works well enough for now
|
||||
await _db.Execute(conn => _repo.SaveCommandMessage(conn, msg.Id, Author.Id));
|
||||
return msg;
|
||||
}
|
||||
|
||||
public async Task Execute<T>(Command commandDef, Func<T, Task> handler)
|
||||
|
@ -189,9 +189,9 @@ namespace PluralKit.Bot
|
||||
if (ctx.Match("random", "r"))
|
||||
return ctx.Execute<Member>(MemberRandom, m => m.MemberRandom(ctx));
|
||||
|
||||
ctx.Reply(
|
||||
// remove compiler warning
|
||||
return ctx.Reply(
|
||||
$"{Emojis.Error} Unknown command {ctx.PeekArgument().AsCode()}. For a list of possible commands, see <https://pluralkit.me/commands>.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task HandleSystemCommand(Context ctx)
|
||||
|
@ -19,7 +19,7 @@ namespace PluralKit.Core
|
||||
internal class Database: IDatabase
|
||||
{
|
||||
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
||||
private const int TargetSchemaVersion = 10;
|
||||
private const int TargetSchemaVersion = 11;
|
||||
|
||||
private readonly CoreConfig _config;
|
||||
private readonly ILogger _logger;
|
||||
|
17
PluralKit.Core/Database/Migrations/11.sql
Normal file
17
PluralKit.Core/Database/Migrations/11.sql
Normal file
@ -0,0 +1,17 @@
|
||||
-- SCHEMA VERSION 11: (insert date) --
|
||||
-- Create command message table --
|
||||
|
||||
create table command_message
|
||||
(
|
||||
message_id bigint primary key,
|
||||
invoker_id bigint not null,
|
||||
timestamp timestamp not null default now()
|
||||
);
|
||||
|
||||
create function cleanup_command_message() returns void as $$
|
||||
begin
|
||||
delete from command_message where timestamp < now() - interval '1 minute';
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
update info set schema_version = 11;
|
@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public Task SaveCommandMessage(IPKConnection conn, ulong message_id, ulong author_id) =>
|
||||
conn.QueryAsync("insert into command_message (message_id, invoker_id) values (@Message, @Author)",
|
||||
new {Message = message_id, Author = author_id });
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user