Filter everyone/here pings if the proxy sender doesn't have permission

This commit is contained in:
Ske 2019-07-28 15:38:28 +02:00
parent 2217b87afe
commit 59dbc5368b
2 changed files with 18 additions and 2 deletions

View File

@ -88,6 +88,9 @@ namespace PluralKit.Bot
public async Task HandleMessageAsync(IMessage message)
{
// Bail early if this isn't in a guild channel
if (!(message.Channel is IGuildChannel)) return;
IEnumerable<ProxyDatabaseResult> results;
using (var conn = await _conn.Obtain())
{
@ -109,10 +112,13 @@ namespace PluralKit.Bot
if (match.InnerText.Trim().Length == 0 && message.Attachments.Count == 0)
return;
// Sanitize @everyone, but only if the original user wouldn't have permission to
var messageContents = SanitizeEveryoneMaybe(message, match.InnerText);
// Fetch a webhook for this channel, and send the proxied message
var webhook = await _webhookCache.GetWebhook(message.Channel as ITextChannel);
var avatarUrl = match.Member.AvatarUrl ?? match.System.AvatarUrl;
var hookMessageId = await ExecuteWebhook(webhook, match.InnerText, match.ProxyName, avatarUrl, message.Attachments.FirstOrDefault());
var hookMessageId = await ExecuteWebhook(webhook, messageContents, match.ProxyName, avatarUrl, message.Attachments.FirstOrDefault());
// Store the message in the database, and log it in the log channel (if applicable)
await _messageStorage.Store(message.Author.Id, hookMessageId, message.Channel.Id, message.Id, match.Member);
@ -127,6 +133,13 @@ namespace PluralKit.Bot
} catch (HttpException) {} // If it's already deleted, we just swallow the exception
}
private static string SanitizeEveryoneMaybe(IMessage message, string messageContents)
{
var senderPermissions = ((IGuildUser) message.Author).GetPermissions(message.Channel as IGuildChannel);
if (!senderPermissions.MentionEveryone) return messageContents.SanitizeEveryone();
return messageContents;
}
private async Task<bool> EnsureBotPermissions(ITextChannel channel)
{
var guildUser = await channel.Guild.GetCurrentUserAsync();

View File

@ -83,6 +83,9 @@ namespace PluralKit.Bot
public static string Sanitize(this string input) =>
Regex.Replace(Regex.Replace(input, "<@[!&]?(\\d{17,19})>", "<\\@$1>"), "@(everyone|here)", "@\u200B$1");
public static string SanitizeEveryone(this string input) =>
Regex.Replace(input, "@(everyone|here)", "@\u200B$1");
public static async Task<ChannelPermissions> PermissionsIn(this IChannel channel)
{
switch (channel)