Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Music Bot discord.js issue, Cannot read property 'voice' of undefined

I'm trying to create a hydra-style bot discord with an embed that automatically gets assigned control reactions with which I can pause, resume, stop the music etc ...

I have a problem, if I try to start the command to pause typing '*psres' everything works fine, however, if I try to start the pause command via the reaction it gives me the following error:

(node:20172) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined
    at Object.execute (C:\Users\Giuseppe\Desktop\botdiscord\commands\psres.js:9:40)
    at Client.<anonymous> (C:\Users\Giuseppe\Desktop\botdiscord\commands\setup.js:41:53)

This is my setup.js:

module.exports = {
    name: 'setup',
    description: "music embed with reaction",
    async execute(message, args, Discord, client){
        
        const channel = '831573587579371580';
        const playem = '⏯️';
        const stopem = '⏹️';  
        const nextem = '⏭️';
        const shuffleem = '🔀';
        const casualem = '🔄';
      
        let embed = new Discord.MessageEmbed()
       
        .setColor('#e42643')
        .setTitle('Barman')
        .setImage('https://images4.alphacoders.com/943/943845.jpg') 
        .setFooter('il prefisso del bot è: *');     
        
        console.log('message: ' + message )
        console.log('args: ' + args)
        console.log('Discord: ' + Discord)
        console.log('client: ' + client)
    
           let messageEmbed = await message.channel.send(embed);
           messageEmbed.react(playem);
           messageEmbed.react(stopem);
           messageEmbed.react(nextem); 
           messageEmbed.react(shuffleem); 
           messageEmbed.react(casualem);
           
           client.on('messageReactionAdd', async (reaction, user) =>{
               if (reaction.message.partial) await reaction.message.fetch();
               if (reaction.partial) await reaction.fetch();
               if (user.bot) return;
               if (!reaction.message.guild) return;
               if (reaction.message.channel.id == channel){
                   
                    if (reaction.emoji.name ===  playem){
                        console.log('pause/resume');                    
                       client.commands.get('psres').execute(message, client, args) //line of error
                    }
                } 
           });
           
    }
} 

This is my pause/resume command:

const { MessageEmbed } = require("discord.js")

const { COLOR } = require("../config.json");

module.exports = {
  name: "psres", 
  description: "pause/resume",
  async execute (client, message, args) {
    const { channel } = message.member.voice; /line of error
    let embed = new MessageEmbed()
.setColor(COLOR);
      
    if (!channel) {
      embed.setAuthor("Devi essere in un canale vocale!")
      return message.channel.send(embed);
    }

    const serverQueue = message.client.queue.get(message.guild.id);
    //prova
    if (!serverQueue) {
        embed.setAuthor("Non c'è nulla che possa mettere in pausa")
        return message.channel.send(embed);
      }
      
      if(serverQueue && serverQueue.playing) {
        serverQueue.playing = false;
        serverQueue.connection.dispatcher.pause(true)
        
        embed.setDescription("✅ | Canzone messa in pausa")
        embed.setThumbnail(client.user.displayAvatarURL())
        return message.channel.send(embed)
    } else if(serverQueue && !serverQueue.playing) {
      serverQueue.playing = true;
      serverQueue.connection.dispatcher.resume()
  embed.setAuthor("✅ | Riprendi")
   embed.setThumbnail(client.user.displayAvatarURL())
  return message.channel.send(embed)
 }
    embed.setDescription("Non c'è nulla che io possa riprendere")
    message.channel.send(embed)
    
  }
}

enter image description here

like image 690
Franco Romano Avatar asked Jan 01 '26 11:01

Franco Romano


1 Answers

In your setup.js file the command handler is client.commands.get('psres').execute(message, client, args) with message, client and args as parameters. But in the pause/resume command file, you are executing execute (client, message, args) with client, message and args as param. The objects are not what you think it is because if you compare both of them, the parameters are not matching/placed in wrong order. So matching the parameters either according to the setup commands param or the way you are executing in the pause/resume command.

like image 117
Radnerus Avatar answered Jan 03 '26 00:01

Radnerus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!