0

I'm trying to make a discord music bot using this tutorial: https://www.youtube.com/watch?v=3wJJDM7jUsk&list=PLbbLC0BLaGjpyzN1rg-gK4dUqbn8eJQq4&index=12&ab_channel=CodeLyon

Right now I'm at 10:06 in the video and I'm trying to let the bot react to the command, but when I do '-play' the bot does not respond to my command. Im not getting any error messages and Im not sure why the bot is not responding.

Could someone help me out here?

main.js:

const Discord = require('discord.js');

// const client = new Discord.Client();
const client = new Discord.Client({ intents: [65535] });

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

const prefix ='-' 

client.on('messageCreate', message => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();
  if (command === 'clear') {
    client.commands.get('clear').execute(message, args);
  } else if (command === 'play') {
    client.commands.get('play').execute(message, args);
  } else if (command === 'leave') {
    client.commands.get('leave').execute(message, args);
  }
});

client.login('token');

play.js:

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
 
module.exports = {
    name: 'play',
    description: 'Joins and plays a video from youtube',
    async execute(message, args) {
        const voiceChannel = message.member.voice.channel;
 
        if (!voiceChannel) return message.channel.send('You need to be in a channel to execute this command!');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissions');
        if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissions');
        if (!args.length) return message.channel.send('You need to send the second argument!');
 
        const validURL = (str) =>{
            var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
            if(!regex.test(str)){
              return false;
            } else {
              return true;
            }
        }
 
        if(validURL(args[0])){
 
            const  connection = await voiceChannel.join();
            const stream  = ytdl(args[0], {filter: 'audioonly'});
 
            connection.play(stream, {seek: 0, volume: 1})
            .on('finish', () =>{
              voiceChannel.leave();
              message.channel.send('leaving channel');
            });
 
            await message.reply(`:thumbsup: Now Playing ***Your Link!***`)
 
            return
        }
 
        
        const  connection = await voiceChannel.join();
 
        const videoFinder = async (query) => {
          const videoResult = await ytSearch(query);
 
          return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
 
        }
 
        const video = await videoFinder(args.join(' '));
 
        if(video){
          const stream  = ytdl(video.url, {filter: 'audioonly'});
          connection.play(stream, {seek: 0, volume: 1})
          .on('finish', () =>{
            voiceChannel.leave();
          });

          await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
      } else {
          message.channel.send('No video results found');
      }
    }
}
clipyz
  • 15
  • 3
  • Does this answer your question? [Discord bot doesn't answer commands](https://stackoverflow.com/questions/75525777/discord-bot-doesnt-answer-commands) – Zsolt Meszaros May 12 '23 at 16:06
  • Also, the `message` event is deprecated, you should use `messageCreate` instead: `client.on('messageCreate', message => ` – Zsolt Meszaros May 12 '23 at 16:07
  • @ZsoltMeszaros Hi, I have changed my message to messageCreated and read the other post and changed my intents. However my bot still isnt responding – clipyz May 12 '23 at 16:38
  • Do you start your message with a dash (`-`)? – Zsolt Meszaros May 12 '23 at 16:39
  • @ZsoltMeszaros i have found out that it doesnt run my if else statement when the command === play but i dont know why it wont run that code – clipyz May 12 '23 at 16:39
  • I do start my message with my prefix, i tried using an other prefix but it makes no difference – clipyz May 12 '23 at 16:41

0 Answers0