Add channel blacklists for logging and proxying

Necessary database migrations for this commit:
    alter table servers add column log_blacklist bigint[] not null default array[]::bigint[];
    alter table servers add column blacklist bigint[] not null default array[]::bigint[];
This commit is contained in:
Ske
2019-11-03 19:15:50 +01:00
parent 0cdd99d195
commit 378cba09e5
7 changed files with 142 additions and 36 deletions

View File

@@ -18,21 +18,21 @@ namespace PluralKit.Bot {
_logger = logger.ForContext<LogChannelService>();
}
public async Task LogMessage(PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, IGuildChannel originalChannel, IUser sender, string content) {
var logChannel = await GetLogChannel(originalChannel.Guild);
if (logChannel == null) return;
public async Task LogMessage(PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, IGuildChannel originalChannel, IUser sender, string content)
{
var guildCfg = await _data.GetOrCreateGuildConfig(originalChannel.GuildId);
// Bail if logging is disabled either globally or for this channel
if (guildCfg.LogChannel == null) return;
if (guildCfg.LogBlacklist.Contains(originalChannel.Id)) return;
// Bail if we can't find the channel
if (!(await _client.GetChannelAsync(guildCfg.LogChannel.Value) is ITextChannel logChannel)) return;
var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, originalMsgId, sender, content, originalChannel);
var url = $"https://discordapp.com/channels/{originalChannel.GuildId}/{originalChannel.Id}/{messageId}";
await logChannel.SendMessageAsync(text: url, embed: embed);
}
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;
}
}
}

View File

@@ -80,17 +80,20 @@ namespace PluralKit.Bot
public async Task HandleMessageAsync(IMessage message)
{
// Bail early if this isn't in a guild channel
if (!(message.Channel is ITextChannel)) return;
var results = await _cache.GetResultsFor(message.Author.Id);
if (!(message.Channel is ITextChannel channel)) return;
// Find a member with proxy tags matching the message
var results = await _cache.GetResultsFor(message.Author.Id);
var match = GetProxyTagMatch(message.Content, results);
if (match == null) return;
// And make sure the channel's not blacklisted from proxying.
var guildCfg = await _data.GetOrCreateGuildConfig(channel.GuildId);
if (guildCfg.Blacklist.Contains(channel.Id)) return;
// We know message.Channel can only be ITextChannel as PK doesn't work in DMs/groups
// Afterwards we ensure the bot has the right permissions, otherwise bail early
if (!await EnsureBotPermissions(message.Channel as ITextChannel)) return;
if (!await EnsureBotPermissions(channel)) return;
// Can't proxy a message with no content and no attachment
if (match.InnerText.Trim().Length == 0 && message.Attachments.Count == 0)
@@ -114,7 +117,7 @@ namespace PluralKit.Bot
// Execute the webhook itself
var hookMessageId = await _webhookExecutor.ExecuteWebhook(
(ITextChannel) message.Channel,
channel,
proxyName, avatarUrl,
messageContents,
message.Attachments.FirstOrDefault()