-
Notifications
You must be signed in to change notification settings - Fork 1
feat(bot): add guild events #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Events } from "discord.js"; | ||
|
|
||
| import client from ".."; | ||
| import { discordAddNewGuild } from "../db/discord"; | ||
|
|
||
| client.on(Events.GuildCreate, async (guild) => { | ||
| console.log(`Joined new guild: ${guild.name} (ID: ${guild.id})`); | ||
|
|
||
| // Add the new guild to tracking | ||
| const result = await discordAddNewGuild(guild.id); | ||
|
|
||
| if (result.success) { | ||
| console.log(`Successfully added guild ${guild.id} to tracking.`); | ||
| } else { | ||
| console.error(`Failed to add guild ${guild.id} to tracking.`); | ||
| } | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Events } from "discord.js"; | ||
|
|
||
| import client from ".."; | ||
| import { discordRemoveGuildFromTracking } from "../db/discord"; | ||
|
|
||
| client.on(Events.GuildDelete, async (guild) => { | ||
| console.log(`Left guild: ${guild.name} (ID: ${guild.id})`); | ||
|
|
||
| // Remove the guild from tracking | ||
| const result = await discordRemoveGuildFromTracking(guild.id); | ||
|
|
||
| if (result.success) { | ||
| console.log(`Successfully removed guild ${guild.id} from tracking.`); | ||
| } else { | ||
| console.error(`Failed to remove guild ${guild.id} from tracking.`); | ||
| } | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Checks for any guilds that may have been added/removed while the bot was offline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { eq } from "drizzle-orm"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { dbDiscordTable } from "../../db/schema"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import client from "../.."; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { db } from "../../db/db"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default async function () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Checking for guilds to update on startup..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let currentGuilds: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Keep checking every 10 seconds until currentGuilds is not empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (currentGuilds.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Waiting for guilds to load..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentGuilds = client.guilds.cache.map((guild) => guild.id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (currentGuilds.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await new Promise((resolve) => setTimeout(resolve, 10000)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await new Promise((resolve) => setTimeout(resolve, 10000)); | |
| await new Promise((resolve) => setTimeout(resolve, GUILD_LOAD_RETRY_INTERVAL)); |
Outdated
Copilot
AI
Jul 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition if (result) is incorrect. Database update operations return an array, which is always truthy even when empty. Check result.length > 0 instead.
| if (result) { | |
| if (result.length > 0) { |
Outdated
Copilot
AI
Jul 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition if (result) is incorrect. Database insert operations return an array, which is always truthy even when empty. Check result.length > 0 instead.
| if (result) { | |
| if (result.length > 0) { |
GalvinPython marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
Copilot
AI
Jul 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using forEach with async callbacks can lead to unhandled promise rejections and doesn't properly handle errors. Use Promise.all with map or a for...of loop instead.
| missingGuilds.forEach(async (guild) => { | |
| console.log(`Removing guild from tracking: ${guild.guildId}`); | |
| const result = await db | |
| .update(dbDiscordTable) | |
| .set({ isInServer: false }) | |
| .where(eq(dbDiscordTable.guildId, guild.guildId)) | |
| .returning(); | |
| if (result) { | |
| console.log( | |
| `Successfully removed guild ${guild.guildId} from tracking.`, | |
| ); | |
| } else { | |
| console.error( | |
| `Failed to remove guild ${guild.guildId} from tracking.`, | |
| ); | |
| } | |
| }); | |
| newGuilds.forEach(async (guildId) => { | |
| console.log(`Adding new guild to tracking: ${guildId}`); | |
| const result = await db | |
| .insert(dbDiscordTable) | |
| .values({ | |
| guildId, | |
| allowedPublicSharing: false, | |
| feedrUpdatesChannelId: null, | |
| isInServer: true, | |
| memberCount: 0, | |
| }) | |
| .returning(); | |
| if (result) { | |
| console.log(`Successfully added guild ${guildId} to tracking.`); | |
| } else { | |
| console.error(`Failed to add guild ${guildId} to tracking.`); | |
| } | |
| }); | |
| for (const guild of missingGuilds) { | |
| try { | |
| console.log(`Removing guild from tracking: ${guild.guildId}`); | |
| const result = await db | |
| .update(dbDiscordTable) | |
| .set({ isInServer: false }) | |
| .where(eq(dbDiscordTable.guildId, guild.guildId)) | |
| .returning(); | |
| if (result) { | |
| console.log( | |
| `Successfully removed guild ${guild.guildId} from tracking.`, | |
| ); | |
| } else { | |
| console.error( | |
| `Failed to remove guild ${guild.guildId} from tracking.`, | |
| ); | |
| } | |
| } catch (error) { | |
| console.error( | |
| `Error while removing guild ${guild.guildId} from tracking:`, | |
| error, | |
| ); | |
| } | |
| } | |
| for (const guildId of newGuilds) { | |
| try { | |
| console.log(`Adding new guild to tracking: ${guildId}`); | |
| const result = await db | |
| .insert(dbDiscordTable) | |
| .values({ | |
| guildId, | |
| allowedPublicSharing: false, | |
| feedrUpdatesChannelId: null, | |
| isInServer: true, | |
| memberCount: 0, | |
| }) | |
| .returning(); | |
| if (result) { | |
| console.log(`Successfully added guild ${guildId} to tracking.`); | |
| } else { | |
| console.error(`Failed to add guild ${guildId} to tracking.`); | |
| } | |
| } catch (error) { | |
| console.error( | |
| `Error while adding guild ${guildId} to tracking:`, | |
| error, | |
| ); | |
| } | |
| } |
Uh oh!
There was an error while loading. Please reload this page.