userUpdate event is not being emitted

I want to have an UpdateUser event but I got an intents error.
46 Replies
d.js toolkit
d.js toolkit2y ago
• 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.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
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);
}
});
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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
so do I just do oldMember.username? also, the event doesent fire at all
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
alright thanks for the tip but the event wont fire at all
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
yeah the setup seems fine according to docs other commands and event work for example the ready event work fine
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
all of the events are being handled in the root file, index.js
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
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) => {
// ...
}
// 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) => {
// ...
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
sure 1 sec
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
[Object: null prototype] {
shardDisconnect: [Function (anonymous)],
ready: [Function (anonymous)],
userUpdate: [Function (anonymous)],
messageCreate: [AsyncFunction (anonymous)]
}
[Object: null prototype] {
shardDisconnect: [Function (anonymous)],
ready: [Function (anonymous)],
userUpdate: [Function (anonymous)],
messageCreate: [AsyncFunction (anonymous)]
}
stringifying it returns an empty object
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
yes
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
no console.log didnt work either
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
1 sec
const client = new discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessages,
],
});
const client = new discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessages,
],
});
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
there was also no errors seems like the event just didnt get recognised
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
alright gimme a moment
d.js docs
d.js docs2y ago
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)
client
.on("debug", console.log)
.on("warn", console.log)
- Note: if you initialize your Client as bot or other identifiers you need to use these instead of client - If the output is too long to post consider using a bin instead: gist | paste.gg | sourceb.in | hastebin
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
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)
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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
yep but the onmessage and onuserupdate event doesent its weird
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
14.11.0
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
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 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
so what would I use to check if a user's name updated? which one? and I'd still use the UserUpdate event? the docs wont let me search for anything which is why im asking here wait this is for actual discord docs? I was talking about the discord.js.org docs not working the search doesent work
supg
supgOP2y ago
supg
supgOP2y ago
I put in anything and it always says no result found oh it was core package, changing to discord.js works perfectly could I use guildMemberUpdate to check the username change of a user alright ill check docs
supg
supgOP2y ago
we already have that intent added heres the current function, would this work?
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);
}
});
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);
}
});
I just changed username and it didnt work ok so we used guildMemberUpdate and it only fired for a username update like the display name but not the actual @ username
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
supg
supgOP2y ago
only firing when we change the display name, not the actual username 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? alright we're gonna try to swap to the dev version and see how it goes alrigth we tried some things and it didnt work, imma just look into this some other time, thx for all of the help though imma close this thread
Want results from more Discord servers?
Add your server