- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个用于在某些游戏中进行交易的 Discord 机器人。到目前为止,我已经使用了大部分基本命令——!create 在 SQL 数据库中创建一个交易列表,!find 找到一个——但它只能在完全相同的词上找到它。我正在尝试做的是使搜索不那么具体,因此术语不必完全相同即可显示结果。我当前的代码非常复杂,不用说,非常破烂:
var searchTerms = args[1].split(" ");
var output = {};
for (var id in userData) {
for (var offer in userData[id].offers) {
var score = 0;
for (var key in searchTerms) {
if (offer.includes(key)) {
score ++;
}
}
if (score >= searchTerms.length / 2) {
output[id] = userData[id].offers[offer] + " - " + ((score / searchTerms.length) * 100) + "%";
}
}
}
if (output == {}) {
bot.sendMessage({
to: channelID,
message: 'No matching offers found.'
});
} else {
msg = ""
for (id in output) {
msg += '<@' + id + '> - ' + output[id] + " "
}
bot.sendMessage({
to: channelID,
message: Object.keys(output).length + ' offers found: ' + msg
});
}
我是 Javascript 的新手,所以我不太确定如何让它工作。感谢任何提示,谢谢!
最佳答案
看起来您要实现的是一种称为模糊搜索
的机制,用户可以使用拼写错误或近似字符串找到相似的结果。
(引用:https://en.wikipedia.org/wiki/Approximate_string_matching)
这对于编程初学者来说并不是一个容易实现的功能,要么数据库必须支持某种模糊查询,要么你必须先从数据库中获取所有数据,然后使用 JavaScript 模糊查询搜索库来实现这一点。
如果你还想这样做,我建议使用 Fuse.js , 几行就可以完成模糊搜索
//list to be searched
var books = [{
'ISBN': 'A',
'title': "Old Man's War",
'author': 'John Scalzi'
}, {
'ISBN': 'B',
'title': 'The Lock Artist',
'author': 'Steve Hamilton'
}]
// init the search
var options = {
keys: ['title', 'author'],
id: 'ISBN'
}
var fuse = new Fuse(books, options)
fuse.search('old')
// result
[
"A"
]
模糊搜索是一个复杂的计算机科学问题,如果您想更多地了解它以及 Fuse.js 是如何实现的,这里有一些有用的链接
关于javascript - Discord.js 搜索关键词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53065747/
当我在 repl.it 上运行我的 discord 机器人时,出现错误。在错误中,我在下面看到了一些 HTML 代码。 CloudFlare 似乎阻止了我的机器人访问 Discord。这是什么原因造成
我计划使用 python 制作一个不和谐的机器人。 discord.py 和 discord.py[voice] 有什么区别? 最佳答案 discord.py 和 discord[voice] 之间的
我正在尝试编写一个脚本来检测用户是否以及何时将他们在 discord 上的名称从“bob”更改为“.bob”之类的名称,它会禁止此更改并将其保留为“bob”。 if (user.nickname.st
我想使用 Discord Bot 向用户发送私有(private)消息。 用户与机器人不在同一台服务器上。 如果我可以使用 author.sendMessage,我该如何初始化(查找)作者变量? 我可
我正在尝试将某人添加到特定服务器,然后通过 DM 只用不和谐 ID 表示该人。 它的工作方式是,有人在网站上使用 Discord OAuth2 登录自己,登录后他应该被添加到特定服务器,然后机器人应该
我无法弄清楚如何使用类似于 /giphy、/xivdb 等的实时“反馈”来实现斜杠命令。没有公共(public) API 吗? ?我正在使用 discord.py,但我想这对于这个问题来说并不重要。
我正在尝试使用 Discord Bot 在 discord 服务器上创建一个类别,但我无法在 Internet 上找到该方法或其他内容。我还看了“discord.js.org”。然后我认为没有任何可能
我正在为桌面创建灯,当我的麦克风在 Discord 上处于事件状态时,该灯将为绿色,当我的麦克风静音时,该灯将为红色。所以我想从不和谐中获取我的麦克风状态。但我不知道怎么做。 当我不在进行语音聊天时,
我已经看到了您允许某个角色使用命令的变体。但是,我试图实现完全相反的效果:如何禁止某个角色使用命令。 我搜索了堆栈溢出,没有找到答案,也没有在 discord.py 官方文档上找到答案。任何形式的帮助
我开发了自己的简单 discord bot(使用 discord.js)并将其部署到节点服务器。 一切正常。 现在我想为它添加更多功能。在开发过程中,我想在本地进行测试(当然)。 问题是:我可以在本地
我正在使用 Discord.js 创建一个基本的 Discord 机器人。当机器人第一次启动时,我运行 client.guilds.array()获取机器人当前订阅的所有公会的列表。我将其保存到其他程
我想为付费成员(member)提供 Discord 服务器。我想我可以让他们通过 Stripe 付款,向他们发送付款邀请,然后定期运行 Python 脚本,该脚本检查我的所有 Discord 成员是否
我试图向我的机器人添加一些新功能,但它最终没有工作,所以我删除了新代码,现在它不会响应任何命令。它仍然在线,但正如我之前所说,它完全没有响应。我已经查看了代码,但找不到问题。我对编码机器人还是个新手,
我正在编写一个建议机器人,它应该将玩家的建议发送到我服务器中的建议 channel ,并在建议 channel 中用一些表情符号使用react。 问题是使用“消息”作为消息参数会对发送到触发代码的消息
我想创建一个清除命令,如果 example .clear @user#0000 100 它将清除@user#0000 发送的最后 100 条消息。这是我的代码: @commands.comma
所以,我想为我的机器人创建一个命令,它可以从给定的消息 ID 中获取所有反应并将这三个类别存储到数组中,对于每个 react : react 的名称(例如:smile:) 使用react的用户(例如
我正在尝试在 discord.js 上制作一个简单的右舷系统,删除消息部分让我很困惑。目前,如果一条消息获得星号,则会在右舷 channel 创建一个嵌入,而且至关重要的是,页脚是原始消息的 ID。我
我最近启动了一个新的 discord.js 机器人,但出现“无效 token ”错误。这不可能是因为 async 功能,因为我只启动了机器人并且我有 0 个命令,上次发生这种情况是当我试图在 hero
我正在尝试向我的机器人添加一条命令,该命令会回复用户邀请到服务器的总人数 我的代码: if message.content.startswith('!invites'): totalInvit
我在一些服务器/公会中添加了一个 Discord 机器人。我可以使用此处提供的资源获得特定公会: https://discord.com/developers/docs/resources/guild
我是一名优秀的程序员,十分优秀!