From 4187f4269aa539090e9edf2c630789e2ead08cc7 Mon Sep 17 00:00:00 2001 From: Vilmos Zsombor TANCZOS Date: Mon, 28 Apr 2025 14:17:01 +0200 Subject: [PATCH] hello and send messages --- Commands/HelloModule.cs | 14 ++++++++++++++ Commands/SendModule.cs | 43 +++++++++++++++++++++++++++++++++++++++++ Program.cs | 31 +++++++++++++++++++++++++++++ k-dc-bot-cs.csproj | 1 + 4 files changed, 89 insertions(+) create mode 100644 Commands/HelloModule.cs create mode 100644 Commands/SendModule.cs diff --git a/Commands/HelloModule.cs b/Commands/HelloModule.cs new file mode 100644 index 0000000..9f4d3d1 --- /dev/null +++ b/Commands/HelloModule.cs @@ -0,0 +1,14 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; + +namespace KBot; + +public class HelloModule : BaseCommandModule +{ + [Command("greet")] + [RequireRoles(RoleCheckMode.All, "Admin")] + public async Task GreetCommand(CommandContext ctx) + { + await ctx.RespondAsync("Greetings! Thank you for executing me!"); + } +} diff --git a/Commands/SendModule.cs b/Commands/SendModule.cs new file mode 100644 index 0000000..57d673c --- /dev/null +++ b/Commands/SendModule.cs @@ -0,0 +1,43 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using DSharpPlus.Entities; + +namespace KBot; + +public class SendModule : BaseCommandModule +{ + [ + Command("send"), + Description("Lets you send a message to a channel, user, or in reply to a message"), + RequireRoles(RoleCheckMode.All, "Admin") + ] + public async Task SendChannelCommand( + CommandContext ctx, + [Description("The channel the message will be sent to")] DiscordChannel channel, + [RemainingText, Description("The message sent")] string content + ) + { + await ctx.Client.SendMessageAsync(channel, content); + } + + [Command("send"), RequireRoles(RoleCheckMode.All, "Admin")] + public async Task SendDmCommand( + CommandContext ctx, + [Description("The member you want to send the message to")] DiscordMember member, + [RemainingText, Description("The message sent")] string content + ) + { + var channel = await member.CreateDmChannelAsync(); + await ctx.Client.SendMessageAsync(channel, content); + } + + [Command("send"), RequireRoles(RoleCheckMode.All, "Admin")] + public async Task SendReplyCommand( + CommandContext ctx, + [Description("The message you want to reply to")] DiscordMessage message, + [RemainingText, Description("The message sent")] string content + ) + { + await message.RespondAsync(content); + } +} diff --git a/Program.cs b/Program.cs index b855ace..69cba7f 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,8 @@ using dotenv.net; using dotenv.net.Utilities; using DSharpPlus; +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Exceptions; namespace KBot; @@ -19,6 +21,35 @@ class Program } ); + var commands = discord.UseCommandsNext( + new CommandsNextConfiguration() { StringPrefixes = new[] { "," } } + ); + + commands.RegisterCommands(); + commands.RegisterCommands(); + + commands.CommandErrored += async (s, e) => + { + if (e.Exception is CommandNotFoundException) + { + await e.Context.RespondAsync("Unknown command!"); + } + else if (e.Exception is ChecksFailedException checksFailed) + { + await e.Context.RespondAsync("You don't have permission to use this command!"); + } + else if (e.Exception is ArgumentException) + { + var command = commands.FindCommand("help " + e.Command?.Name, out string? str); + var ctx = commands.CreateContext(e.Context.Message, "", command, str); + await command?.ExecuteAsync(ctx); + } + else + { + await e.Context.RespondAsync($"An error occurred: {e.Exception.GetType().Name}"); + } + }; + await discord.ConnectAsync(new DSharpPlus.Entities.DiscordActivity("bing chillin")); await Task.Delay(-1); } diff --git a/k-dc-bot-cs.csproj b/k-dc-bot-cs.csproj index aad1e59..67a2d4b 100644 --- a/k-dc-bot-cs.csproj +++ b/k-dc-bot-cs.csproj @@ -11,6 +11,7 @@ +