diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClient.ts | 2 | ||||
-rw-r--r-- | src/lib/index.ts | 1 | ||||
-rw-r--r-- | src/lib/models/shared/MemberCount.ts | 38 | ||||
-rw-r--r-- | src/tasks/memberCount.ts | 26 |
4 files changed, 67 insertions, 0 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts index 1e6418c..4f0c6b0 100644 --- a/src/lib/extensions/discord-akairo/BushClient.ts +++ b/src/lib/extensions/discord-akairo/BushClient.ts @@ -58,6 +58,7 @@ import { ModLog } from '../../models/instance/ModLog.js'; import { Reminder } from '../../models/instance/Reminder.js'; import { StickyRole } from '../../models/instance/StickyRole.js'; import { Global } from '../../models/shared/Global.js'; +import { MemberCount } from '../../models/shared/MemberCount.js'; import { Shared } from '../../models/shared/Shared.js'; import { Stat } from '../../models/shared/Stat.js'; import { AllowedMentions } from '../../utils/AllowedMentions.js'; @@ -444,6 +445,7 @@ export class BushClient<Ready extends boolean = boolean> extends AkairoClient<Re Stat.initModel(this.sharedDB); Global.initModel(this.sharedDB); Shared.initModel(this.sharedDB); + MemberCount.initModel(this.sharedDB); await this.sharedDB.sync({ // Sync all tables to fix everything if updated // if another instance restarts we don't want to overwrite new changes made in development diff --git a/src/lib/index.ts b/src/lib/index.ts index 59d8b53..8a6b75d 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -76,6 +76,7 @@ export * from './models/instance/ModLog.js'; export * from './models/instance/Reminder.js'; export * from './models/instance/StickyRole.js'; export * from './models/shared/Global.js'; +export * from './models/shared/MemberCount.js'; export * from './models/shared/Shared.js'; export * from './models/shared/Stat.js'; export * from './utils/AllowedMentions.js'; diff --git a/src/lib/models/shared/MemberCount.ts b/src/lib/models/shared/MemberCount.ts new file mode 100644 index 0000000..409d1ac --- /dev/null +++ b/src/lib/models/shared/MemberCount.ts @@ -0,0 +1,38 @@ +import { type Sequelize } from 'sequelize'; +const { DataTypes, Model } = (await import('sequelize')).default; + +export interface MemberCountModel { + timestamp: Date; + guildId: string; + memberCount: number; +} + +export interface MemberCountCreationAttributes { + timestamp?: Date; + guildId: string; + memberCount: number; +} + +/** + * Data specific to a certain instance of the bot. + */ +export class MemberCount extends Model<MemberCountModel, MemberCountCreationAttributes> implements MemberCountModel { + public declare timestamp: Date; + public declare guildId: string; + public declare memberCount: number; + + /** + * Initializes the model. + * @param sequelize The sequelize instance. + */ + public static initModel(sequelize: Sequelize): void { + MemberCount.init( + { + timestamp: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW }, + guildId: { type: DataTypes.STRING, allowNull: false }, + memberCount: { type: DataTypes.BIGINT, allowNull: false } + }, + { sequelize, timestamps: false } + ); + } +} diff --git a/src/tasks/memberCount.ts b/src/tasks/memberCount.ts new file mode 100644 index 0000000..0f1dfc0 --- /dev/null +++ b/src/tasks/memberCount.ts @@ -0,0 +1,26 @@ +import { BushTask, MemberCount, Time } from '#lib'; +import assert from 'assert'; + +export default class MemberCountTask extends BushTask { + public constructor() { + super('memberCount', { + delay: Time.Minute, + runOnStart: true + }); + } + + public override async exec() { + if (!client.config.isProduction) return; + + const res = await Promise.allSettled( + client.guilds.cache.map((g) => MemberCount.create({ guildId: g.id, memberCount: g.memberCount })) + ); + + res + .filter((r) => r.status === 'rejected') + .forEach((r) => { + assert(r.status === 'rejected'); + void client.console.error('memberCount', r.status); + }); + } +} |