Fix Welcome @unknown-user with guildMemberAdd

Hey Our bot sends a welcome message with an @tag to the person who just joined the server. (guildMemberAdd) However since recently this has gone wrong and looks like this: (Screenshot) For the code I've tried both userMention and @<user.id> but they seem to yield the same result.
function transformedWelcomeText(data: {
welcomeText: string;
newUser: GuildMember;
guild: Guild;
}): string {
const { guild, newUser, welcomeText } = data;
const changes = new Map<string, string>([
["[ServerName]", guild.name],
["[UserMention]", `<@${newUser.id}>`], // Works better then userMention, as fetching the user might fail.
]);

let result = welcomeText;
changes.forEach((v, k) => {
result = result.replaceAll(k, v);
});

return result;
}
function transformedWelcomeText(data: {
welcomeText: string;
newUser: GuildMember;
guild: Guild;
}): string {
const { guild, newUser, welcomeText } = data;
const changes = new Map<string, string>([
["[ServerName]", guild.name],
["[UserMention]", `<@${newUser.id}>`], // Works better then userMention, as fetching the user might fail.
]);

let result = welcomeText;
changes.forEach((v, k) => {
result = result.replaceAll(k, v);
});

return result;
}
Anyone knows whats going on here?
No description
6 Replies
d.js toolkit
d.js toolkit7d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Lokdei / Bert
Lokdei / BertOP7d ago
npm list discord.js [email protected] C:\Users\Bert\gitroot\cafebaas-discord-bot ├─┬ @discordx/[email protected] │ └── [email protected] deduped ├─┬ @discordx/[email protected] │ └── [email protected] deduped ├── [email protected] └─┬ [email protected] └── [email protected] deduped node -v v20.13.0
Lokdei / Bert
Lokdei / BertOP7d ago
The full source code of the welcome file: https://pastebin.com/fyjaQGtf
Pastebin
@unknown-user on discord server welcome - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
souji
souji7d ago
the <@id> format is resolved to a name to show as "mention" by the viewing client if the user is not cached, it won't be able to resolve it if you allow a notification (allowed_mentions) the user is included in the message itself, is immediately cached and resolved if you do not allow a notification, the user is not included in the message payload and the client has to rely on other events to cache it -# note: "client" means the discord app of the user viewing the welcome text (you) -# note2: if the member leaves your client may also decide to remove the user and it will resolve with "unknown-user" yet again if you are causing notifications with these and the user is still in the server, it may also mean that the id in the <@id> format is not actually one. in your code the only way that could happen is if some other part of your code does an assignment instead of a comparison (have seen people do something like if (member.id = "owner_id_here") {} instead of === or ==) which could result in a corrupted member object when it enters this code - if you can show the raw message data for these test welcome messages, we can easily rule that out though side note: if you are trying to get a collection member from a key (in most cases discord.js that's the ID), you should not use find since it iterates the collection until it finds one - but instead .get(key), so .get(config.discordGuildId) the find predicate is useful when you are trying to find by anything but the key but that's not causing any issues, just a performance hit, especially if the collection is very large
Lokdei / Bert
Lokdei / BertOP6d ago
This is an information goldmine, i'll get to do some testing! Thank you for your detailed answer! ❤️ I've implemented the .get(key) improvement and that's working great! Thank you for the suggestion. I'm still trying to make sense of the allowed_mentions. implementation. I've changed the code to allow a mention (I think) but it's still showing @unkown-user. Do I need to change something in the client constructor as well? Or am i missing something obvious.
const sendOptions: MessageCreateOptions = {
content: greeting,
allowedMentions: { users: [newMember.id] },
components: [],
};

if (
config.enableWelcomeClanOnboarding ||
config.enableWelcomeFriendOnboarding
) {
sendOptions.components = [joinButtons];
}

try {
logger.info("Sending welcome text to welcome channel");
await welcomeChannel.send(sendOptions);
} catch (error) {
logger.error("Unable to send welcome text to welcome channel", error);
}
const sendOptions: MessageCreateOptions = {
content: greeting,
allowedMentions: { users: [newMember.id] },
components: [],
};

if (
config.enableWelcomeClanOnboarding ||
config.enableWelcomeFriendOnboarding
) {
sendOptions.components = [joinButtons];
}

try {
logger.info("Sending welcome text to welcome channel");
await welcomeChannel.send(sendOptions);
} catch (error) {
logger.error("Unable to send welcome text to welcome channel", error);
}
souji
souji6d ago
assign the return of the awaited #send to something and log it - you should have the new member in message.mentions.users from what i can see here that should work, provided they are still on the server when you check also consider the debug steps regarding logging the result and making sure those ids are what you expect them to be

Did you find this page helpful?