javascript - 使用用户的参数创建嵌入
问题描述
我正在尝试创建一个命令,该命令在嵌入中传递消息作者给出的参数。
这是代码
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);
}
}
此代码有效,但我不想将第一个“,”放在前缀和命令之后
我应该改变什么?
编辑:我正在使用命令处理
解决方案
您可以从字符串中删除前缀和命令,然后用,
. 检查以下代码段:
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)
您还可以解构title
、description
和footer
args:
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)
推荐阅读
- java - LoadingQueue 实现,如果为空则加载条目
- .net-core - 仅禁用与 HealthCheck 相关的日志记录
- python-3.x - 解释器在这个程序中的工作
- c# - 使用 Linq 连续总和大于 0 的连续元素
- javascript - iOS 智能横幅导致底部工具栏与固定在屏幕底部的元素重叠
- pandas - 如何使用 pandas 合成表
- swift - 如何在 swift 上从 YouTube-ios-player-helper 获取当前时间?
- linux - Ciphertext-Policy Attribute-Based Encryption toolkit key-gen error
- python - Python中类内导入和类外导入之间的误解差异
- r - 警告:错误:“路径”在 Shiny App 中不存在