level up message

so I am having an issue with the level up message not sending... the console doesn't throw and errors or anything so I am just confused why it doesn't send a levelup message
const newExp = await DatabaseHelper.addMemberExp(
message.author.id,
PER_MESSAGE
);
if (newExp && message.member) {
logger.info(
`Added exp to ${message.author.username} - they now have ${newExp}`
);
await ExperienceHelper.ensureUserHasLevelRoles(
message.member,
ExperienceHelper.getLevel(newExp)
);
if (
ExperienceHelper.getLevel(newExp - PER_MESSAGE) <
ExperienceHelper.getLevel(newExp)
) {
const spamCh = message.guild?.channels.cache.get(levelupChannel);
console.log(`spamCh: ${spamCh}`);
console.log(`spamCh type: ${spamCh?.type}`);
if (spamCh) {
if (spamCh.type === "GUILD_TEXT") {
spamCh.send({
content: `Hey ${message.author.toString()}, you've leveled up! You are now level ${ExperienceHelper.getLevel(
newExp
)}`,
});
} else {
console.error("Level up channel is not a text channel.");
}
} else {
console.error("Level up channel not found.");
}
}
}
}
const newExp = await DatabaseHelper.addMemberExp(
message.author.id,
PER_MESSAGE
);
if (newExp && message.member) {
logger.info(
`Added exp to ${message.author.username} - they now have ${newExp}`
);
await ExperienceHelper.ensureUserHasLevelRoles(
message.member,
ExperienceHelper.getLevel(newExp)
);
if (
ExperienceHelper.getLevel(newExp - PER_MESSAGE) <
ExperienceHelper.getLevel(newExp)
) {
const spamCh = message.guild?.channels.cache.get(levelupChannel);
console.log(`spamCh: ${spamCh}`);
console.log(`spamCh type: ${spamCh?.type}`);
if (spamCh) {
if (spamCh.type === "GUILD_TEXT") {
spamCh.send({
content: `Hey ${message.author.toString()}, you've leveled up! You are now level ${ExperienceHelper.getLevel(
newExp
)}`,
});
} else {
console.error("Level up channel is not a text channel.");
}
} else {
console.error("Level up channel not found.");
}
}
}
}
17 Replies
d.js toolkit
d.js toolkit7mo 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!
duck
duck7mo ago
if nothing is being logged, it's not reaching those if statements where it would send the message in the first place
d.js docs
d.js docs7mo ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. - Once you do, log relevant values and if-conditions - More sophisticated debugging methods are breakpoints and runtime inspections: learn more
NyR
NyR7mo ago
Also in addition, assuming code is reaching there, channel type is a number, use ChannelType enum
duck
duck7mo ago
(see the post tags)
NyR
NyR7mo ago
Ah, well... sometimes i forget there are other versions my bad
Corsy
CorsyOP7mo ago
There are no error logs or anything, everything else is working within that code except for that Just in total confusion tbh lol
duck
duck7mo ago
which is why you should do this to see where execution stops
Corsy
CorsyOP7mo ago
alright i'll add it in a few different places and see what happens so I have added some in
console.log("this is working #3")
// Add member exp
const newExp = await DatabaseHelper.addMemberExp(
message.author.id,
PER_MESSAGE
);
if (newExp && message.member) {
logger.info(
`Added exp to ${message.author.username} - they now have ${newExp}`
);
await ExperienceHelper.ensureUserHasLevelRoles(
message.member,
ExperienceHelper.getLevel(newExp)
);
console.log("this isworking #2")
if (
ExperienceHelper.getLevel(newExp - PER_MESSAGE) <
ExperienceHelper.getLevel(newExp)

) {
const spamCh = message.guild?.channels.cache.get(levelupChannel);
console.log("this is working #1")
if (spamCh) {
if (spamCh.type === "GUILD_TEXT") {
spamCh.send({
content: `Hey ${message.author.toString()}, you've leveled up! You are now level ${ExperienceHelper.getLevel(
newExp
)}`,
});
} else {
console.error("Level up channel is not a text channel.");
}
} else {
console.error("Level up channel not found.");
}
}
}
}
console.log("this is working #3")
// Add member exp
const newExp = await DatabaseHelper.addMemberExp(
message.author.id,
PER_MESSAGE
);
if (newExp && message.member) {
logger.info(
`Added exp to ${message.author.username} - they now have ${newExp}`
);
await ExperienceHelper.ensureUserHasLevelRoles(
message.member,
ExperienceHelper.getLevel(newExp)
);
console.log("this isworking #2")
if (
ExperienceHelper.getLevel(newExp - PER_MESSAGE) <
ExperienceHelper.getLevel(newExp)

) {
const spamCh = message.guild?.channels.cache.get(levelupChannel);
console.log("this is working #1")
if (spamCh) {
if (spamCh.type === "GUILD_TEXT") {
spamCh.send({
content: `Hey ${message.author.toString()}, you've leveled up! You are now level ${ExperienceHelper.getLevel(
newExp
)}`,
});
} else {
console.error("Level up channel is not a text channel.");
}
} else {
console.error("Level up channel not found.");
}
}
}
}
the one that does not show up on the console is console.log("this is working#1") any ideas?
duck
duck7mo ago
but #2 does log?
Corsy
CorsyOP7mo ago
I have clarified that the levelupchannel is correct #2 does #3 and #2 does
duck
duck7mo ago
then it sounds like ExperienceHelper.getLevel(newExp - PER_MESSAGE) < ExperienceHelper.getLevel(newExp) is false this is leaving the scope of this channel
Corsy
CorsyOP7mo ago
So where would I go about fixing this exactly? I am just stuck here tbh, been trying to look at it multiple times, changing things etc
duck
duck7mo ago
no idea what ExperienceHelper is or how getLevel is written, but you should probably start there or at the very least log what each call returns (as mentioned here)
Corsy
CorsyOP7mo ago
import { GuildMember, MessageAttachment } from "discord.js";
import { logger } from "../papertrail";
import { Rank } from "canvacord";

