How do I make my bot "greet" a new member?

So I have this projeect structure.
src/
commands/
ping.js - WORKING command
listeners/
guildAddMember.js - the file I work on the welcome message in
index.js
package.json
src/
commands/
ping.js - WORKING command
listeners/
guildAddMember.js - the file I work on the welcome message in
index.js
package.json
The code of guildAddMember.js file:
const { Listener } = require('@sapphire/framework');
const { Embed, EmbedBuilder } = require('discord.js');

class WelcomeListener extends Listener {
constructor(context, options) {
super(context, {
...options,
event: 'guildMemberAdd'
});
}

async run(member) {
const { client } = this.container;

const newcomer = this.container.guild.member;
const channel = client.channels.cache.get('channel id IS provided and is correct');

const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle(`Welcome, ${newcomer}`)
.setDescription('We\'re glad you joined us.')
.setTimestamp()

channel.send({ embeds: [embed] });
}
}

module.exports = {
WelcomeListener
};
const { Listener } = require('@sapphire/framework');
const { Embed, EmbedBuilder } = require('discord.js');

class WelcomeListener extends Listener {
constructor(context, options) {
super(context, {
...options,
event: 'guildMemberAdd'
});
}

async run(member) {
const { client } = this.container;

const newcomer = this.container.guild.member;
const channel = client.channels.cache.get('channel id IS provided and is correct');

const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle(`Welcome, ${newcomer}`)
.setDescription('We\'re glad you joined us.')
.setTimestamp()

channel.send({ embeds: [embed] });
}
}

module.exports = {
WelcomeListener
};
index.js file:
const { SapphireClient } = require('@sapphire/framework');
const { GatewayIntentBits} = require('discord.js');
const { token } = require('../config.json');
const colors = require('colors');

const client = new SapphireClient({
intents: [
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
],

loadMessageCommandListeners: true,
});

client.login(token).then((token) =>
client.user.setPresence({ activities: [{ name: 'with your mind' }], status: 'online' })
);
const { SapphireClient } = require('@sapphire/framework');
const { GatewayIntentBits} = require('discord.js');
const { token } = require('../config.json');
const colors = require('colors');

const client = new SapphireClient({
intents: [
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
],

loadMessageCommandListeners: true,
});

client.login(token).then((token) =>
client.user.setPresence({ activities: [{ name: 'with your mind' }], status: 'online' })
);
For some reason, when I join with my alt, the bot does not react with any message. I need help finding out why, thanks!
10 Replies
Favna
Favna2mo ago
Check your intents. I think you need something along the lines of Guild member intent. It should give you an autocompletable list because it's an enum Also does that ping command work? If yes that rules out folder config problems
mizorint
mizorint2mo ago
Nope, all the intents are enabled. On discord developer portal all 3 privileged intents are on, but now I'm not sure about the ones I use in my index file. I can't check them out now though. Also yes, it does work.
Favna
Favna2mo ago
Do you define this.container.guild somewhere? Because that's normally invalid. In fact, newcomer can be replaced by the member from the parameters. FYI if you use TypeScript instead of JavaScript a thing like this would be alerted to you. Also if that is indeed the case you would also see this through the logs.
mizorint
mizorint2mo ago
Oh, I didn't define this.comtainer.guild. but I will try this and let you know if this helps. About the logs, nothing was in the console besides the slash application command registry and my another listener's notification that the bot is ready Oops, didnt mean to ping you, forgot to uncheck the tag thing.
Favna
Favna2mo ago
uhm oh I dont think we have a default impl for listenerError event
mizorint
mizorint2mo ago
So does it mean that the listener error will not be logged?
Favna
Favna2mo ago
not by default no. You can add your own listener with the event listenerError to listen for errors from other listeners
mizorint
mizorint2mo ago
Will do as well. I think these are the only possible ways to solve the problem so far?
Favna
Favna2mo ago
The way javascript (or any programming language for that matter) works is if an errors is thrown for a line then anything below that line no longer gets executed and an error will be thrown where I pointed it out specifically
Uncaught TypeError: Cannot read property of undefined (reading 'member')
mizorint
mizorint2mo ago
Thanks. Ill get to it in the morning
Want results from more Discord servers?
Add your server