simplest way to play mp3 in Discord voice channel - discord

I figured out how to connect my bot to a voice channel, but I have no idea of how to make it play my mp3 files.
bot.on('message', async message => {
// Voice only works in guilds, if the message does not come from a guild,
// we ignore it
if (!message.guild) return;
if (message.content === '/join') {
// Only try to join the sender's voice channel if they are in one themselves
if (message.member.voice.channel) {
const connection = await message.member.voice.channel.join();
} else {
message.reply('You need to join a voice channel first!');
}
}
});

you can use the voiceConnection to play() the audio file
const channel = message.member.voice.channel;
channel.join().then(async(connection) => {
const stream = connection.play('/path/to/audio/file.mp3');
stream.on("finish", () => {
channel.leave(); // Leaves channel once the mp3 file finishes playing
});
}).catch((err: any) => console.log(err));

Related

Reaction Role Panel Stops working after a bot restart Discord.JS

I have a working reaction role menu that assigns and takes away roles fine until the bot restarts then it stop giving/taking away the roles, I am struggling to find a solution that matches with the way this reaction menu works.
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot)
if(!reaction.message.guild) return;
if (reaction.message.channel.id === channel) {
if (reaction.emoji.name === reactone){
await reaction.message.guild.members.cache.get(user.id).roles.add(roleone);
}
if (reaction.emoji.name === reacttwo){
await reaction.message.guild.members.cache.get(user.id).roles.add(roletwo);
}
if (reaction.emoji.name === reactthree){
await reaction.message.guild.members.cache.get(user.id).roles.add(rolethree);
}
if (reaction.emoji.name === reactfour){
await reaction.message.guild.members.cache.get(user.id).roles.add(rolefour);
}
//add more here
} else{
return
}
});
The client is not receiving the messageReactionAdd event from messages that were sent before the restart. You need to fetch these messages to be able to receive the messageReactionAdd from these messages.
There's an event called ready which will be executed when the startup is completed. That's the perfect place to fetch these messages by using the following code:
const channel = client.channels.cache.get('ID'); # Channel ID where the msg was sent
channel.messages.fetch({ limit: 10 });
or if you know the message ID:
const channel = client.channels.cache.get('ID'); # Channel ID where the msg was sent
channel.messages.fetch({ around: MESSAGE_ID, limit: 1 });
Read more on the official discord.js documentation

join voice channel when ready (discord.js v12)

I want to make my bot join 2 voice channel in A different guild or more when the bot ready
OK I tried to use it but it doesn't work
client.on("ready", () => {
const channel = client.channels.cache.get['ChannelID1', 'ChannelID2']
if (!channel) return
channel.join().then(connection => {
console.log('Done')
});
});
It doesn't work. I made sure that the ID is right and everything and it's still not working.
You have a syntax error in your code. In line 2 you have to call the function and not get a property of it:
client.on("ready", () => {
const channel = client.channels.cache.get('id');
if (!channel) return
channel.join().then(connection => {
console.log('Done')
});
});
There is also another problem. It will only join channel 1 because you cannot get two channels at the same time. You can split it up like this:
client.on("ready", () => {
const channels = ['id', 'id']
for (const channel of channels) {
const voiceChannel = client.channels.cache.get(channel)
if (!voiceChannel) continue
voiceChannel().then(connection => {
console.log('Done')
});
}
});

How do I get my discord bot to join a vc that a user is in?

I'm trying to get my bot join a vc that a user is in, when saying "+sing". After the audio is finished the bot should disconnect from the vc. This is as far as i've gotten, I don't know what I have done wrong.
client.on("message", msg => {
if (message.content === '+sing') {
const connection = msg.member.voice.channel.join()
channel.join().then(connection => {
console.log("Successfully connected.");
connection.play(ytdl('https://www.youtube.com/watch?v=jRxSRyrTANY', { quality: 'highestaudio' }));
}).catch(e => {
console.error(e);
connection.disconnect();
})
});
Btw I'm using discord.js.
You can get their GuildMember#VoiceState#channel and use that as the channel object to join
client.on("message", msg => {
if (message.content === '+sing') {
// you should add a check to make sure they are in a voice channel
// join their channel
const connection = msg.member.voice.channel.join()
}
})

Giving roles on discord

I'm making a discord murder mystery bot.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", (message) => {
msg = message.content.toLowerCase();
if (message.author.bot) {
return;
}
mention = message.mentions.users.first();
if (msg.startsWith("kill")) {
if (mention == null) {
return;
}
message.delete();
mention.send('you are dead');
message.channel.send("now done");
}
});
client.login('my token');
What would I add to the code so after the person who was tagged got there role changed from alive to dead?
// First, make sure that you're in a guild
if (!message.guild) return;
// Get the guild member from the user
// You can also use message.mentions.members.first() (make sure that you check that
// the message was sent in a guild beforehand if you do so)
const member = message.guild.members.cache.get(mention.id);
// You can use the ID of the roles, or get the role by name. Example:
// const aliveRole = message.guild.roles.cache.find(r => r.name === 'Alive');
const aliveRole = 'alive role ID here';
const deadRole = 'dead role ID here';
// You can also use try/catch with await if you make the listener and async
// function:
/*
client.on("message", async (message) => {
// ...
try {
await Promise.all([
member.roles.remove(aliveRole),
member.roles.add(deadRole)
]);
} catch (error) {
console.error(error);
}
})
*/
Promise.all([
member.roles.remove(aliveRole),
member.roles.add(deadRole)
]).catch(console.error);
The Promise.all means that the promises for adding and removing the roles are started at the same time. A promise is an object that can resolve to a value or reject with an error, so the .catch(console.error) logs all errors. I recommend that you handle errors for message.delete(), mention.send('you are dead'), and message.channel.send("now done") as well.
For more information on member.roles.remove() and member.roles.add(), see the documentation for GuildMemberRoleManager.

Getting this error code: " (node:25096) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'channel' of undefined "

Hi I've been trying to get a discord bot to join a voice channel for a while and have been struggling, this is the code i have so far and i get this message when i type "/join" in the general chat of the server. Thanks in advance!
const Discord = require('discord.js');
const client = new Discord.Client();
client.login('my token);
client.on('message', async message => {
// Voice only works in guilds, if the message does not come from a guild,
// we ignore it
if (!message.guild) return;
if (message.content === '/join') {
// Only try to join the sender's voice channel if they are in one themselves
if (message.member.voice.channel) {
const connection = await message.member.voice.channel.join();
} else {
message.reply('You need to join a voice channel first!');
}
}
});

Resources