#userUpdate event is not being emitted
125 messages · Page 1 of 1 (latest)
• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.
you don't need any intents for the userUpdate event
Here's my code, any idea on why it wont work?
client.on('userUpdate', (oldMember, newMember) => {
debug("UserUpdate event fired");
const oldUsername = oldMember.user.username;
const newUsername = newMember.user.username;
// Check if the username has changed
if (oldUsername !== newUsername) {
// Username has been changed
debug(`User '${oldUsername}' changed their username to '${newUsername}'`);
// You can perform any actions or emit events here based on the username change
eventEmitter.emit('usernameChange', oldUsername);
}
});
its supposed to detect username changes, but it wont even run in the first place
oldMember is already a user so .user will not work
you could yes, but i suggest changing oldMember and newMember to oldUser and newUser so you dont confuse users and members
alright thanks for the tip
but the event wont fire at all
is your bot online?
yeah
the setup seems fine according to docs
other commands and event work
for example the ready event work fine
do you have an event handler? or is that event in one file only
all of the events are being handled in the root file, index.js
mind if i ask what eventEmitter is?
hold on lemme grab it
here
// root file
// declaring
const { EventEmitter } = require("events");
const eventEmitter = new EventEmitter();
// pushing to container
eventEmitter.emit('usernameChange', oldUsername);
// command file
eventEmitter.on("usernameChange", async (username) => {
// ...
}
sure 1 sec
preferably in the ready event since you said that does fire
[Object: null prototype] {
shardDisconnect: [Function (anonymous)],
ready: [Function (anonymous)],
userUpdate: [Function (anonymous)],
messageCreate: [AsyncFunction (anonymous)]
}
stringifying it returns an empty object
seems like the event is being registered
what does debug do?
does that log messages in the console?
yes
does debug("UserUpdate event fired"); log in the console?
can you show your client's intents
1 sec
const client = new discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessages,
],
});
interesting..
alright gimme a moment
Please add the following code to your code base outside of any other event listeners and provide the full log output relevant to your issue.
client
.on("debug", console.log)
.on("warn", console.log)
- Note: if you initialize your Client as
botor other identifiers you need to use these instead ofclient - If the output is too long to post consider using a bin instead: gist | paste.gg | sourceb.in | hastebin
also please do this as well
require('dotenv').config()
const discord = require('discord.js')
const { GatewayIntentBits, Collection, GatewayDispatchEvents } = require('discord.js')
const fs = require('fs')
const { EventEmitter } = require('events')
const eventEmitter = new EventEmitter()
module.exports = { eventEmitter }
const client = new discord.Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, GatewayIntentBits.DirectMessages]
})
const debug = require('debug')('MAIN')
const path = require('path')
const { run } = require('./util/taskMngr')
client.on('ready', () => {
debug(`Logged in as ${client.user.tag}!`)
run()
console.log(client._events)
console.log(JSON.stringify(client._events))
})
client.on('userUpdate', (oldUser, newUser) => {
debug('UserUpdate event fired')
const oldUsername = oldUser.username
const newUsername = newUser.username
// Check if the username has changed
if (oldUsername !== newUsername) {
debug(`User '${oldUsername}' changed their username to '${newUsername}'`)
eventEmitter.emit('usernameChange', oldUsername)
}
})
client.commands = new Collection()
const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(path.join(__dirname, 'commands', file))
client.commands.set(command.data.name, command)
debug(`Command ${file} loaded`)
}
client.on('messageCreate', async (message) => {
if (!message.content.startsWith(process.env.PREFIX) || message.author.bot) return
const messageArray = message.content.split(' ')
const cmd = messageArray[0]
const args = messageArray.slice(1)
const commandName = cmd.slice(process.env.PREFIX.length)
const command = client.commands.get(commandName)
if (!command) return
try {
await command.execute(message, args)
} catch (error) {
debug(error)
message.reply('There was an error trying to execute that command!')
}
})
client.login(process.env.TOKEN)
also the "messageCreate" event doesent work
ill try this
that'd be great
MAIN [WS => Manager] Fetched Gateway Information
MAIN URL: wss://gateway.discord.gg
MAIN Recommended Shards: 1 +313ms
MAIN [WS => Manager] Session Limit Information
MAIN Total: 1000
MAIN Remaining: 996 +0ms
MAIN [WS => Shard 0] Connecting to wss://gateway.discord.gg?v=10&encoding=json +1ms
MAIN [WS => Shard 0] Waiting for event hello for 60000ms +4ms
MAIN [WS => Shard 0] Preparing first heartbeat of the connection with a jitter of 0.8242820102342703; waiting 34001ms +178ms
MAIN [WS => Shard 0] Waiting for identify throttle +1ms
MAIN [WS => Shard 0] Identifying
MAIN shard id: 0
MAIN shard count: 1
MAIN intents: 37379
MAIN compression: none +1ms
MAIN [WS => Shard 0] Waiting for event ready for 15000ms +1ms
MAIN [WS => Shard 0] Shard received all its guilds. Marking as fully ready. +202ms
MAIN Logged in as bot1#4088! +1ms
you said the ready event fires though?
14.11.0
you said you had an intents error in your original question. do you have that error anywhere?
no, the error turned out to be because I added some random GatewayDispatchEvents thing inside of the intents list
that was an issue made by me
userUpdate event is not being emitted
good call ^
I just tried changing the username of my alt, that is in the same server as my bot. Nothing printed
oh ok messageCreate event seems to work fine
so its only the userupdate event
let me check on something
so what would I use to check if a user's name updated?
Our userUpdate is extractsd from member_update
Or presence, idr now
Fairly sure you need an intent for that
which one?
The one from the table on discord docs
and I'd still use the UserUpdate event?
the docs wont let me search for anything
which is why im asking here
Which docs
wait this is for actual discord docs?
I was talking about the discord.js.org docs
not working
Not working how
the search doesent work
Doesn't work how
And what is the package you are searching in?
oh it was core package, changing to discord.js works perfectly
could I use guildMemberUpdate to check the username change of a user
Probably?
alright ill check docs
It's probably 0 diff functionally
We extract and emit userUpdate from guild member (or presence, idr) update
we already have that intent added
heres the current function, would this work? ```js
client.on('guildMemberUpdate', (oldMember, newMember) => {
debug("UserUpdate event fired");
const oldUsername = oldMember.user.username;
const newUsername = newMember.user.username;
// Check if the username has changed
if (oldUsername !== newUsername) {
// Username has been changed
debug(User '${oldUsername}' changed their username to '${newUsername}');
// You can perform any actions or emit events here based on the username change
eventEmitter.emit('usernameChange', oldUsername);
}
});
If you didn't change anything from that and ir didn't work, it might be the presence
I just changed username and it didnt work
Actually
Given how we extract into userUpdate, not sure if we still emit the GMU that triggered it
Might, idk
ok so
we used guildMemberUpdate
and it only fired for a username update
like the display name
but not the actual @ username
you'll want to use the dev version to get their username, but the event is firing ?
only firing when we change the display name, not the actual username
The GMU or userupdate
gmu
well before we tried UserUpdate and it didnt fire at all but we'll try again
also whats the package for the discord dev thing?