This is my bot script that im trying to make play music and some other random commands. If you guys could help me out hat would be great. Im pretty new to coding and programming entirely so I'm still learning. I'm trying to code my bot to have some funny useless commands and then I'm trying to get it to join a voice chat and play music via youtube. i haven't made a leave command yet but I'm planning on doing so.
const client = new Discord.Client();
const prefix = require('$');
const token = 'Hidden for reasons'
prefix.setPrefix('$', '803575853895122944')
prefix.setPrefix('$', '804111808356024320')
let defaultPrefix = '$';
console.log(prefix.getPrefix());
client.on('message' ,message =>{
if (message.guild) return;
let guildPrefix = prefix.getPrefix(message.guild.id);
if (!guildPrefix) guildPrefix = defaultPrefix;
let args = message.content.slice(guildPrefix.length).split(' ');
if (!message.content.startsWith(guildPrefix.length)) return;
if (args[0].toLowerCase() === 'ping') {
return message.channel.send('Pong!');
};
});
client.on('ready' , () =>{
console.log('Bot is online');
client.user.setActivity('Hangin with the boys', {type: 'PLAYING'}).catch(console.error);
})
client.on('message', message=>{
let args = message.content.substring(PREFIX.length).split("$");
switch(args[0]){
case 'lez':
message.reply('wassup');
break;
case 'help':
message.reply('i cant be bothered')
break;
case 'rank':
message.reply('no one cares about your rank');
break;
}
})
client.on('guildMemberAdd', member =>{
const channel = member.guild.channels.find(channel => channel.name === "welcome");
if(!channel) return;
channel.send('Welcome to our server, ${member} , please read the rules in the rules channel!')
});
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
**if *client*.on('message' ,message =>{**
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 permissins');
if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
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');
}
}
}
})
***client*.login(token);** `
question from:
https://stackoverflow.com/questions/65948086/so-im-trying-to-make-a-discord-bot-this-is-my-first-time-ive-got-this-problem 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…