Discord.js voiceStateUpdate not registering properly - node.js

I have this piece code for discord.js copied from reddit which is supposed to detect if user has joined or left the channel but it sends leave notifications only no matter if someone joins or leaves the channel. About a year ago when I last tried this same code, it worked well, but now it doesn't.
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User Joins a voice channel
console.log('join');
} else if(newUserChannel === undefined){
// User leaves a voice channel
console.log('leave');
}
})
Has discord.js changed or what am I doing wrong? If you want the whole code, I can send it aswell.

Since you are using Discord V12, voiceChannel is no longer a property of the GuildMember object. Use voice property instead.
guildMember.voice.channel;
There is a nice guide where you can find what has changed in V12 when upgrading from V11.

Related

is there a way to make my bot joins a voice channel even it is empty

so what I am trying to reach is that I want when someone adds the bot to the server he can run a command with voice channel ID [!join 9562xxxxxxxxxxxxx] and it joins the channel and played the music even the voice channel is empty, because I am controlling the music throw the code I made, so it is something like a radio station.
I hope I did a great job explaining the problem and I am really a beginner programmer.
node v12.22.10
discord.js v12.5.3
What I was doing is getting a server id and channel id and putting them in the code.
You shouldn't be using discord.js v12 anymore as it uses an old version of the Discord API which will deprecate soon. If you still want to use v12, you should do the following thing:
let args = message.content.substring(1).split(" "); // Array of the arguments
switch(args[0].toLowerCase()){
case 'join':
if(!args[1]) return message.channel.send(`Please enter a voice channel id`);
if(!Number(args[1])) return message.channel.send(`Please enter a valid voice channel id`);
const vc = message.guild.channels.cache.get(args[1]);
if(!vc) return message.channel.send(`This voice channel does not exist`);
if(typeof vc.join !== 'function') return message.channel.send(`The channel is not a voice channel`);
vc.join().then(connection => {
const dispatcher = connection.play('Your Song URL');
}).catch(err => {
message.channel.send(`There was an error while connecting to the voice channel`);
});
break;
}

How edit messages in Discord.js V12 using message ID

There is the way to edit a message on DiscordJS
To do this, we just need to pass it to the function, the client variable.
exports.myNewFunction = async function myNewTestFunction(client)
First, in order to edit a message using the Discord.JS API, we will need to find the server's GuildID.
const guild = client.guilds.cache.get(`Your Guild ID`);
Now, we're going to need to find the channel the message is on. For this, we will use the channel ID
const channel = guild.channels.cache.find(c => c.id === `Your message Channel` && c.type === 'text');
Finally, let's go to the editing part of the message. For this, we will only need to provide the ID of the message that you want to be edited.
channel.messages.fetch(`Your Message ID`).then(message => {
message.edit("New message Text");
}).catch(err => {
console.error(err);
});

Get message ID from reaction discord.js

Note: I am using discord.js V11, I know I plan on updating it to V12 next month after I unspaghetti my spaghetti code.
So I have no idea how to grab the messageID from a message that has triggered the reaction in the bot.
The way I want it to work is as follows: A user reacts to a message, any message, with the reaction programmed within the bot. The bot then grabs the message url that the reaction was given to, and then sends a message to a client.channels.get("id").
So I tried using this code but really couldn't quite get to where I needed to be:
client.on('messageReactionAdd', async(reaction, user, message) => {
if(reaction.emoji.name === "hm") {
let ticket = client.channels.get("CHANNEL_ID");
let ticketurl = message.url
ticket.send("Test confirmed" + ticketurl);
}
});
Figured it out!
I just needed to add this:
let ticketurl = react.message.url

how to move a member to a different channel

So I have been looking around how to make a command to move someone to a different channel but they use a command called .setVoiceChannel but I can't find it? I know think might be kind of a noob question.
here is what I currently have
const user = message.author.id;
const member = message.guild.member(user);
// what a I trying to do
member.setVoiceChannel(/* them parameters */); // not defined and I can't find it in documents
// The member is the message author.
const member = message.member;
// Getting the channel.
const channel = client.channels.cache.get("Channel");
// Checking if the channel exists and if the channel is a voice channel.
if (!channel || channel.type !== "voice") return console.log("Invalid channel");
// Checking if the member is in a voice channel.
if (!member.voice.channel) return console.log("The member is not in a voice channel.");
// Moving the member.
member.voice.setChannel(channel).catch(e => console.error(`Couldn't move the user. | ${e}`));
Note: setVoiceChannel() is a valid method of GuildMember in Discord JS v11.
It has been changed to GuildMember.voice.setChannel(ChannelResolvable) in Discord JS v12.

Discord.js: Verify channel

I'm trying to make my own bot for my server, for now i'm focusing on the verify. By acting to the check mark emoji it'll add the verified role, and then it should remove only the user reaction, but instead it'll remove every reaction right away
client.on('messageReactionAdd', async (reactionReaction, user) => {
const message = reactionReaction.message;
const verifyChannel = message.guild.channels.cache.find(c => c.name === 'approvazione');
const member = message.guild.members.cache.get(user.id);
if (member.user.bot) return;
const verify = message.guild.roles.cache.get('728000975046180988');
if (reactionReaction.emoji.name === '✅' && message.channel.id === verifyChannel.id) {
member.roles.add(verify).catch(console.error);
await reactionReaction.remove(member).catch(console.error);
}
here is the message sent by the bot with it's own reaction
and here is the same message after i reacted, and both mine and the bot reaction are removed, i just want my reaction to be removed
If you look at the docs it takes no parameter for the user:
https://discord.js.org/#/docs/main/stable/class/MessageReaction?scrollTo=remove
This was changed in v12, the method now is to use .users.remove:
reactionReaction.users.remove(member);

Resources