#please can someone tell me why my code doesn't work:

110 messages · Page 1 of 1 (latest)

valid sierra
#

module.exports = { name: 'guildMemberAdd', async execute(member) { console.log(${member} has joined the server`);
const welcomeRole = await member.guild.roles.cache.find(role => role.name === 'member');
await member.roles.add(welcomeRole);

    const welcomeChannel = await member.guild.channels.cache.find(channel => channel.name === 'welcome');
    await welcomeChannel.fetch();
    welcomeChannel.send(`Welcome ${member.user}, hope you have a good time here!``)
    
}

}
`

the welcome message or role doesn't work, and yes I have made a member role and a channel called welcome.

frank mossBOT
#

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

valid sierra
#

nothing returns

#

and btw in the welcomeChannel.send(Welcome ${member.user}, hope you have a good time here!)`. I do have `` these but the code block doesnt work with it in dicsord

frozen blade
#

Do you have the GuildMembers Client intent?

valid sierra
sweet salmon
#

In your client constructor?

valid sierra
#

is there any delay for it to recognise it as i didnt as these long ago

frozen blade
#

Adding GatewayIntentBits.GuildMembers should take effect immediately

valid sierra
#

then why doesnt it worj

#

work

#

error now

late fiber
valid sierra
#

wdym

late fiber
#

node . or node <what ever your main file is> (ex. index.js)

valid sierra
#

yes ik

#

but when i do that

#

nothing even happens

#

even when i do node . my bot works but nothing actually happens in the welcome channel and it doesnt even give them the member role when i have a member role

#

anyone know why?

velvet latch
#

Sounds like you don't have your project structured properly

valid sierra
#

what?

valid sierra
velvet latch
#

Not without seeing your entire project. Basically you need an entry point into the project, and then code that will handle certain things like events and commands

velvet latch
valid sierra
# velvet latch Show index.js code, this would be your entry point

`const { token } = require('./config.json');
const { Client, Events, GatewayIntentBits, SlashCommandBuilder, Collection } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for(const file of eventFiles){
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if(event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args))
}
}

client.login(token);

`

velvet latch
#

Try node index.js

valid sierra
#

no error

velvet latch
#

Might be worth adding some logging and error catching to see if anything is going on

valid sierra
#

logging which part tho

#

there is a lot

velvet latch
#

I would start with logging client after it's logged

valid sierra
velvet latch
valid sierra
#

so console.log(client)

velvet latch
#

Yes

valid sierra
#

yes i meant log

#

it put error

valid sierra
valid sierra
#

thats all of it

velvet latch
velvet latch
valid sierra
#

in the event folder

#

so where would i put the console.log in this?:
`const { Client, Collection, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

module.exports = {
name: 'guildMemberAdd',
async execute(member) {
console.log(${member} has joined the server);
const welcomeRole = await member.guild.roles.cache.find(role => role.name === 'member');
await member.roles.add(welcomeRole);

    const welcomeChannel = await member.guild.channels.cache.find(channel => channel.name === 'welcome');
    await welcomeChannel.fetch();
    welcomeChannel.send(`Welcome ${member.user}, hope you have a good time here!`)

}

}
`

velvet latch
#

You said

even when i do node . my bot works but nothing actually happens in the welcome channel and it doesnt even give them the member role when i have a member role

How do you know this to be the case? Are there any errors?

valid sierra
#

well ik because nothing happens in the welcome channel and it doesnt give the member a role..

velvet latch
valid sierra
#

in my index.js

velvet latch
#

So I ask again..
You said

even when i do node . my bot works but nothing actually happens in the welcome channel and it doesnt even give them the member role when i have a member role

How do you know this to be the case? Are there any errors?

valid sierra
#

no there are no errors

velvet latch
#

So you need to diagnose what is going on. Try logging the event you expect to be fired and see if you get any output

valid sierra
velvet latch
#

I would start by just logging member

valid sierra
valid sierra
valid sierra
velvet latch
#

GatewayIntentBits.GuildMembers

valid sierra
#

..

valid sierra
#

oh

#

so

velvet latch
#

Yeah, add GatewayIntentBits.GuildMembers

valid sierra
#

`const client = new Client({ intents: [GatewayIntentBits.GuildMembers] });

#

so that

velvet latch
#

Keep the Guilds intent

valid sierra
#

do i keep the GatewayIntentBits.Guilds

velvet latch
#

Yes

valid sierra
#

and then , GatewayIntentBits.GuildMembers

velvet latch
#

Yep

#

Then check if you can log member in the guildMemberAdd event

valid sierra
#

nope nothing:
`const { Client, Collection, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] });

module.exports = {
name: 'guildMemberAdd',
async execute(member) {
console.log(${member} has joined the server);
console.log(member)
const welcomeRole = await member.guild.roles.cache.find(role => role.name === 'member');
await member.roles.add(welcomeRole);

    const welcomeChannel = await member.guild.channels.cache.find(channel => channel.name === 'welcome');
    await welcomeChannel.fetch();
    welcomeChannel.send(`Welcome ${member.user}, hope you have a good time here!`)
    console.log(welcomeChannel)
    console.log(welcomeRole)
    console.log(member)
    
}

}`

#

if im doing it right..

velvet latch
#

In your main file, log eventFiles and make sure the correct event files are found

valid sierra
#

they are

velvet latch
#

And how are you testing the event?

valid sierra
#

console.log(eventFiles)

valid sierra
sweet salmon
#

Did someone join your guild while your bot is running after you changed its code?

valid sierra
#

yes i have my alt joiining

#

but if i leave again

#

and join

#

does it still work?

velvet latch
#

It will

sweet salmon
#

It would fire again, yes

valid sierra
#

ok

#

so why wont it work..

velvet latch
valid sierra
#

omg thank you so much

#

it finally works

velvet latch
# valid sierra it finally works

Great, now you will likely run into other issues as different events require different intents, check this
https://discord-api-types.dev/api/discord-api-types-v10/enum/GatewayIntentBits
https://discord.com/developers/docs/topics/gateway#list-of-intents

discord-api-types documentation

discord-api-types is a simple Node/Deno module that brings up to date typings for Discord's API

Discord Developer Portal

Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.

valid sierra
#

so every time i make a new one

#

i have to add the thing i want in my intents

#

in my main and the file i want it in

velvet latch
valid sierra
#

so likeGatewayIntentBits.Guilds

velvet latch
valid sierra
burnt pendantBOT
#

• Websocket intents limit events and decrease memory usage: learn more
• See what intents you need here