export const LEVEL_VALUES = [
0, 3000, 6000, 9000, 12000, 15000, 18000, 21000, 24000, 30000,
];
// export const LEVEL_VALUES = [
// 0, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000,
// ];
export const PER_HOUR_PASSIVE_CURRENCY = [
100, 150, 200, 250, 300, 350, 450, 500, 550, 600,
];
export const LEVELING_ROLE_IDS = [
"1246186080848314398",
"1246186125542821981",
"1246186149987225640",
"1246186173664202765",
"1246186196561039520",
"1246186221449773076",
"1246186249518186536",
"1246186279750729758",
"1246186306682228746",
"1246186327062614096",
];
const ROLE_CARD_COLORS = ["#4169e1"];

export default class ExperienceHelper {
public static getLevel(exp: number): number {
for (let i = 0; i < LEVEL_VALUES.length; i++) {
if (exp < LEVEL_VALUES[i]) return i;
}
return LEVEL_VALUES.length;
}

public static getPassiveCurrencyPerHour(level: number): number {
return PER_HOUR_PASSIVE_CURRENCY[level - 1];
}

public static async ensureUserHasLevelRoles(
member: GuildMember,
level: number
) {
const roles = member.roles.cache;
let i = 1;
for (const roleId of LEVELING_ROLE_IDS) {
let levelIdx = i++;
if (!roles.has(roleId)) {
if (levelIdx == level) {
logger.info(`Adding level ${levelIdx} role to ${member.user.tag}`);
await member.roles.add(roleId);
}
} else {
if (levelIdx !== level) {
logger.info(
`Removing level ${levelIdx} role from ${member.user.tag}`
);
await member.roles.remove(roleId);
}
}
}
}

public static getExpToNextLevel(exp: number): number {
const level = ExperienceHelper.getLevel(exp);
return LEVEL_VALUES[level] - exp;
}

public static getExpToNextLevelPercent(exp: number): number {
const level = ExperienceHelper.getLevel(exp);
const nextLevel = LEVEL_VALUES[level];
const percent = (nextLevel - exp) / nextLevel;
return percent;
}

public static getExpToNextLevelPercentString(exp: number): string {
const percent = ExperienceHelper.getExpToNextLevelPercent(exp);
return `${Math.round(percent * 100)}%`;
}

public static async getExperienceCard(
member: GuildMember,
exp: number,
rank: number
): Promise<MessageAttachment[]> {
const level = ExperienceHelper.getLevel(exp);
const nextLevel = LEVEL_VALUES[level] ?? 0;
const percent = nextLevel > 0 ? (nextLevel - exp) / nextLevel : 1;
const percentString = `${Math.round(percent * 100)}%`;
const avatarUrl =
member.avatarURL({ format: "png", size: 128 }) ||
member.user.avatarURL({ format: "png", size: 128 }) ||
"https://cdn.discordapp.com/embed/avatars/0.png";
const card = new Rank()
.setAvatar(avatarUrl)
.setUsername(member.displayName)
.setLevel(level)
.setCurrentXP(exp)
.setRequiredXP(nextLevel)
// .setProgressBar(ROLE_CARD_COLORS)
// .setProgressBar("#b170c0")
.setBackground("COLOR", ROLE_CARD_COLORS[0])
.setOverlay("#050303")
// .setProgressBar(percent)
.setRank(rank)
.setCustomStatusColor(ROLE_CARD_COLORS[0]);
const data = await card.build({ fontX: "Manrope", fontY: "Manrope" });
const file = new MessageAttachment(data, "rank.png");
return [file];
}
}
import { GuildMember, MessageAttachment } from "discord.js";
import { logger } from "../papertrail";
import { Rank } from "canvacord";

