【问题标题】:Giphy not working on Discord bot (javascript)Giphy 无法在 Discord 机器人上运行(javascript)
【发布时间】:2020-08-16 20:28:23
【问题描述】:

我在关注this 教程,了解如何制作一个不和谐的机器人,一切正常,直到 33:32 他添加了我已经安装了 giphy sdk/api 的 giphy 东西,创建了一个应用程序,但在他制作完成之后搜索语句他说你可以控制台记录它,所以我做到了,并且有一些 gif 结果出来,在我的控制台上返回 undefined(我不知道为什么),然后他添加了一些数学内容,我也这样做了,然后在他添加了消息部分的点,他还添加了此代码files:[responseFinal.images.fixed_height.url],然后在我的控制台上返回了此代码

(node:3136) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'images' of undefined
    at D:\Discord bots\Oboto v2.0\index.js:24:61
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:3136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3136) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这让我感到困惑,然后我选择了一种替代方法,而不是 giphy.search 我做了 giphy.random 使用相同的参数,删除了数学内容和 console.log(response)响应,猜猜它实际上给了我一个 gif !(当然在控制台中)然后我实现了我的 files:[]statement aaaa,它返回了相同的事情 (cannot read property 'images' of undefined) 我对 discord.js 和 javascript 也有点陌生,这也是我的整个代码,

const Discord = require('discord.js');
const { prefix, token, giphyToken } = require('./config.json');
const client = new Discord.Client();

var GphApiClient = require('giphy-js-sdk-core')
giphy = GphApiClient(giphyToken)

client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', message => {
    if (message.member.hasPermission(["KICK_MEMBERS", "BAN_MEMBERS"])){

        if (message.content.startsWith(`${prefix}kick`)) {

            let member = message.mentions.members.first();
            member.kick().then((member) =>{

                giphy.random('gifs', {'q':'fail'})  
                    .then((response) => {

                        console.log(response);
                        message.channel.send(":wave:",{files:[response.images.fixed_height.url]});

                })


            })
        }   
    }
})

client.login(token);

【问题讨论】:

    标签: javascript discord.js giphy-api


    【解决方案1】:

    cannot read property 'images' of undefined,这意味着您正在尝试访问一个空对象。与java中的空指针异常相同。这意味着您的响应为空。

    你也得到了UnhandledPromiseRejectionWarning,这意味着你的承诺是抛出你在任何地方都没有发现的错误。你可以像这样捕捉你的错误

    member.kick().then((member) =>{
        giphy.random('gifs', {'q':'fail'})  
        .then((response) => {
            console.log(response);
            message.channel.send(":wave:",{files:[response.images.fixed_height.url]});
        }).catch(e => { console.error(e}) }
    }).catch(e => { console.error(e) }
    

    现在您可以看到您遇到了什么错误。您还可以将 try catch 方法与 async await 结合使用。

    【讨论】:

      【解决方案2】:

      此代码由我修复:D

      Discord Bot - 用 Giphy 踢成员

      我根本不是专业人士。 您也可以将此 giphy 添加到新成员通知中。

      const Discord = require('discord.js');
      const { prefix, token, giphyToken } = require('./config.json');
      const bot = new Discord.Client();
      
      var GphApiClient = require('giphy-js-sdk-core');
      bot.giphy = GphApiClient(giphyToken);
      
      bot.on('message', (message) => {
        if (message.member.hasPermission(['KICK_MEMBER', 'BAN_MEMBERS'])) {
          //console.log(message.content);
      
          if (message.content.startsWith(`${prefix}kick`)) {
            //message.channel.send("kick")
      
            let member = message.mentions.members.first();
            member.kick().then((member) => {
              bot.giphy.search('gifs', { q: 'fail' }).then((response) => {
                  var totalResponses = response.data.length;
                  var responseIndex = Math.floor(Math.random() * 10 + 1) % totalResponses;
                  var responseFinal = response.data[responseIndex];
      
                  message.channel.send(':wave: ' + member.displayName + ' has been kicked!',{
                      files: [responseFinal.images.fixed_height.url]
                    }
                  )
                })
            })
          }
        }
      });
      
      bot.login(token);
      

      【讨论】:

      • 毕竟我只是个新手
      猜你喜欢
      • 1970-01-01
      • 2019-12-21
      • 2020-12-27
      • 2021-04-19
      • 2019-10-31
      • 2019-01-19
      • 2018-07-22
      • 2017-10-11
      • 2021-02-19
      相关资源
      最近更新 更多