【问题标题】:Discord.js Urban Dictionary Node FetchDiscord.js 城市字典节点获取
【发布时间】: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.logging list看看它是什么吗?或者也许不对其进行解构并记录响应?

标签: javascript discord discord.js


【解决方案1】:

当你使用.then()时,里面的变量不会被返回。

const { list } = await fetch(`https://api.urbandictionary.com/v0/define?${args}`).then(response => response.json())

上述代码中的response.json() 是本地的,因此“list”是未定义的。

const response = await fetch(`https://api.urbandictionary.com/v0/define?${args}`)
const {list} = await response.json()

另外,请务必将此命令仅限于 NSFW 频道,因为城市词典可能会返回 NSFW 数据,并且违反不和谐 TOS 将这些数据放入非 NSFW 频道。

【讨论】:

  • 我已经检查过了,.then 实际上将返回的值更改为Promise。我使用的确切代码是Promise.resolve({ abc: "def" }).then(value =&gt; value.abc),它确实返回了"def"
【解决方案2】:

您的代码运行良好,除了 api 调用 URL 应该是 ...

https://api.urbandictionary.com/v0/define?term=

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 2017-09-25
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多