【问题标题】:How do I fix "Declaration or statement expected. ts(1128)" in Discord.js?如何修复 Discord.js 中的“预期声明或声明。ts(1128)”?
【发布时间】:2019-12-17 04:44:06
【问题描述】:

我刚开始编写 Discord 机器人,但在使用 Giphy API 进行编码时遇到了问题。我不知道整个代码是否错误,但无论如何我对此感到困惑。我该如何解决这个问题?

第一个 If-Then 语句没有给出任何错误,但代码中的第二个语句确实给出了一个错误。

我使用 Visual Studio Code 进行编码,并使用 Discord.js 作为 Node.js 模块。

client.on('message', message =>{
    //console.log(message.content);

    if(message.content.startsWith(`${prefix}`)) {
    message.channel.send("oh yeah it's the prefix of the bot")
    }

    if(message.content.startsWith(`${prefix}gif`)) {
    giphy.trending("gifs", {})
        .then((response) => {
            var totalResponses = response.data.length;
            var responseIndex = Math.floor((Math.random() * 10) +1) % totalResponses;
            var responseFinal = response.data[responseIndex]

            message.channel.send("There you go!", {
                files: [responseFinal.images.fixed_height.url]
            })
            file
    })
})

错误:

声明或声明预期 ts(1128)

【问题讨论】:

  • 好吧,错误消息肯定会指向一行...您期望 file 自己在做什么?
  • 另外,您缺少 if(message.content.startsWith(${prefix}gif)) { 的右括号
  • 第二个结尾"}"

标签: javascript node.js discord.js giphy-api


【解决方案1】:

您缺少一个右大括号 - 这是您的确切代码,但已修复。

client.on('message', message => {
    //console.log(message.content);

    if (message.content.startsWith(`${prefix}`)) {
        message.channel.send("oh yeah it's the prefix of the bot")
    }

    if (message.content.startsWith(`${prefix}gif`)) {
        giphy.trending("gifs", {})
            .then((response) => {
                var totalResponses = response.data.length;
                var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
                var responseFinal = response.data[responseIndex]

                message.channel.send("There you go!", {
                    files: [responseFinal.images.fixed_height.url]
                })
                file
            })
    }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-02
    • 2019-01-19
    • 2021-02-09
    • 2018-06-12
    • 1970-01-01
    • 2020-12-05
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多