【发布时间】:2021-03-14 02:35:46
【问题描述】:
我正在尝试为我的机器人创建一个 Urban Dictionary 命令,以便用户可以查找特定的单词或短语
(prefix)urban <args>
我正在使用 Commando 命令处理程序、节点获取和查询字符串。链接在底部。 这是我的代码:
const commando = require('discord.js-commando')
const { MessageEmbed } = require('discord.js')
const fetch = require('node-fetch')
const queryString = require('query-string')
module.exports = class UrbanDictionaryCommand extends commando.Command {
constructor(client) {
super(client, {
name: 'urbandictionary',
aliases: ['urban'],
group: 'misc',
memberName: 'urbandictionary',
description: 'Shows the urban dictionary entry for a word or phrase'
})
}
async run(message, args) {
if(!args) {
message.reply('You need to specify something to search')
return
}
const { list } = await fetch(`https://api.urbandictionary.com/v0/define?${args}`).then(response => response.json())
try {
const [answer] = list
const trim = (str, max) => ((str.length > max) ? `${str.slice(0, max - 3)}...` : str)
const embed = new MessageEmbed()
embed.setTitle(answer.word)
embed.setURL(answer.permalink)
embed.addFields({
name: 'Definition',
value: trim(answer.definition, 1024),
inline: false
}, {
name: 'Example',
value: trim(answer.example, 1024),
inline: false
}, {
name: 'Rating',
value: `${answer.thumbs_up} ???? || ${answer.thumbs_down} ????`,
inline: false
})
embed.setFooter(`Command issued by ${message.author.tag}`, message.author.displayAvatarURL())
message.channel.send(embed)
} catch (error) {
console.log(error)
message.channel.send(`No results found for ${args}`)
return
}
}
}
当我在我的 Discord 服务器中执行命令时,我收到此错误消息
TypeError: list is not iterable
at UrbanDictionaryCommand.run (C:\Users\Owner\OneDrive\Desktop\(BotName)\cmds\misc\urban.js:26:30)
我不完全确定命令的问题是什么
这里是使用的特性的 npms 链接:
Node-Fetch: https://www.npmjs.com/package/node-fetch
Query-String: https://www.npmjs.com/package/query-string
【问题讨论】:
-
你能试试
console.logginglist看看它是什么吗?或者也许不对其进行解构并记录响应?
标签: javascript discord discord.js