Reputation: 103
I created a music bot that streams music when someone taps !play
in-text channel
Now I want to switch !play
with please play
but it response only with pleaseplay
with no space between them and when I tried to change the code:
const prefix = 'please'; //before
const prefix = 'please '; //after
but it doesn't work at all with an error
log (node:5296) UnhandledPromiseRejectionWarning: Error: No video id found:
const {Client, Attachment, Message} = require('discord.js');
const {token} = require("./config.json");
const bot = new Client();
const prefix = 'please ';
const ytdl = require("ytdl-core");
const request = require('request');
const cheerio = require('cheerio');
const queue = new Map();
bot.on('ready', () => {
console.log('Client is online!');
bot.on("message", async message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const serverQueue = queue.get(message.guild.id);
if (message.content.startsWith(`${prefix}play`)) {
execute(message, serverQueue);
return;
} else if (message.content.startsWith(`${prefix}skip`)) {
skip(message, serverQueue);
return;
} else if (message.content.startsWith(`${prefix}stop`)) {
stop(message, serverQueue);
return;
} else {
message.channel.send("You need to enter a valid command!");
}
});
async function execute(message, serverQueue) {
const args = message.content.split(" ");
const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send(
"You need to be in a voice channel to play music!"
);
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"I need the permissions to join and speak in your voice channel!"
);
}
const songInfo = await ytdl.getInfo(args[1]);
const song = {
title: songInfo.title,
url: songInfo.video_url
};
if (!serverQueue) {
const queueContruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true
};
queue.set(message.guild.id, queueContruct);
queueContruct.songs.push(song);
try {
var connection = await voiceChannel.join();
queueContruct.connection = connection;
play(message.guild, queueContruct.songs[0]);
} catch (err) {
console.log(err);
queue.delete(message.guild.id);
return message.channel.send(err);
}
} else {
serverQueue.songs.push(song);
return message.channel.send(`${song.title} has been added to the queue!`);
}
}
function skip(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"You have to be in a voice channel to stop the music!"
);
if (!serverQueue)
return message.channel.send("There is no song that I could skip!");
serverQueue.connection.dispatcher.end();
}
function stop(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"You have to be in a voice channel to stop the music!"
);
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
}
function play(guild, song) {
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection
.play(ytdl(song.url))
.on("finish", () => {
serverQueue.songs.shift();
play(guild, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`streaming: **${song.title}**`);
}
bot.login(token);
Upvotes: 2
Views: 1094
Reputation: 86
Edit: Full upgraded code... also clear like a crystal.
const {Client, Attachment, Message} = require('discord.js');
const {token} = require("./config.json");
const bot = new Client();
const prefix = 'please';
const ytdl = require("ytdl-core");
const request = require('request');
const cheerio = require('cheerio');
const queue = new Map();
bot.on('ready', () => {
console.log('Client is online!');
}
bot.on("message", async message => {
let content = message.content.split(' ');
if (message.author.bot) return;
if (content.shift() !== prefix) return;
const serverQueue = queue.get(message.guild.id);
switch (content.shift()) {
case 'play': exec(content, message, serverQueue); break;
case 'skip': skip(content, message, serverQueue); break;
case 'stop': stop(content, message, serverQueue); break;
default: message.channel.send("You need to enter a valid command!");
}
});
async function exec (ctx, msg, que) {
const voiceChannel = msg.member.voice.channel;
if (!voiceChannel)
return msg.channel.send(
"You need to be in a voice channel to play music!"
);
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK"))
return msg.channel.send(
"I need the permissions to join and speak in your voice channel!"
);
const songInfo = await ytdl.getInfo(ctx.join(' '));
const song = {
title: songInfo.title,
url: songInfo.video_url
};
if (!que) {
const queueContruct = {
textChannel: msg.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true
};
queue.set(msg.guild.id, queueContruct);
queueContruct.songs.push(song);
try {
var connection = await voiceChannel.join();
queueContruct.connection = connection;
play(msg.guild, queueContruct.songs[0]);
} catch (err) {
console.log(err);
queue.delete(msg.guild.id);
return msg.channel.send(err);
}
} else {
que.songs.push(song);
return msg.channel.send(`${song.title} has been added to the queue!`);
}
}
await function play(gui, sng) {
const serverQueue = queue.get(gui.id);
if (!sng) {
serverQueue.voiceChannel.leave();
queue.delete(gui.id);
return;
}
const dispatcher = serverQueue.connection
.play(ytdl(sng.url))
.on("finish", () => {
serverQueue.songs.shift();
play(gui, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`streaming: **${sng.title}**`);
}
async function stop(ctx, msg, que) {
if (!msg.member.voice.channel)
return msg.channel.send(
"You have to be in a voice channel to stop the music!"
);
que.songs = [];
que.connection.dispatcher.end();
}
async function skip(ctx, msg, que) {
if (!msg.member.voice.channel)
return msg.channel.send(
"You have to be in a voice channel to stop the music!"
);
if (!que)
return msg.channel.send("There is no song that I could skip!");
que.connection.dispatcher.end();
}
bot.login(token);
Upvotes: 1
Reputation: 8492
The problem is that your arguments are split based on the space character.
const args = message.content.split(" ");
args[1]
is being referenced to get the text after the command name, but with the space in the command, args[1]
will always be "play" (or whichever command they are using)
A quick fix would be to change args[1]
to args[2]
const songInfo = await ytdl.getInfo(args[2]);
Upvotes: 2