Adam
Adam
DIAdiscord.js - Imagine an app
Created by Adam on 5/29/2024 in #djs-questions
DJS fetch function never ending?
Hey! I'm making a bot for my boss that checks every <interval> for staff activity and I came accross an issue: During the check execution, suddenly, everything stopped working fine, let me explain. I tested while I was making it, everything worked. Woke up today and worked on it and it stopped working correctly: it is stuck at a members.fetch() function, no code runs after it even though it worked fine before. Here is the piece of code where the error is found:
export async function runCheck(data: IGuild, check: ICheck, client: Bot) {
const nextCheck = data.nextActivityCheck;
console.log("ran check 1")
const now = Date.now();
if (nextCheck < now || !nextCheck) {
console.log("ran check 2") // this code runs
data.nextActivityCheck = now + data.activityCheckInterval;
const members = await client.guilds.cache.get(data._id)?.members.fetch().catch(() => null);
console.log(members)
console.log("uwu") // ! this code is not executed, with the previous line too
// I tried doing a little debugging and found out it blocks on .fetch
if (!members) return;
console.log("ran check 3")
const staffList = members.filter((m) => m.roles.cache.has(data.staffRole));
data.staffList = [
...data.staffList,
...staffList.filter(
(staff) => !data.staffList.some((existingStaff) => existingStaff.id === staff.id)
).map((m) => ({
id: m.id,
lastActivity: data.staffList.find((s) => s.id === m.id)?.lastActivity || 0
}))
];

await data.save();
console.log("ran check 4")

check.interval = setTimeout(() => runCheck(data, check, client), data.activityCheckInterval);
check.activityTimeout = setTimeout(() => activityTimeout(data, client), data.activityCheckTimeout);

await executeAction(data, client);
console.log("ran check 5")

return;
}
}
export async function runCheck(data: IGuild, check: ICheck, client: Bot) {
const nextCheck = data.nextActivityCheck;
console.log("ran check 1")
const now = Date.now();
if (nextCheck < now || !nextCheck) {
console.log("ran check 2") // this code runs
data.nextActivityCheck = now + data.activityCheckInterval;
const members = await client.guilds.cache.get(data._id)?.members.fetch().catch(() => null);
console.log(members)
console.log("uwu") // ! this code is not executed, with the previous line too
// I tried doing a little debugging and found out it blocks on .fetch
if (!members) return;
console.log("ran check 3")
const staffList = members.filter((m) => m.roles.cache.has(data.staffRole));
data.staffList = [
...data.staffList,
...staffList.filter(
(staff) => !data.staffList.some((existingStaff) => existingStaff.id === staff.id)
).map((m) => ({
id: m.id,
lastActivity: data.staffList.find((s) => s.id === m.id)?.lastActivity || 0
}))
];

await data.save();
console.log("ran check 4")

check.interval = setTimeout(() => runCheck(data, check, client), data.activityCheckInterval);
check.activityTimeout = setTimeout(() => activityTimeout(data, client), data.activityCheckTimeout);

await executeAction(data, client);
console.log("ran check 5")

return;
}
}
3 replies
DIAdiscord.js - Imagine an app
Created by Adam on 1/17/2024 in #djs-questions
avatarURL function is broken?
Hey. I've been making a website with an integration through a Discord bot to get a staff list and its information. I've came accross an issue with the avatarURL function always returning null (I use an alternative for now, displayAvatarURL(). Here the code of the staff route:
const staffList = this.app.config.discordData.staffList;

let staffData: StaffData[] | undefined = this.app._cache.get("staffData");
if (!staffData) {
staffData = [];
const guild = this.app._client.guilds.cache.get(this.app.config.discordData.guildId) || await this.app._client.guilds.fetch(this.app.config.discordData.guildId);
for (const staff of staffList) {
const user = guild.members.cache.get(staff) || await guild.members.fetch(staff);
console.log(user.displayAvatarURL())
staffData.push({
staff: user,
highestRole: user.roles.highest,
avatar: user.avatarURL() as string
});
}

this.app._cache.set("staffData", staffData);
}

res.render("staff", { title: "Staff", data: staffData });
const staffList = this.app.config.discordData.staffList;

let staffData: StaffData[] | undefined = this.app._cache.get("staffData");
if (!staffData) {
staffData = [];
const guild = this.app._client.guilds.cache.get(this.app.config.discordData.guildId) || await this.app._client.guilds.fetch(this.app.config.discordData.guildId);
for (const staff of staffList) {
const user = guild.members.cache.get(staff) || await guild.members.fetch(staff);
console.log(user.displayAvatarURL())
staffData.push({
staff: user,
highestRole: user.roles.highest,
avatar: user.avatarURL() as string
});
}

this.app._cache.set("staffData", staffData);
}

res.render("staff", { title: "Staff", data: staffData });
And here is where I login the bot in the structures/app.ts file:
private initializeClient() {
this._client.on("ready", () => {
this.logger.info(`Connected to Discord through the bot ${this._client.user?.tag}`, "Discord");
});

this._client.login(this.config.discordData.botToken).catch(err => {
this.logger.error(`Discord connection failed; This may cause an outage.`);
});
}
private initializeClient() {
this._client.on("ready", () => {
this.logger.info(`Connected to Discord through the bot ${this._client.user?.tag}`, "Discord");
});

this._client.login(this.config.discordData.botToken).catch(err => {
this.logger.error(`Discord connection failed; This may cause an outage.`);
});
}
8 replies
DIAdiscord.js - Imagine an app
Created by Adam on 1/27/2023 in #djs-questions
Websocket closing after some time (unable to reconnect)
Hey, I've been developing the bot, and for some reasons, some time after starting it, it disconnects, no error log or no crash, just debug logs with errors
21 replies