在消息中使用特定代码字作为 arguments/prefixes?
Use specific codewords as arguments/prefixes in a message?
我在 JavaScript 和 discord.js@v12 一起工作。我在他们的支持 Discord 中问了这个问题,但由于问题与 JavaScript 比 Discordjs 更相关,我在这里!
我们开始吧。我想在一个单条消息中填写一个RichEmbed。此 RichEmbed 将包含标题、说明和 1 字段。 RichEmbeds中可以有多个字段,所以我想弄清楚我只需要1.
现在,我意识到我可以使用以下方法 a) 使用 awaitMessages 并一次填写一条消息 b) 用 space 分隔消息中的参数并使每个参数 text-like-this
c) 用 [=15 分隔消息中的参数=] 并使每一行成为一个完整的论点。然而,这些对我来说都不是很吸引人。我也有一些人要用我的命令,他们不是很懂技术,因此可能不明白命令的用法,除非有非常详细的解释,当我不在时效率不高。
我过去使用过 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!
}
有了这个,我可以轻松地替换 first 参数,其中包含 title:
、desc:
或 field:
,但是我会喜欢浏览整个消息 (message.content
)。之后,我想找到哪个参数中有 title:
。然后我想在 aftertitle
之后输入每个参数,直到它达到 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
而不是 space/whitespace.
来拆分参数
const msgArray = message.content.split(/ +/g)
const args = message.content.slice(prefix.length + msgArray[0].length).trim().split(`\n`)
因为我用换行符来分割参数,所以我可以在我的参数中使用空格而不用担心它们会变得奇怪。
然后我检查每个参数是否以 Title
、Description
或 Field
、 或 开头none 个。然后,我遍历每个参数(从 1 到 3 人类术语)并检查它们是否以这三个中的任何一个开始,然后从那里。 注意这是手动的,如果我想添加另一个字段或图像,则兼容性不佳,但这适用于我的情况,因此,我现在已经解决了它。
今天下午我意识到我可以简单地循环遍历所有 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,
},
],
}})
}
对于那些想要查看原始结果代码的人,请查看此 pastebin 结果。
我在 JavaScript 和 discord.js@v12 一起工作。我在他们的支持 Discord 中问了这个问题,但由于问题与 JavaScript 比 Discordjs 更相关,我在这里!
我们开始吧。我想在一个单条消息中填写一个RichEmbed。此 RichEmbed 将包含标题、说明和 1 字段。 RichEmbeds中可以有多个字段,所以我想弄清楚我只需要1.
现在,我意识到我可以使用以下方法 a) 使用 awaitMessages 并一次填写一条消息 b) 用 space 分隔消息中的参数并使每个参数 text-like-this
c) 用 [=15 分隔消息中的参数=] 并使每一行成为一个完整的论点。然而,这些对我来说都不是很吸引人。我也有一些人要用我的命令,他们不是很懂技术,因此可能不明白命令的用法,除非有非常详细的解释,当我不在时效率不高。
我过去使用过 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!
}
有了这个,我可以轻松地替换 first 参数,其中包含 title:
、desc:
或 field:
,但是我会喜欢浏览整个消息 (message.content
)。之后,我想找到哪个参数中有 title:
。然后我想在 aftertitle
之后输入每个参数,直到它达到 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
而不是 space/whitespace.
const msgArray = message.content.split(/ +/g)
const args = message.content.slice(prefix.length + msgArray[0].length).trim().split(`\n`)
因为我用换行符来分割参数,所以我可以在我的参数中使用空格而不用担心它们会变得奇怪。
然后我检查每个参数是否以 Title
、Description
或 Field
、 或 开头none 个。然后,我遍历每个参数(从 1 到 3 人类术语)并检查它们是否以这三个中的任何一个开始,然后从那里。 注意这是手动的,如果我想添加另一个字段或图像,则兼容性不佳,但这适用于我的情况,因此,我现在已经解决了它。
今天下午我意识到我可以简单地循环遍历所有 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,
},
],
}})
}
对于那些想要查看原始结果代码的人,请查看此 pastebin 结果。