Discord.js 机器人 // 说命令

新手上路,请多包涵

我已经编写了一个“say 命令”,它应该在我每次输入 -say 时执行一条消息。它工作正常,我只想将前缀设置为“?”而不是仅针对该命令的“-”,其他命令应该设置为主命令(“-”)。最重要的是,我希望它在输入命令后 _删除命令_,所以剩下的就是消息 [(e.g ?say hello --> (delete "?say hello" --> send message "hello" to text channel)]. 我还想在我的命令中 指定 文本通道,而不是仅仅将其设置为发送仅向一个特定频道发送消息 [e.g -say (message) (text channel)] 如果它说“完成”之类的话,那也很酷。并在约 5 秒后删除该确认。

所以这是代码:

 client.on('message', function(message) {

    if(message.author.bot) return;

    else if(isValidCommand(message, "say")) {

        let sendMessage = message.content.substring(4);

        let sendChannel = client.channels.cache.get('767374258492932106');

        sendChannel.send(sendMessage)
    }
});

在下文中,我将向您展示我的无效代码,试图将前缀设置为“?”但它没有执行,只说“声明或声明期待和遗漏或错误的标点符号..

 client.on('message', function(message) {
    if (['?'].every((prefix) => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    else if(isValidCommand(message, "say")) {

        let sendMessage = message.content.substring(4);

        let sendChannel = client.channels.cache.get('767374258492932106');

        sendChannel.send(sendMessage)
    }
});

如果有人帮助我,我将不胜感激。谢谢!

原文由 Deazy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 515
1 个回答

我不确定你想用什么来实现

    if (['?'].every((prefix) => {

但这就是我要做的

使用 args、substring 和 switch 来识别命令。

的用法

client.on('message', async message =>{
    if (message.author.bot) return;
    if (message.content.startsWith(prefix)) {
        let args = message.content.substring(prefix.length).split(" ");
        switch (args[0].toLowerCase()){
            case 'say': {
        let sendMessage = message.content.substring(prefix.length +args[0].length+ args[1].length + 2); //2 is accounting for the 2 space between prefix and # and prefix and the main content
                setTimeout(()=>{message.delete()},5000)
                let sendChannel = client.channels.cache.get(args[1]);
                sendChannel.send(sendMessage)
                break;
            }
        }
    }
    if (message.content.startsWith(otherPrefix))    {
        let args = message.content.substring(otherPrefix.length).split(" ");
        switch (args[0].toLowerCase()){
            case 'say': {
        // Do something else
                break;
            }
        }
    }
});

编辑:表扬的用法就像

!say #generalTextChannel abc

#generalTextChannel 标记频道的位置

或者

!say 767374258492932106 abc

其中 767374258492932106 是频道 ID

原文由 Bulaxy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题