From 0653e33ff7ea9715fe66ffe300906d74b9846d0e Mon Sep 17 00:00:00 2001 From: PoisnFang Date: Tue, 6 Jun 2023 20:01:03 -0700 Subject: [PATCH] Updated permissions --- Commands/SlashCommands.cs | 17 ++++++++++------- PoisnCopy.sln | 6 +++--- Program.cs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Commands/SlashCommands.cs b/Commands/SlashCommands.cs index f0cb9d9..96e4d9c 100644 --- a/Commands/SlashCommands.cs +++ b/Commands/SlashCommands.cs @@ -1,6 +1,5 @@ using CsvHelper; using DSharpPlus; -using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.Entities; using DSharpPlus.SlashCommands; using DSharpPlus.SlashCommands.Attributes; @@ -11,13 +10,17 @@ namespace PoisnCopy; [SlashRequirePermissions( Permissions.SendMessages - & Permissions.AccessChannels - & Permissions.ManageChannels - & Permissions.ReadMessageHistory - & Permissions.EmbedLinks - & Permissions.AttachFiles + | Permissions.AccessChannels + | Permissions.ManageChannels + | Permissions.ReadMessageHistory + | Permissions.EmbedLinks + | Permissions.AttachFiles + | Permissions.ReadMessageHistory + | Permissions.CreatePrivateThreads + | Permissions.CreatePublicThreads + | Permissions.ManageThreads + | Permissions.SendMessagesInThreads )] -[Hidden] public class SlashCommands : ApplicationCommandModule { [SlashCommand("copychannel", "Copy a channel")] diff --git a/PoisnCopy.sln b/PoisnCopy.sln index 07d010b..9e4dc06 100644 --- a/PoisnCopy.sln +++ b/PoisnCopy.sln @@ -1,9 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30611.23 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33723.286 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoisnCopy", "PoisnCopy.csproj", "{071E04E6-CEDB-414B-BABA-E2AEAF939586}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoisnCopy", "PoisnCopy.csproj", "{071E04E6-CEDB-414B-BABA-E2AEAF939586}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Program.cs b/Program.cs index 65c4a2f..1f11b57 100644 --- a/Program.cs +++ b/Program.cs @@ -1,15 +1,21 @@ using DSharpPlus; +using DSharpPlus.CommandsNext.Exceptions; +using DSharpPlus.CommandsNext; +using DSharpPlus.Entities; using DSharpPlus.Interactivity; using DSharpPlus.Interactivity.Enums; using DSharpPlus.Interactivity.Extensions; using DSharpPlus.SlashCommands; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using DSharpPlus.SlashCommands.EventArgs; +using Microsoft.Extensions.Logging; namespace PoisnCopy; internal class Program { + public readonly EventId BotEventId = new(42, "ControlCopy"); private CancellationTokenSource _cts { get; set; } private IConfigurationRoot _config; @@ -56,6 +62,7 @@ private async Task InitBot(string[] args) var slash = _discord.UseSlashCommands( new SlashCommandsConfiguration { Services = services } ); + slash.SlashCommandErrored += Commands_CommandErrored; slash.RegisterCommands(); @@ -93,4 +100,30 @@ private ServiceProvider BuildServices() .AddSingleton(_discord); return deps.BuildServiceProvider(); } + + private async Task Commands_CommandErrored( + SlashCommandsExtension sender, + SlashCommandErrorEventArgs e + ) + { + e.Context.Client.Logger.LogError( + BotEventId, + $"{e.Context.User.Username} tried executing '{e.Context.CommandName ?? ""}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? ""}", + DateTime.Now + ); + + if (e.Exception is SlashExecutionChecksFailedException ex) + { + var emoji = DiscordEmoji.FromName(e.Context.Client, ":no_entry:"); + + var embed = new DiscordEmbedBuilder + { + Title = "Access denied", + Description = + $"{emoji} You do not have the permissions required to execute this command.", + Color = new DiscordColor(0xFF0000) // red + }; + await e.Context.CreateResponseAsync(embed); + } + } } \ No newline at end of file