Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 让机器人使用player命令发送嵌入_Node.js_Discord.js - Fatal编程技术网

Node.js 让机器人使用player命令发送嵌入

Node.js 让机器人使用player命令发送嵌入,node.js,discord.js,Node.js,Discord.js,我再次需要帮助创建我的discord机器人。我正在尝试让机器人使用命令发送嵌入。我的代码太复杂,无法通过这个消息发送,因为我有很多额外的函数,这些函数的作用是命令,所以我只想说一下命令应该是什么样子 /embed [title]; [description] 在标题和描述之前 setAuthor(`${message.author.username}`, message.author.displayAvatarURL) 所以嵌入的作者会出现。你知道怎么做吗 首先,以下是如何使用regex解析

我再次需要帮助创建我的discord机器人。我正在尝试让机器人使用命令发送嵌入。我的代码太复杂,无法通过这个消息发送,因为我有很多额外的函数,这些函数的作用是命令,所以我只想说一下命令应该是什么样子

/embed [title]; [description]
在标题和描述之前

setAuthor(`${message.author.username}`, message.author.displayAvatarURL)

所以嵌入的作者会出现。你知道怎么做吗

首先,以下是如何使用regex解析命令中的文本:

case /^\/embed \[[\w ]+\]; \[[\w ]+\]$/.test(command):
    sendEmbed(message);
    break;
这(
/^\/embed\[\w]+\];\[\w]+\]$/
)可以分解如下:

  • 开头的
    /^
    和结尾的
    $/
    表示我们正在尝试匹配整个字符串/命令
  • /embed
    与确切文本匹配
    “embed”
  • \[[\w]+\]
    用于标题和说明,并匹配方括号内的文本,其中文本为字母(大写或小写)、数字、下划线或空格。如果您需要更多字符,如
    “!”
    “-”
    ,我可以向您演示如何添加这些字符
  • .test(command)
    正在测试正则表达式是否与命令中的文本匹配,并返回布尔值(
    true
    /
    false
现在,您将正则表达式检查代码放入消息/命令侦听器中,然后调用send embed函数(我将其命名为
sendEmbed
),如下所示:

// set message listener 
client.on('message', message => {
    let command = message.content;

    // match commands like /embed [title]; [description]
    // first \w+ is for the title, 2nd is for description
    if ( /^\/embed \[[\w ]+\]; \[[\w ]+\]$/.test(command) )
        sendEmbed(message);
});

function sendEmbed(message) {
    let command = message.content;
    let channel = message.channel;
    let author = message.author;

    // get title string coordinates in command
    let titleStart = command.indexOf('[');
    let titleEnd = command.indexOf(']');
    let title = command.substr(titleStart + 1, titleEnd - titleStart - 1);

    // get description string coordinates in command
    // -> (start after +1 so we don't count '[' or ']' twice)
    let descStart = command.indexOf('[', titleStart + 1);
    let descEnd = command.indexOf(']', titleEnd + 1);
    let description = command.substr(descStart + 1, descEnd - descStart - 1);

    // next create rich embed
    let embed = new Discord.RichEmbed({
        title: title,
        description: description
    });

    // set author based of passed-in message
    embed.setAuthor(author.username, author.displayAvatarURL);

    // send embed to channel
    channel.send(embed);
}
const embed = new MessageEmbed()
  .setTitle(title)
  .setDescription(description)
  .setAuthor(author.username, author.displayAvatarURL);
如果你有问题,请告诉我

2021年1月更新

在最新版本的Discord js(自2021年1月起)中,您必须创建如下嵌入:

// set message listener 
client.on('message', message => {
    let command = message.content;

    // match commands like /embed [title]; [description]
    // first \w+ is for the title, 2nd is for description
    if ( /^\/embed \[[\w ]+\]; \[[\w ]+\]$/.test(command) )
        sendEmbed(message);
});

function sendEmbed(message) {
    let command = message.content;
    let channel = message.channel;
    let author = message.author;

    // get title string coordinates in command
    let titleStart = command.indexOf('[');
    let titleEnd = command.indexOf(']');
    let title = command.substr(titleStart + 1, titleEnd - titleStart - 1);

    // get description string coordinates in command
    // -> (start after +1 so we don't count '[' or ']' twice)
    let descStart = command.indexOf('[', titleStart + 1);
    let descEnd = command.indexOf(']', titleEnd + 1);
    let description = command.substr(descStart + 1, descEnd - descStart - 1);

    // next create rich embed
    let embed = new Discord.RichEmbed({
        title: title,
        description: description
    });

    // set author based of passed-in message
    embed.setAuthor(author.username, author.displayAvatarURL);

    // send embed to channel
    channel.send(embed);
}
const embed = new MessageEmbed()
  .setTitle(title)
  .setDescription(description)
  .setAuthor(author.username, author.displayAvatarURL);

请参见嵌入到文档中的示例。其他一切都应该是一样的。

如果我的答案有帮助,请告诉我!您可以编辑它以匹配最新版本的discord吗js@Quark-我更新了问题,现在应该可以了:)如果你遇到任何问题,请告诉我