admin管理员组文章数量:1433485
I would like the bot to join a voice channel when someone joins and then play a sound file.
So far, I've gotten the bot to join the voice channel but it just doesn't play the sound and no errors show so I don't really know what is going wrong.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('BOT TOKEN');
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
let textChannel = oldMember.guild.channels.get('TEXTCHANNEL ID')
if(oldUserChannel === undefined && newUserChannel !== undefined) {
if (newMember.id === 'MEMEBER ID') //Member 1
{
newUserChannel.join()
.then(connection => {
console.log("Joined voice channel!");
const dispatcher = connection.playFile("C:\Users\NAME\Documents\Wele_Bot\music\bossman.mp3");
dispatcher.on("end", end => {newUserChannel.leave()});
})
.catch(console.error);
}
else if (newMember.id === 'MEMEBER ID') //Member 2
{
textChannel.send('Hello Member 2')
}
else if (newMember.id === 'MEMEBER ID') //Member 3
{
textChannel.send('Hello Member 3')
}
else //Random
{
textChannel.send("Hello")
}
}
});
I would like the bot to join a voice channel when someone joins and then play a sound file.
So far, I've gotten the bot to join the voice channel but it just doesn't play the sound and no errors show so I don't really know what is going wrong.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('BOT TOKEN');
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
let textChannel = oldMember.guild.channels.get('TEXTCHANNEL ID')
if(oldUserChannel === undefined && newUserChannel !== undefined) {
if (newMember.id === 'MEMEBER ID') //Member 1
{
newUserChannel.join()
.then(connection => {
console.log("Joined voice channel!");
const dispatcher = connection.playFile("C:\Users\NAME\Documents\Wele_Bot\music\bossman.mp3");
dispatcher.on("end", end => {newUserChannel.leave()});
})
.catch(console.error);
}
else if (newMember.id === 'MEMEBER ID') //Member 2
{
textChannel.send('Hello Member 2')
}
else if (newMember.id === 'MEMEBER ID') //Member 3
{
textChannel.send('Hello Member 3')
}
else //Random
{
textChannel.send("Hello")
}
}
});
Share
Improve this question
edited Apr 16, 2019 at 18:52
acarlstein
1,8382 gold badges15 silver badges21 bronze badges
asked Apr 16, 2019 at 18:45
Sean CarsonSean Carson
811 gold badge1 silver badge3 bronze badges
10
- 1 I am suspecting about the link to your sound audio. Should it be something like "http://....*.mp3" instead? otherwise the web will not understand your localpath – duc mai Commented Apr 16, 2019 at 19:16
- 1 @ducmai To play a local file, the current setup is fine. The Discord.js docs example uses an absolute path like the code above. – slothiful Commented Apr 16, 2019 at 19:44
-
Is your username actually
NAME
? That's what's in your path. If not, that would be why nothing is played. – slothiful Commented Apr 16, 2019 at 19:46 - @slothiful I just put place holders into the question – Sean Carson Commented Apr 16, 2019 at 19:49
-
1
You could update to d.js v12(master), that has a full voice rewrite but al sso brings breaking changes,
npm i discordjs/discord.js
to get v12 – PLASMA chicken Commented Apr 20, 2019 at 9:50
1 Answer
Reset to default 1I believe your issue is that the bot isn't actually joining the channel before you call the dispatcher. The bot needs to be told to enter the channel before you call the dispatcher. Havn't worked on audio in a while but I believe that connection.join()
right above your dispatcher would work. You may need to .then()
to ensure it dosn't initilize the dispatcher before the bot joins the channel.
本文标签: javascriptDiscord Bot that Plays an Audio File When Someone JoinsStack Overflow
版权声明:本文标题:javascript - Discord Bot that Plays an Audio File When Someone Joins - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745614437a2666312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论