Fixed several issues; ready for testing

This commit is contained in:
Noikoio 2018-02-20 12:26:10 -08:00
parent 3362c8701c
commit 7f22da5a4e
2 changed files with 65 additions and 68 deletions

View file

@ -3,6 +3,7 @@ using Discord.WebSocket;
using Npgsql; using Npgsql;
using NpgsqlTypes; using NpgsqlTypes;
using System; using System;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Module.ModLogs namespace Noikoio.RegexBot.Module.ModLogs
@ -32,18 +33,27 @@ namespace Noikoio.RegexBot.Module.ModLogs
} }
#region Event handling #region Event handling
private async Task Client_MessageReceived(SocketMessage arg) => await AddOrUpdateCacheItemAsync(arg); private async Task Client_MessageReceived(SocketMessage arg)
{
if (arg.Author.IsBot) return;
await AddOrUpdateCacheItemAsync(arg);
}
private async Task Client_MessageUpdated( private async Task Client_MessageUpdated(
Discord.Cacheable<Discord.IMessage, ulong> before, Cacheable<IMessage, ulong> before, SocketMessage after, ISocketMessageChannel channel)
SocketMessage after, ISocketMessageChannel channel)
{ {
if (after is SocketUserMessage afterMsg) if (after.Author.IsBot) return;
// We only want channel messages
if (after is SocketUserMessage afterMsg && !(afterMsg is IDMChannel))
{ {
if (after.Author.IsBot) return;
// We're not interested in all message updates, only those that leave a timestamp. // We're not interested in all message updates, only those that leave a timestamp.
if (!afterMsg.EditedTimestamp.HasValue) return; if (!afterMsg.EditedTimestamp.HasValue) return;
} }
else return; // no after??? else return; // probably unnecessary?
// Once an edited message is cached, the original message contents are discarded. // Once an edited message is cached, the original message contents are discarded.
// This is the only time to report it. // This is the only time to report it.
@ -52,27 +62,24 @@ namespace Noikoio.RegexBot.Module.ModLogs
await AddOrUpdateCacheItemAsync(after); await AddOrUpdateCacheItemAsync(after);
} }
private async Task Client_MessageDeleted( private async Task Client_MessageDeleted(Cacheable<Discord.IMessage, ulong> msg, ISocketMessageChannel channel)
Discord.Cacheable<Discord.IMessage, ulong> msg, ISocketMessageChannel channel)
{ {
await ProcessReportMessage(true, msg.Id, channel, null); await ProcessReportMessage(true, msg.Id, channel, null);
} }
#endregion #endregion
#region Reporting #region Reporting
// Reports an edited or deleted message as if it were a log entry (even though it's not). // Reports an edited or deleted message as if it were a log entry (even though it's not).
private async Task ProcessReportMessage( private async Task ProcessReportMessage(
bool isDelete, ulong messageId, ISocketMessageChannel ch, string editMsg) bool isDelete, ulong messageId, ISocketMessageChannel ch, string editMsg)
{ {
var cht = ch as SocketTextChannel; ulong guildId;
if (cht == null) if (ch is SocketTextChannel sch)
{ {
// TODO remove debug print if (sch is IDMChannel) return;
Console.WriteLine("Incoming message not of a text channel"); guildId = sch.Guild.Id;
return;
} }
ulong guildId = cht.Guild.Id; else return;
// Check if enabled before doing anything else // Check if enabled before doing anything else
var rptTarget = _outGetConfig(guildId) as ConfigItem.EntityName?; var rptTarget = _outGetConfig(guildId) as ConfigItem.EntityName?;
@ -127,24 +134,24 @@ namespace Noikoio.RegexBot.Module.ModLogs
await rptTargetChannel.SendMessageAsync("", embed: em); await rptTargetChannel.SendMessageAsync("", embed: em);
} }
const int ReportCutoffLength = 750; const int ReportCutoffLength = 500;
const string ReportCutoffNotify = "**Message length too long; showing first {0} characters.**\n\n"; const string ReportCutoffNotify = "**Message length too long; showing first {0} characters.**\n\n";
private EmbedBuilder CreateReportEmbed( private EmbedBuilder CreateReportEmbed(
bool isDelete, bool isDelete,
EntityCache.CacheUser ucd, ulong messageId, ISocketMessageChannel chInfo, EntityCache.CacheUser ucd, ulong messageId, ISocketMessageChannel chInfo,
(string, string) content) // tuple: Item1 = cached content. Item2 = after-edit message (string, string) content) // tuple: Item1 = cached content. Item2 = after-edit message
{ {
string before = content.Item1; string msgCached = content.Item1;
string after = content.Item2; string msgPostEdit = content.Item2;
if (content.Item1.Length > ReportCutoffLength) if (content.Item1.Length > ReportCutoffLength)
{ {
before = string.Format(ReportCutoffNotify, ReportCutoffLength) msgCached = string.Format(ReportCutoffNotify, ReportCutoffLength)
+ content.Item1.Substring(ReportCutoffLength); + content.Item1.Substring(0, ReportCutoffLength);
} }
if (isDelete && content.Item2.Length > ReportCutoffLength) if (!isDelete && content.Item2.Length > ReportCutoffLength)
{ {
after = string.Format(ReportCutoffNotify, ReportCutoffLength) msgPostEdit = string.Format(ReportCutoffNotify, ReportCutoffLength)
+ content.Item2.Substring(ReportCutoffLength); + content.Item2.Substring(0, ReportCutoffLength);
} }
// Note: Value for ucb is null if cached user could not be determined // Note: Value for ucb is null if cached user could not be determined
@ -157,45 +164,44 @@ namespace Noikoio.RegexBot.Module.ModLogs
Fields = new System.Collections.Generic.List<EmbedFieldBuilder>(), Fields = new System.Collections.Generic.List<EmbedFieldBuilder>(),
Footer = new EmbedFooterBuilder() Footer = new EmbedFooterBuilder()
{ {
Text = (ucd == null ? "" : $"UID {ucd.UserId} - ") + $"MID {messageId}", Text = (ucd == null ? "UID: Unknown" : $"UID: {ucd.UserId}"),
IconUrl = _dClient.CurrentUser.GetAvatarUrl() IconUrl = _dClient.CurrentUser.GetAvatarUrl()
}, },
Timestamp = DateTimeOffset.Now Timestamp = DateTimeOffset.UtcNow
}; };
if (isDelete) if (isDelete)
{ {
eb.Author.Name = "Message deleted by ";
eb.Color = new Color(0x9b9b9b); eb.Color = new Color(0x9b9b9b);
eb.Description = content.Item1; eb.Description = msgCached;
eb.Author.Name = "Message deleted by "
+ ucd == null ? "unknown user" : $"{ucd.Username}#{ucd.Discriminator}";
} }
else else
{ {
eb.Color = new Color(8615955); eb.Author.Name = "Message edited by ";
eb.Color = new Color(0x837813);
eb.Fields.Add(new EmbedFieldBuilder() eb.Fields.Add(new EmbedFieldBuilder()
{ {
Name = "Before", Name = "Before",
Value = before Value = msgCached
}); });
eb.Fields.Add(new EmbedFieldBuilder() eb.Fields.Add(new EmbedFieldBuilder()
{ {
Name = "After", Name = "After",
Value = after Value = msgPostEdit
}); });
} }
if (ucd != null) eb.Fields.Add(new EmbedFieldBuilder() eb.Author.Name += ucd == null ? "unknown user" : $"{ucd.Username}#{ucd.Discriminator}";
{
Name = "Username", var context = new StringBuilder();
Value = $"<@!{ucd.UserId}>", if (ucd != null) context.AppendLine($"Username: <@!{ucd.UserId}>");
IsInline = true context.AppendLine($"Channel: <#{chInfo.Id}> #{chInfo.Name}");
}); context.Append($"Message ID: {messageId}");
eb.Fields.Add(new EmbedFieldBuilder() eb.Fields.Add(new EmbedFieldBuilder()
{ {
Name = "Channel", Name = "Context",
Value = $"<#{chInfo.Id}>\n#{chInfo.Name}", Value = context.ToString()
IsInline = true
}); });
return eb; return eb;
@ -233,21 +239,13 @@ namespace Noikoio.RegexBot.Module.ModLogs
{ {
using (var db = await RegexBot.Config.GetOpenDatabaseConnectionAsync()) using (var db = await RegexBot.Config.GetOpenDatabaseConnectionAsync())
{ {
// No upsert. Delete, then add.
using (var t = db.BeginTransaction())
{
using (var c = db.CreateCommand())
{
c.CommandText = "DELETE FROM " + TableMessage + " WHERE message_id = @MessageId";
c.Parameters.Add("@MessageId", NpgsqlDbType.Bigint).Value = msg.Id;
c.Prepare();
await c.ExecuteNonQueryAsync();
}
using (var c = db.CreateCommand()) using (var c = db.CreateCommand())
{ {
c.CommandText = "INSERT INTO " + TableMessage c.CommandText = "INSERT INTO " + TableMessage
+ " (message_id, author_id, guild_id, channel_id, created_ts, edited_ts, message) VALUES" + " (message_id, author_id, guild_id, channel_id, created_ts, edited_ts, message) VALUES"
+ "(@MessageId, @UserId, @GuildId, @ChannelId, @Date, @Edit, @Message)"; + " (@MessageId, @UserId, @GuildId, @ChannelId, @Date, @Edit, @Message)"
+ " ON CONFLICT (message_id) DO UPDATE"
+ " SET message = EXCLUDED.message, edited_ts = EXCLUDED.edited_ts";
c.Parameters.Add("@MessageId", NpgsqlDbType.Bigint).Value = msg.Id; c.Parameters.Add("@MessageId", NpgsqlDbType.Bigint).Value = msg.Id;
c.Parameters.Add("@UserId", NpgsqlDbType.Bigint).Value = msg.Author.Id; c.Parameters.Add("@UserId", NpgsqlDbType.Bigint).Value = msg.Author.Id;
c.Parameters.Add("@GuildId", NpgsqlDbType.Bigint).Value = ((SocketGuildUser)msg.Author).Guild.Id; c.Parameters.Add("@GuildId", NpgsqlDbType.Bigint).Value = ((SocketGuildUser)msg.Author).Guild.Id;
@ -263,7 +261,6 @@ namespace Noikoio.RegexBot.Module.ModLogs
} }
} }
} }
}
catch (NpgsqlException ex) catch (NpgsqlException ex)
{ {
await _outLog($"SQL error in {nameof(AddOrUpdateCacheItemAsync)}: " + ex.Message); await _outLog($"SQL error in {nameof(AddOrUpdateCacheItemAsync)}: " + ex.Message);

View file

@ -24,7 +24,7 @@ namespace Noikoio.RegexBot.Module.ModLogs
_msgCacheInstance = new MessageCache(client, Log, GetConfig); _msgCacheInstance = new MessageCache(client, Log, GetConfig);
throw new NotImplementedException(); //throw new NotImplementedException();
} }
[ConfigSection("ModLogs")] [ConfigSection("ModLogs")]