是否将特定码字用作消息中的参数/前缀



我使用JavaScriptdiscord.js@v12.我在他们对Discord的支持中问了这个问题,但由于这个问题与JavaScript比Discordjs更相关,我来了!

让我们开始吧。我想在中填写一条单个消息。此RichEmbed将包括标题、说明和1字段。RichEmbed中可以有多个字段,所以我想弄清楚我只需要1

现在,我意识到我可以使用以下方法来实现这一点a(使用awaitMessages并一次填写一条消息b(用空格分隔消息中的参数并使每个参数成为text-like-this

c(我过去曾使用过a(,事实证明它在快速工作流程中相当低效,如果我需要用很多句子进行长描述,b则毫无用处C是可用的,但同样,使用该命令的人在技术上不是";"有能力";因此可能会自己挣扎。我想把这个写得尽可能流利。

我也尝试过这种方法,我觉得它很有用,但可能会让人困惑:

/commandname Separating title / Description and / Field by a forward slash

所以我得出结论,我希望我的语法看起来像这样

/commandname title:My title! desc:This is the description field1:Field 1 and its title

编辑:我在发布后花了一些研究,发现我可以使用replace方法来获得上述结果。这是我目前的代码:

let embed_title
let embed_desc
let embed_field
let firstarg = args[1]
if (firstarg.includes(`title:`)) {
var split = firstarg.replace(`title:`, ``)
embed_title = split
// expected output with "/commandname title:Testing123!": /commandname Testing123!
} else if (firstarg.includes(`desc:`)) {
var split = firstarg.replace(`desc:`, ``)
embed_desc = split
// expected output with "/commandname desc:Testing123!": /commandname Testing123!
} else if (firstarg.includes(`field:`)) {
var split = firstarg.replace(`field:`, ``)
embed_field = split
// expected output with "/commandname field:Testing123!": /commandname Testing123!
}

有了这个,我可以很容易地替换第一个参数,该参数中有title:desc:field:,但我想浏览整个消息(message.content(。之后,我想找出哪个参数中有title:。然后,我想馈送title之后的每个参数,直到它到达desc:。重复此步骤,直到覆盖field

我使用不同的语法解决了我的问题。

而不是使用/command然后:

title:Title of message desc:Description of message field1:Field of message

我正在使用/command,然后:

title Title of new message
description Description of new message
field Field 1 of new message

我用换行符n而不是空格/空白分隔参数。

const msgArray = message.content.split(/ +/g)
const args = message.content.slice(prefix.length + msgArray[0].length).trim().split(`n`)

由于我用换行符分隔论点,所以它允许我在论点中使用空格,而不用担心它们会变得奇怪。

然后,我检查每个参数,如果它以TitleDescriptionField开头,都不是。然后,我迭代每个参数(从13(,并检查它们是否以这三个参数中的任何一个开头,然后从那里开始注意如果我想添加另一个字段或图像,这是手动的,并且没有极好的兼容性,但这适用于我的情况,因此,我现在已经解决了它

今天下午我意识到,我可以简单地循环浏览所有的args,找到哪一个包含某个关键字。在这个过程中,我还可以筛选出任何与某些关键字不匹配的东西。

完整代码如下:

var _title = ``
var _desc = ``
var _fieldvalue = ``
var _useless = ``
args.forEach((element, i) => {
i += 1
if (element.startsWith(`description `)) {
_desc = element.slice(12)
}else if (element.startsWith(`title `)) {
_title = element.slice(6)
}else if (element.startsWith(`field `)) {
_fieldvalue = element.slice(6)
}else{
_useless = element
}
})
if (!_title && !_desc) return message.channel.send(`Please include at least a title or a description by using `title` or `description`.`)
if (!_fieldvalue) {
message.channel.send({embed: {
title: _title || null,
description: _desc || null,
}})
} else {
message.channel.send({embed: {
title: _title || null,
description: _desc || null,
fields: [
{
name: `u200b`,
value: _fieldvalue,
},
],
}})
}

对于那些想要查看原始结果代码的人,请查看此粘贴结果

最新更新