getting guild object

anyone knows how can I check if any kind of event happened and get the guild object? my bot only works on guilds so direct messages can be ignored.
import { Client } from "discord.js";
import { readdirSync } from "fs";
import { join } from "path";
import { color } from "../functions";
import { BotEvent } from "../types";

module.exports = (client: Client) => {
let eventsDir = join(__dirname, "../events")

readdirSync(eventsDir).forEach(file => {
if (!file.endsWith(".js")) return;
let event: BotEvent = require(`${eventsDir}/${file}`).default
event.once ?
client.once(event.name, (...args) => event.execute(...args))
:
client.on(event.name, (...args) => {
console.log(...args);
const isApiOffline = checkApiAvailability();
if (isApiOffline) {
//pseudo code
//smthg like this but I dont want to make a switch case over all event types to get the guild object.
const guildId = args[0].getEvent().guild;
const guild = client.guilds.cache.get(guildId);
const owner = guild?.fetchOwner();
owner.send("API is offline");
}

});
})
}
function checkApiAvailability() {
return false;
}
import { Client } from "discord.js";
import { readdirSync } from "fs";
import { join } from "path";
import { color } from "../functions";
import { BotEvent } from "../types";

module.exports = (client: Client) => {
let eventsDir = join(__dirname, "../events")

readdirSync(eventsDir).forEach(file => {
if (!file.endsWith(".js")) return;
let event: BotEvent = require(`${eventsDir}/${file}`).default
event.once ?
client.once(event.name, (...args) => event.execute(...args))
:
client.on(event.name, (...args) => {
console.log(...args);
const isApiOffline = checkApiAvailability();
if (isApiOffline) {
//pseudo code
//smthg like this but I dont want to make a switch case over all event types to get the guild object.
const guildId = args[0].getEvent().guild;
const guild = client.guilds.cache.get(guildId);
const owner = guild?.fetchOwner();
owner.send("API is offline");
}

});
})
}
function checkApiAvailability() {
return false;
}
3 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!
monbrey
monbrey7mo ago
What exactly do you mean by "any kind of event" You can listen to specific events and if they happened in a guild, they will have a reference to that guild in some way You're going to need to know which event it is to know which properties you're actually getting as arguments
Skylife
Skylife7mo ago
just when an event happens i want to check if my api is online - like a middleware so if not i can return early and notify the server owner once.
Want results from more Discord servers?
Add your server