繁体   English   中英

如何使用动态名称设置 Discord Bot 命令

[英]How do I setup Discord Bot commands with dynamic names

所以我正在尝试为我朋友的不和谐服务器制作一个声音机器人。 我们之前从另一位朋友那里得到了一个,但由于某种原因它停止了工作,我无法获得源代码。 旧机器人必须播放声音的命令只是前缀“?” 然后是声音前的名称。 “?youdied”我正在使用 DSharpPlus 库(v3.2.3)在 C# 中编写这个。 这是设置命令的外观:

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

由于旧机器人没有使用静态命令来播放声音,只是可以添加或删除的声音名称,因此不会真正使用命令。 但是,我不能只将名称留空并拥有“公共异步任务”。 可以只使用“播放”命令并将声音名称作为“?play youdied”之类的参数传递,但是因为我知道这可能是可能的(尽管可能不是C#)我想尝试一下,如果有办法.

有关我的设置的更多信息:我有一个 Program.cs 文件,其中包含初始化 DiscordClient、CommandsNextModule 和 InteractivityModule 的所有内容。 然后是定义方法的 BasicCommands.Modules.cs 文件,这些方法实际上是由机器人完成的。

在这种情况下,您可能希望利用MessageCreated事件。 在客户端设置期间定义它。

_discordClient.MessageCreated += Client_MessageCreated;

然后捕获消息:

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
   }    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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