gpt4 book ai didi

javascript - 如何让不和谐机器人读取嵌入内容

转载 作者:行者123 更新时间:2023-12-02 03:26:34 25 4
gpt4 key购买 nike

我了解不和谐机器人如何读取常规用户输入的消息并使用

进行响应
if(message.content.toLowerCase().includes('cyber'))
message.channel.send("Key Word Detected ");

但如果它是嵌入的,它不会读取该消息。请帮助我更改该设置,以在嵌入消息中查找关键字并引发机器人的响应。

最佳答案

MessageEmbed 中的文本可以位于authordescriptionfootermessage.contenttitle中。它们也可以位于每个字段内,因此可能需要检查所有这些内容。
这是您可以使用的一个小函数(我知道这看起来很困惑,但这只是因为有很多逻辑运算符):

/*
message {Discord.Message}: the message you want to search in
target {string}: the string you're looking for
{
caseSensitive {boolean}: whether you want the search to be case case-sensitive
author {boolean}: whether you want to search in the author's name
description {boolean}: whether you want to search in the description
footer {boolean}: whether you want to search in the footer
title {boolean}: whether you want to search in the title
fields {boolean}: whether you want to search in the fields
}
*/
function findInMessage(message, target, {
caseSensitive = false,
author = false,
description = true,
footer = true,
title = true,
fields = true
}) {
if (!target || !message) return null;
let str = caseSensitive ? target : target.toLowerCase();

if ((caseSensitive && message.content.includes(str)) ||
(!caseSensitive && message.content.toLowerCase().includes(str))) return true;

for (let embed of message.embeds) {
if ((caseSensitive && (
(author && embed.author.includes(str)) ||
(description && embed.description.includes(str)) ||
(footer && embed.footer.includes(str)) ||
(title && embed.title.includes(str)))) ||
(!caseSensitive && (
(author && embed.author.toLowerCase().includes(str)) ||
(description && embed.description.toLowerCase().includes(str)) ||
(footer && embed.footer.toLowerCase().includes(str)) ||
(title && embed.title.toLowerCase().includes(str))))
) return true;

if (fields)
for (let field of embed.fields) {
if ((caseSensitive && [field.name, field.value].includes(str)) ||
(!caseSensitive && [field.name.toLowerCase(), field.value.toLowerCase()].includes(str))) return true;
}
}

return false;
}

当找到您输入的单词时,该函数返回 true;当未找到它时,返回 false;当其中之一时,返回 null缺少非可选参数。
您可以像这样使用它:

if (findInMessage(message, 'cyber')) message.channel.send("Key word detected.");

顶部有一些说明,希望有所帮助;)

关于javascript - 如何让不和谐机器人读取嵌入内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53201455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com