Check for permissions to look at the channel message history

This commit is contained in:
13xforever 2018-09-20 19:30:57 +05:00 committed by Roberto Anić Banić
parent fb46bc95c3
commit eef13b3d01
2 changed files with 20 additions and 5 deletions

View File

@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using NReco.Text;
@ -26,6 +27,13 @@ namespace CompatBot.EventHandlers
if (!NeedToSilence(args.Message))
return;
var botMember = args.Guild?.CurrentMember ?? args.Client.GetMember(args.Client.CurrentUser);
if (!args.Channel.PermissionsFor(botMember).HasPermission(Permissions.ReadMessageHistory))
{
await args.Message.ReactWithAsync(args.Client, DiscordEmoji.FromUnicode("🙅"), @"No can do, boss ¯\\_(ツ)\_/¯").ConfigureAwait(false);
return;
}
await args.Message.ReactWithAsync(args.Client, DiscordEmoji.FromUnicode("😟"), "Okay (._.)").ConfigureAwait(false);
var lastBotMessages = await args.Channel.GetMessagesBeforeAsync(args.Message.Id, 20, DateTime.UtcNow.AddMinutes(-5)).ConfigureAwait(false);
if (lastBotMessages.OrderByDescending(m => m.CreationTimestamp).FirstOrDefault(m => m.Author.IsCurrent) is DiscordMessage msg)

View File

@ -59,11 +59,18 @@ namespace CompatBot.Utils
public static async Task ReactWithAsync(this DiscordMessage message, DiscordClient client, DiscordEmoji emoji, string fallbackMessage = null, bool showBoth = false)
{
var canReact = message.Channel.IsPrivate || message.Channel.PermissionsFor(client.GetMember(message.Channel.Guild, client.CurrentUser)).HasPermission(Permissions.AddReactions);
if (canReact)
await message.CreateReactionAsync(emoji).ConfigureAwait(false);
if ((!canReact || showBoth) && !string.IsNullOrEmpty(fallbackMessage))
await message.Channel.SendMessageAsync(fallbackMessage).ConfigureAwait(false);
try
{
var canReact = message.Channel.IsPrivate || message.Channel.PermissionsFor(message.Channel.Guild.CurrentMember).HasPermission(Permissions.AddReactions);
if (canReact)
await message.CreateReactionAsync(emoji).ConfigureAwait(false);
if ((!canReact || showBoth) && !string.IsNullOrEmpty(fallbackMessage))
await message.Channel.SendMessageAsync(fallbackMessage).ConfigureAwait(false);
}
catch (Exception e)
{
client.DebugLogger.LogMessage(LogLevel.Warning, "", e.ToString(), DateTime.Now);
}
}
public static Task ReactWithAsync(this CommandContext ctx, DiscordEmoji emoji, string fallbackMessage = null, bool showBoth = false)