首页 > 解决方案 > 使用用户的参数创建嵌入

问题描述

我正在尝试创建一个命令,该命令在嵌入中传递消息作者给出的参数。

这是代码

        const args = message.content.split(", ");

        var titleargs = args[1]
        var descriptionargs = args[2]
        var footerargs = args[3]
        {
          {
              var myInfo = new Discord.MessageEmbed()
                  .setTitle(titleargs)
                  .setDescription(descriptionargs)
                  .setFooter(footerargs)
                  .setColor(0xff0000)
  
  
                  message.channel.send(myInfo);
  
          }
      }

此代码有效,但我不想将第一个“,”放在前缀和命令之后

我应该改变什么?

编辑:我正在使用命令处理

标签: javascriptnode.jsdiscorddiscord.js

解决方案


您可以从字符串中删除前缀和命令,然后用,. 检查以下代码段:

const prefix = '&'
const command = 'announce'
const message = {
  content: '&announce a 1, b 2, c 3'
}

const args = message.content
  // remove the prefix and the command
  .slice(prefix.length + command.length)
  .split(',')
  // remove extra whitespaces
  .map(s => s.trim())

console.log(args)

您还可以解构titledescriptionfooterargs:

const args = message.content
  .slice(prefix.length + command.length)
  .split(',')
  .map((s) => s.trim())

const [title, description, footer] = args

const myInfo = new Discord.MessageEmbed()
  .setTitle(title)
  .setDescription(description)
  .setFooter(footer)
  .setColor(0xff0000)

message.channel.send(myInfo)

在此处输入图像描述


推荐阅读