簡體   English   中英

Discord.js 城市字典節點獲取

[英]Discord.js Urban Dictionary Node Fetch

我正在嘗試為我的機器人創建 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)

我不完全確定命令有什么問題

以下是所用功能的 npm 鏈接:

Node-Fetch: https://www.npmjs.com/package/node-fetch
Query-String: https://www.npmjs.com/package/query-string

當您使用.then()時,不會返回其中的變量。

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

上面代碼中的response.json()是本地的,因此“列表”是未定義的。

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

並且在一個完全不相關的注釋中,確保將此命令僅限於 NSFW 頻道,因為城市詞典可能會返回 NSFW 數據,並且它反對 discord TOS 將該數據放入非 NSFW 頻道。

除了 api 調用 URL 應該是......

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM