作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我了解不和谐机器人如何读取常规用户输入的消息并使用
进行响应if(message.content.toLowerCase().includes('cyber'))
message.channel.send("Key Word Detected ");
但如果它是嵌入的,它不会读取该消息。请帮助我更改该设置,以在嵌入消息中查找关键字并引发机器人的响应。
最佳答案
MessageEmbed
中的文本可以位于author
、description
、footer
、message.content
和title
中。它们也可以位于每个字段内,因此可能需要检查所有这些内容。
这是您可以使用的一个小函数(我知道这看起来很困惑,但这只是因为有很多逻辑运算符):
/*
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/
我是一名优秀的程序员,十分优秀!