#userUpdate event is not being emitted

125 messages · Page 1 of 1 (latest)

karmic moth
#

I want to have an UpdateUser event but I got an intents error.

weak crownBOT
#

• 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.

restive cedar
#

you don't need any intents for the userUpdate event

karmic moth
#

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

restive cedar
#

oldMember is already a user so .user will not work

karmic moth
#

so do I just do oldMember.username?

#

also, the event doesent fire at all

restive cedar
#

you could yes, but i suggest changing oldMember and newMember to oldUser and newUser so you dont confuse users and members

karmic moth
#

but the event wont fire at all

restive cedar
#

is your bot online?

karmic moth
#

yeah

#

the setup seems fine according to docs

#

other commands and event work

#

for example the ready event work fine

restive cedar
#

do you have an event handler? or is that event in one file only

karmic moth
#

all of the events are being handled in the root file, index.js

restive cedar
#

mind if i ask what eventEmitter is?

karmic moth
#

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) => {
  // ...
}
restive cedar
#

alright

#

please log <Client>._events and show the output of that

karmic moth
#

sure 1 sec

restive cedar
#

preferably in the ready event since you said that does fire

karmic moth
#
[Object: null prototype] {
  shardDisconnect: [Function (anonymous)],
  ready: [Function (anonymous)],
  userUpdate: [Function (anonymous)],
  messageCreate: [AsyncFunction (anonymous)]
}
#

stringifying it returns an empty object

restive cedar
#

seems like the event is being registered

#

what does debug do?

#

does that log messages in the console?

karmic moth
#

yes

restive cedar
#

does debug("UserUpdate event fired"); log in the console?

karmic moth
#

no

#

console.log didnt work either

restive cedar
#

can you show your client's intents

karmic moth
#

1 sec

#
const client = new discord.Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.DirectMessages,
  ],
});
restive cedar
#

interesting..

karmic moth
#

there was also no errors

#

seems like the event just didnt get recognised

restive cedar
#

do you mind showing your entire index.js

#

if its a lot, please use a bin

karmic moth
#

alright gimme a moment

fringe oysterBOT
#

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 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
restive cedar
#

also please do this as well

karmic moth
# restive cedar do you mind showing your entire index.js
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

restive cedar
#

that'd be great

karmic moth
#

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

restive cedar
#

you said the ready event fires though?

karmic moth
#

yep

#

but the onmessage and onuserupdate event doesent

#

its weird

restive cedar
#

interesting

#

whats your djs version? npm ls discord.js

karmic moth
#

14.11.0

restive cedar
#

you said you had an intents error in your original question. do you have that error anywhere?

karmic moth
#

that was an issue made by me

restive cedar
#

userUpdate event is not being emitted

karmic moth
#

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

restive cedar
#

let me check on something

urban magnet
#

Uh

#

Our userUpdate is not discord's user_update

karmic moth
urban magnet
#

Our userUpdate is extractsd from member_update

#

Or presence, idr now

#

Fairly sure you need an intent for that

karmic moth
urban magnet
#

The one from the table on discord docs

karmic moth
#

and I'd still use the UserUpdate event?

karmic moth
#

which is why im asking here

urban magnet
#

Which docs

karmic moth
#

wait this is for actual discord docs?

#

not working

urban magnet
#

Not working how

karmic moth
#

the search doesent work

urban magnet
#

Doesn't work how

karmic moth
#

I put in anything

#

and it always says no result found

urban magnet
#

And what is the package you are searching in?

karmic moth
#

oh it was core package, changing to discord.js works perfectly

urban magnet
#

Ok, next

#

Yes, discord has a list of what event needs what intent

#

On their docs

karmic moth
#

could I use guildMemberUpdate to check the username change of a user

urban magnet
#

Probably?

karmic moth
#

alright ill check docs

urban magnet
#

It's probably 0 diff functionally

#

We extract and emit userUpdate from guild member (or presence, idr) update

karmic moth
#

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);

}
});

urban magnet
#

If you didn't change anything from that and ir didn't work, it might be the presence

karmic moth
urban magnet
#

Actually

#

Given how we extract into userUpdate, not sure if we still emit the GMU that triggered it

#

Might, idk

karmic moth
#

ok so

#

we used guildMemberUpdate

#

and it only fired for a username update

#

like the display name

#

but not the actual @ username

restive cedar
#

you'll want to use the dev version to get their username, but the event is firing ?

karmic moth
urban magnet
#

The GMU or userupdate

karmic moth
urban magnet
#

Then try the other one

#

Or both

#

Idk

karmic moth
#

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?

urban magnet
#

Jist @dev version of d.js

#

Again, i am not sure if user gets updated via member update or presence update.

karmic moth
#

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