0

我正在关注如何制作不和谐机器人的教程,一切正常,直到 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:[]声明 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);
4

2 回答 2

0

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 方法与异步等待一起使用。

于 2020-05-02T10:46:41.440 回答
0

此代码由我修复: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);
于 2020-05-12T16:07:50.927 回答