Search code examples
javascriptnode.jsdiscorddiscord.jsnode-fetch

why does my code in nodejs gives error and asks me to write catch but i have already said it in the code


So this is the error

(node:24077) UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'id' of undefined
at Client.msg (/app/bot.js:18:22)
at Client.emit (events.js:196:13)
at MessageCreateAction.handle (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/ws/7.4.2/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (events.js:196:13)
at Receiver.receiverOnMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/ws/7.4.2/node_modules/ws/lib/websocket.js:825:20)
(node:24077) 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(). (rejection id: 1)
(node:24077) [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.

this is the code

console.log('loding')
const Discord = require('discord.js');
const client = new Discord.Client();
const fetch = require('node-fetch')

client.login(process.env.SECRET);


client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});



client.on('message', msg) 

async function msg(){

this is the discord channel id

  if (msg.channel.id = '791583092275937303' ){
   if (msg.content === 'ping') {
     msg.reply('Pong!');
   }else if (msg.content === '!gif'){
     msg.reply('gif');
     let url = `https://api.tenor.com/v1/search?q=codingtrain&key=${process.env.tenor}&limit=8`
     
     wow().then(response => {
      console.log('ok')
       msg.reply('ok')
  }).catch(err => {
      console.log(err);
      
  });

     
     async function wow(){
     let response = await fetch(url)
     let blob = await response.json();
     
     }
   }
  }
}

this is the console before the error

node bot.js
loding
Logged in as cybemachine#8971!

this is the discord server link:- https://discord.gg/CvCBUsyN


Solution

  • You are using msg outside of the message event, therefor msg is undefined. You also said = in your first if-condition, but it has to be === or ==, as you want to know if the id is equal to your provided id. Using only = means you want to set a value of a variable. You have to update your code to this:

    console.log('loding')
    const Discord = require('discord.js');
    const client = new Discord.Client();
    const fetch = require('node-fetch')
    
    client.login(process.env.SECRET);
    
    client.on('ready', () => {
      console.log(`Logged in as ${client.user.tag}!`);
    });
    
    client.on('message', async msg => {
      if (msg.channel.id === '791583092275937303') {
        if (msg.content === 'ping') {
          msg.reply('Pong!');
        } else if (msg.content === '!gif') {
          msg.reply('gif');
          let url = `https://api.tenor.com/v1/search?q=codingtrain&key=${process.env.tenor}&limit=8`
    
          wow().then(response => {
            console.log('ok')
            msg.reply('ok')
          }).catch(err => {
            console.log(err);
    
          });
    
    
          async function wow() {
            let response = await fetch(url)
            let blob = await response.json();
    
          }
        }
      }
    })