hello and send messages

This commit is contained in:
Vilmos Zsombor TANCZOS 2025-04-28 14:17:01 +02:00
parent 61b0fdf733
commit 4187f4269a
4 changed files with 89 additions and 0 deletions

14
Commands/HelloModule.cs Normal file
View file

@ -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!");
}
}

43
Commands/SendModule.cs Normal file
View file

@ -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);
}
}

View file

@ -1,6 +1,8 @@
using dotenv.net; using dotenv.net;
using dotenv.net.Utilities; using dotenv.net.Utilities;
using DSharpPlus; using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Exceptions;
namespace KBot; namespace KBot;
@ -19,6 +21,35 @@ class Program
} }
); );
var commands = discord.UseCommandsNext(
new CommandsNextConfiguration() { StringPrefixes = new[] { "," } }
);
commands.RegisterCommands<HelloModule>();
commands.RegisterCommands<SendModule>();
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 discord.ConnectAsync(new DSharpPlus.Entities.DiscordActivity("bing chillin"));
await Task.Delay(-1); await Task.Delay(-1);
} }

View file

@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="dotenv.net" Version="3.2.1" /> <PackageReference Include="dotenv.net" Version="3.2.1" />
<PackageReference Include="DSharpPlus" Version="4.5.1" /> <PackageReference Include="DSharpPlus" Version="4.5.1" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.5.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>