简体   繁体   中英

How do I setup Discord Bot commands with dynamic names

So I am trying to make a sound bot for my friend's discord server. We had one before from another friend but it stopped working for some reason and I haven't been able to get my hands on the source code. The command the old bot had to play sounds was just the prefix '?' and then the name of the sound ex. "?youdied" I'm writing this in C# using the DSharpPlus library (v3.2.3). This is how setting up the commands looks:

public class BasicCommands : IModule
{
[Command("alive")]
[Description("Simple command to test if the bot is running"]
public async Task Alive(CommandContext ctx)
    {
        //trigger the "Typing..." in discord
        await ctx.TriggerTypingAsync();

        //send the message to the channel the message was received from
        await ctx.RespondAsync("Shalom!");
    }
}

Since the old bot didnt use a static command for playing sounds, just sound names that could be added or removed, there wouldn't really be a command to use. However, I can't just leave the name blank and have the "public async Task". I could just use a "play" commands and pass the sound name in as a parameter like "?play youdied", but since I know it might be possible (although maybe not with C#) I want to try it if there is a way.

more information on my setup: I have a Program.cs file that has everything to initialize the DiscordClient, CommandsNextModule,and InteractivityModule. Then a BasicCommands.Modules.cs file where the methods are defined, which are actually done by the bot.

In this case, you'd probably want to tap into the MessageCreated event. Define this during client setup.

_discordClient.MessageCreated += Client_MessageCreated;

Then capture message:

private static Task Client_MessageCreated(DiscordClient sender, MessageCreateEventArgs e) {

if (string.IsNullOrEmpty(e.Message.Content))
        return Task.CompletedTask;

if (e.Message.Content.StartsWith("?"))
   {
        string sound = e.Message.Content.Substring(1);
        // do something now with the sound they entered
   }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM