Chcę, aby mój bot wysłał wiadomość na kanał tekstowy, z którym właśnie wszedł na jego kanał głosowy lub właśnie go opuścił. Jestem nowy w kodowaniu, ale próbowałem tego, ale to nie działa.
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel;
let oldUserChannel = oldMember.voiceChannel;
if (oldUserChannel === undefined && newUserChannel !== undefined) {
if (newUserChannel === bot.voiceChannel) {
console.log("Hello");
}
} else if (newUserChannel === undefined) {
}
});
Nic nawet nie pojawia się w konsoli.
0
Waldre
3 listopad 2018, 20:07
1 odpowiedź
Najlepsza odpowiedź
Myślę, że to nie działa, ponieważ używasz bot.voiceChannel
, ale to nie istnieje: .voiceChannel
jest własnością GuildMember
, więc najpierw musisz zdobyć członka.
Zrobiłbym to tak:
bot.on('voiceStateUpdate', (oldMember, newMember) => {
// Here I'm storing the IDs of their voice channels, if available
let oldChannel = oldMember.voiceChannel ? oldMember.voiceChannel.id : null;
let newChannel = newMember.voiceChannel ? newMember.voiceChannel.id : null;
if (oldChannel == newChannel) return; // If there has been no change, exit
// Here I'm getting the bot's channel (bot.voiceChannel does not exist)
let botMember = oldMember.guild.member(bot.user),
botChannel = botMember ? botMember.voiceChannel.id : null;
// Here I'm getting the channel, just replace VVV this VVV with the channel's ID
let textChannel = oldMember.guild.channels.get('CHANNEL_ID_HERE');
if (!textChannel) throw new Error("That channel does not exist.");
// Here I don't need to check if they're the same, since it would've exit before
if (newChannel == botChannel) {
// console.log("A user joined.");
textChannel.send(`${newMember} has joined the voice channel.`);
} else if (oldChannel == botChannel) {
// console.log("A user left.");
textChannel.send(`${newMember} has left the voice channel.`);
}
});
1
Federico Grandi
3 listopad 2018, 22:11
Podobne pytania
Nowe pytania
discord.js
Discord.js to moduł node.js, który umożliwia programistom interakcję z Discord API. Użyj tego tagu w przypadku pytań dotyczących korzystania z discord.js, a nie pytań dotyczących interfejsu Discord API lub ogólnie Discord (w przypadku pytań dotyczących interfejsu API użyj tagu „discord”). Rozważ także użycie tagu „javascript”.