export const LEVEL_VALUES = [
0, 3000, 6000, 9000, 12000, 15000, 18000, 21000, 24000, 30000,
];
// export const LEVEL_VALUES = [
// 0, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000,
// ];
export const PER_HOUR_PASSIVE_CURRENCY = [
100, 150, 200, 250, 300, 350, 450, 500, 550, 600,
];
export const LEVELING_ROLE_IDS = [
"1246186080848314398",
"1246186125542821981",
"1246186149987225640",
"1246186173664202765",
"1246186196561039520",
"1246186221449773076",
"1246186249518186536",
"1246186279750729758",
"1246186306682228746",
"1246186327062614096",
];
const ROLE_CARD_COLORS = ["#4169e1"];

export default class ExperienceHelper {
public static getLevel(exp: number): number {
for (let i = 0; i < LEVEL_VALUES.length; i++) {
if (exp < LEVEL_VALUES[i]) return i;
}
return LEVEL_VALUES.length;
}

public static getPassiveCurrencyPerHour(level: number): number {
return PER_HOUR_PASSIVE_CURRENCY[level - 1];
}

public static async ensureUserHasLevelRoles(
member: GuildMember,
level: number
) {
const roles = member.roles.cache;
let i = 1;
for (const roleId of LEVELING_ROLE_IDS) {
let levelIdx = i++;
if (!roles.has(roleId)) {
if (levelIdx == level) {
logger.info(`Adding level ${levelIdx} role to ${member.user.tag}`);
await member.roles.add(roleId);
}
} else {
if (levelIdx !== level) {
logger.info(
`Removing level ${levelIdx} role from ${member.user.tag}`
);
await member.roles.remove(roleId);
}
}
}
}

public static getExpToNextLevel(exp: number): number {
const level = ExperienceHelper.getLevel(exp);
return LEVEL_VALUES[level] - exp;
}

public static getExpToNextLevelPercent(exp: number): number {
const level = ExperienceHelper.getLevel(exp);
const nextLevel = LEVEL_VALUES[level];
const percent = (nextLevel - exp) / nextLevel;
return percent;
}

public static getExpToNextLevelPercentString(exp: number): string {
const percent = ExperienceHelper.getExpToNextLevelPercent(exp);
return `${Math.round(percent * 100)}%`;
}

public static async getExperienceCard(
member: GuildMember,
exp: number,
rank: number
): Promise<MessageAttachment[]> {
const level = ExperienceHelper.getLevel(exp);
const nextLevel = LEVEL_VALUES[level] ?? 0;
const percent = nextLevel > 0 ? (nextLevel - exp) / nextLevel : 1;
const percentString = `${Math.round(percent * 100)}%`;
const avatarUrl =
member.avatarURL({ format: "png", size: 128 }) ||
member.user.avatarURL({ format: "png", size: 128 }) ||
"https://cdn.discordapp.com/embed/avatars/0.png";
const card = new Rank()
.setAvatar(avatarUrl)
.setUsername(member.displayName)
.setLevel(level)
.setCurrentXP(exp)
.setRequiredXP(nextLevel)
// .setProgressBar(ROLE_CARD_COLORS)
// .setProgressBar("#b170c0")
.setBackground("COLOR", ROLE_CARD_COLORS[0])
.setOverlay("#050303")
// .setProgressBar(percent)
.setRank(rank)
.setCustomStatusColor(ROLE_CARD_COLORS[0]);
const data = await card.build({ fontX: "Manrope", fontY: "Manrope" });
const file = new MessageAttachment(data, "rank.png");
return [file];
}
}
that is the experiencehelper getlevel is within the same as well I mean nothing else within the code has anything else to do with the levelupmessage either, which is why I am confuse because it did all work before in the past
duck
duck7mo ago
or at the very least log what each call returns
so have you done this?
Corsy
CorsyOP7mo ago
I’ll try it here shortly, got busy last night and am now just getting up So yeah everything seems to be working fine except the end of code
) {
const spamCh = message.guild?.channels.cache.get(levelupChannel);
console.log("this is working #1")
if (spamCh) {
if (spamCh.type === "GUILD_TEXT") {
spamCh.send({
content: `Hey ${message.author.toString()}, you've leveled up! You are now level ${ExperienceHelper.getLevel(
newExp
)}`,
});
} else {
console.error("Level up channel is not a text channel.");
}
} else {
console.error("Level up channel not found.");
}
}
}
}
) {
const spamCh = message.guild?.channels.cache.get(levelupChannel);
console.log("this is working #1")
if (spamCh) {
if (spamCh.type === "GUILD_TEXT") {
spamCh.send({
content: `Hey ${message.author.toString()}, you've leveled up! You are now level ${ExperienceHelper.getLevel(
newExp
)}`,
});
} else {
console.error("Level up channel is not a text channel.");
}
} else {
console.error("Level up channel not found.");
}
}
}
}
This is the only thing that does not come up in logs but also doesn't error either, which is weird I have done that. I put a log in the experiencehelper.ts where each return would be and they all work I've gotten it to work.
const spamCh = await message.guild?.channels.fetch(levelupChannel);
if (spamCh && spamCh.isText())
const spamCh = await message.guild?.channels.fetch(levelupChannel);
if (spamCh && spamCh.isText())
Changed it to that and it seemed to work Sorry for the late reply I been busy
Want results from more Discord servers?
Add your server