fix(bot): try fetching messages from database before deleting

This commit is contained in:
spiral 2023-01-15 17:34:40 +00:00
parent 8432f00996
commit b13659817d

View File

@ -44,7 +44,7 @@ public partial class ModelRepository
public async Task DeleteMessage(ulong id) public async Task DeleteMessage(ulong id)
{ {
_logger.Warning("not deleting message from database because of database issues", id); if ((await GetMessage(id)) == null)
return; return;
var query = new Query("messages").AsDelete().Where("mid", id); var query = new Query("messages").AsDelete().Where("mid", id);
@ -55,12 +55,13 @@ public partial class ModelRepository
public async Task DeleteMessagesBulk(IReadOnlyCollection<ulong> ids) public async Task DeleteMessagesBulk(IReadOnlyCollection<ulong> ids)
{ {
_logger.Warning("not bulk deleting {count} messages from database because of database issues", ids.Count, ids); var q = new Query("messages").Select("mid").WhereIn("mid", ids.Select(id => (long)id).ToArray());
return; var realIds = await _db.Query<long>(q);
if (realIds.Count() == 0) return;
// Npgsql doesn't support ulongs in general - we hacked around it for plain ulongs but tbh not worth it for collections of ulong // Npgsql doesn't support ulongs in general - we hacked around it for plain ulongs but tbh not worth it for collections of ulong
// Hence we map them to single longs, which *are* supported (this is ok since they're Technically (tm) stored as signed longs in the db anyway) // Hence we map them to single longs, which *are* supported (this is ok since they're Technically (tm) stored as signed longs in the db anyway)
var query = new Query("messages").AsDelete().WhereIn("mid", ids.Select(id => (long)id).ToArray()); var query = new Query("messages").AsDelete().WhereIn("mid", realIds);
var rowCount = await _db.ExecuteQuery(query, messages: true); var rowCount = await _db.ExecuteQuery(query, messages: true);
if (rowCount > 0) if (rowCount > 0)
_logger.Information("Bulk deleted messages ({FoundCount} found) from database: {MessageIds}", rowCount, _logger.Information("Bulk deleted messages ({FoundCount} found) from database: {MessageIds}", rowCount,