Skip to content

Commit

Permalink
Updated permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
PizzaConsole committed Jun 7, 2023
1 parent 45e8d59 commit 0653e33
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
17 changes: 10 additions & 7 deletions Commands/SlashCommands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CsvHelper;
using DSharpPlus;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
Expand All @@ -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")]
Expand Down
6 changes: 3 additions & 3 deletions PoisnCopy.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 33 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,6 +62,7 @@ private async Task InitBot(string[] args)
var slash = _discord.UseSlashCommands(
new SlashCommandsConfiguration { Services = services }
);
slash.SlashCommandErrored += Commands_CommandErrored;

slash.RegisterCommands<SlashCommands>();

Expand Down Expand Up @@ -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 ?? "<unknown command>"}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? "<no 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);
}
}
}

0 comments on commit 0653e33

Please sign in to comment.