What is the best way to detect a change in server ownership when the bot happens to be offline?

Hi, I am using Discord.JS and am trying to figure out how I might change a server in my database's owner_id field when the owner id is found to be different. Here is an example:
// ClientReady.ts

try {
// Get database servers (my api)
const databaseServersResponse: Response = await ApiHandler.get(`http://api:${EnvHandler.get("API_PORT")}/servers`);
const databaseServers: Server[] = (await databaseServersResponse.json() as APIResponse).data?.servers;

// Get servers bot is in (discord.js)
const botServers: Record<string, any>[] = await ShardingHandler.getAllGuilds(botClient);
const botServerIds: string[] = botServers.map((server: Record<string, any>) => server.id);

// Get servers bot is not in
const notFoundServers: Server[] = databaseServers.filter((server: Server) => !botServerIds.includes(server.id));

for (const foundServer of botServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${foundServer.ownerId}/servers/${foundServer.id}`, { has_bot: true });
}


// Here is where I am struggling. I don't think I can get the new owner id, because the bot is not in the guild.
for (const notFoundServer of notFoundServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${notFoundServer.owner_id}/servers/${notFoundServer.id}`, { has_bot: false });
}

console.log("Successfully initizalied the bot and updated all necessary data.");
// ClientReady.ts

try {
// Get database servers (my api)
const databaseServersResponse: Response = await ApiHandler.get(`http://api:${EnvHandler.get("API_PORT")}/servers`);
const databaseServers: Server[] = (await databaseServersResponse.json() as APIResponse).data?.servers;

// Get servers bot is in (discord.js)
const botServers: Record<string, any>[] = await ShardingHandler.getAllGuilds(botClient);
const botServerIds: string[] = botServers.map((server: Record<string, any>) => server.id);

// Get servers bot is not in
const notFoundServers: Server[] = databaseServers.filter((server: Server) => !botServerIds.includes(server.id));

for (const foundServer of botServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${foundServer.ownerId}/servers/${foundServer.id}`, { has_bot: true });
}


// Here is where I am struggling. I don't think I can get the new owner id, because the bot is not in the guild.
for (const notFoundServer of notFoundServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${notFoundServer.owner_id}/servers/${notFoundServer.id}`, { has_bot: false });
}

console.log("Successfully initizalied the bot and updated all necessary data.");
If there isn't a way around this to changing the owner_id, what should I do? I'd rather a user who once owned a server (which is added to my database) not have access to it if they are no longer the owner.
5 Replies
d.js toolkit
d.js toolkit3w 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! - Marked as resolved by OP
Amgelo
Amgelo3w ago
why do you need to know who's the owner if the bot is no longer in the server? if the bot is no longer there you could just remove its data since there's no use for you to have it
Swyftey
SwyfteyOP3w ago
My project is a server service (website) similar to disboard, or discordfy There will be a page for adding/editing servers and their data The server isn't added to the database when the bot joins, not as simple as that
Amgelo
Amgelo3w ago
why not? :Thonk: I'd recommend you specify your entire setup since this really seems like an xy question and probably in #other-js-ts since I don't think this is djs related
Swyftey
SwyfteyOP3w ago
Thank you
Want results from more Discord servers?
Add your server