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