Don't attempt to send log messages if the bot doesn't have permissions

This commit is contained in:
Ske 2020-02-22 01:54:10 +01:00
parent 493f7b12e5
commit 8d3be79d33
4 changed files with 15 additions and 11 deletions

View File

@ -128,7 +128,7 @@ namespace PluralKit.Bot {
foreach (var channel in await guild.GetTextChannelsAsync())
{
// TODO: do we need to hide channels here to prevent info-leaking?
var perms = await channel.PermissionsIn();
var perms = channel.PermissionsIn();
// We use a bitfield so we can set individual permission bits in the loop
ulong missingPermissionField = 0;

View File

@ -33,9 +33,14 @@ namespace PluralKit.Bot {
// Bail if we can't find the channel
if (!(await _client.GetChannelAsync(guildCfg.Value.LogChannel.Value) is ITextChannel logChannel)) return;
// Bail if we don't have permission to send stuff here
if (!logChannel.HasPermission(ChannelPermission.SendMessages) || !logChannel.HasPermission(ChannelPermission.EmbedLinks))
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);
}
}

View File

@ -286,7 +286,7 @@ namespace PluralKit.Bot
// Finally remove the original reaction (if we can)
var user = await _client.Rest.GetUserAsync(userWhoReacted);
if (user != null && await realMessage.Channel.HasPermission(ChannelPermission.ManageMessages))
if (user != null && realMessage.Channel.HasPermission(ChannelPermission.ManageMessages))
await realMessage.RemoveReactionAsync(reactedEmote, user);
}
@ -316,7 +316,7 @@ namespace PluralKit.Bot
// And finally remove the original reaction (if we can)
var msgObj = await message.GetOrDownloadAsync();
if (await msgObj.Channel.HasPermission(ChannelPermission.ManageMessages))
if (msgObj.Channel.HasPermission(ChannelPermission.ManageMessages))
await msgObj.RemoveReactionAsync(reactedEmote, user);
}

View File

@ -1,6 +1,5 @@
using System.Threading.Tasks;
using Discord;
using Discord;
using Discord.WebSocket;
namespace PluralKit.Bot
{
@ -10,7 +9,7 @@ namespace PluralKit.Bot
return $"{user.Username}#{user.Discriminator} ({user.Mention})";
}
public static async Task<ChannelPermissions> PermissionsIn(this IChannel channel)
public static ChannelPermissions PermissionsIn(this IChannel channel)
{
switch (channel)
{
@ -18,15 +17,15 @@ namespace PluralKit.Bot
return ChannelPermissions.DM;
case IGroupChannel _:
return ChannelPermissions.Group;
case IGuildChannel gc:
var currentUser = await gc.Guild.GetCurrentUserAsync();
case SocketGuildChannel gc:
var currentUser = gc.Guild.CurrentUser;
return currentUser.GetPermissions(gc);
default:
return ChannelPermissions.None;
}
}
public static async Task<bool> HasPermission(this IChannel channel, ChannelPermission permission) =>
(await PermissionsIn(channel)).Has(permission);
public static bool HasPermission(this IChannel channel, ChannelPermission permission) =>
PermissionsIn(channel).Has(permission);
}
}