diff options
24 files changed, 69 insertions, 45 deletions
diff --git a/package.json b/package.json index fc23824..b39aa04 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,9 @@ "#lib": { "default": "./src/lib/index.js" }, + "#constants": { + "default": "./src/lib/utils/BushConstants.js" + }, "#args": { "default": "./src/arguments/index.js" }, diff --git a/src/arguments/abbreviatedNumber.ts b/src/arguments/abbreviatedNumber.ts index a98699c..43d2938 100644 --- a/src/arguments/abbreviatedNumber.ts +++ b/src/arguments/abbreviatedNumber.ts @@ -7,7 +7,7 @@ export const abbreviatedNumber: BushArgumentTypeCaster<number | null> = (_, phra if (!phrase) return null; const num = numeral(phrase?.toLowerCase()).value(); - if (num === undefined || num === null || isNaN(num)) return null; + if (typeof num !== 'number' || isNaN(num)) return null; return num; }; diff --git a/src/commands/info/userInfo.ts b/src/commands/info/userInfo.ts index 4e3675b..0ff8611 100644 --- a/src/commands/info/userInfo.ts +++ b/src/commands/info/userInfo.ts @@ -1,5 +1,6 @@ import { BushCommand, + Time, type ArgType, type BushGuild, type BushGuildMember, @@ -127,7 +128,7 @@ export default class UserInfoCommand extends BushCommand { if (user.accentColor !== null) generalInfo.push(`**Accent Color:** ${user.hexAccentColor}`); if (user.banner) generalInfo.push(`**Banner:** [link](${user.bannerURL({ extension: 'png', size: 4096 })})`); - const pronouns = await Promise.race([util.getPronounsOf(user), util.sleep(2)]); // cut off request after 2 seconds + const pronouns = await Promise.race([util.getPronounsOf(user), util.sleep(2 * Time.Second)]); // cut off request after 2 seconds if (pronouns && typeof pronouns === 'string' && pronouns !== 'Unspecified') generalInfo.push(`**Pronouns:** ${pronouns}`); diff --git a/src/inhibitors/blacklist/channelGlobalBlacklist.ts b/src/inhibitors/blacklist/channelGlobalBlacklist.ts index b9d7240..9023dc2 100644 --- a/src/inhibitors/blacklist/channelGlobalBlacklist.ts +++ b/src/inhibitors/blacklist/channelGlobalBlacklist.ts @@ -11,9 +11,8 @@ export default class UserGlobalBlacklistInhibitor extends BushInhibitor { } public override exec(message: BushMessage | BushSlashMessage, command: BushCommand): boolean { - if (!message.author || !message.guild) return false; - if (client.isOwner(message.author) || /* client.isSuperUser(message.author) ||*/ client.user!.id === message.author.id) - return false; + if (!message.inGuild()) return false; + if (message.author.isOwner() || client.user!.id === message.author.id) return false; if (client.cache.global.blacklistedChannels.includes(message.channel!.id) && !command.bypassChannelBlacklist) { void client.console.verbose( 'channelGlobalBlacklist', diff --git a/src/inhibitors/blacklist/channelGuildBlacklist.ts b/src/inhibitors/blacklist/channelGuildBlacklist.ts index e881ebb..aa5b2d9 100644 --- a/src/inhibitors/blacklist/channelGuildBlacklist.ts +++ b/src/inhibitors/blacklist/channelGuildBlacklist.ts @@ -11,9 +11,8 @@ export default class ChannelGuildBlacklistInhibitor extends BushInhibitor { } public override async exec(message: BushMessage | BushSlashMessage, command: BushCommand): Promise<boolean> { - if (!message.author || !message.guild) return false; - if (client.isOwner(message.author) || /* client.isSuperUser(message.author) || */ client.user!.id === message.author.id) - return false; + if (!message.inGuild()) return false; + if (message.author.isOwner() || client.user!.id === message.author.id) return false; if ( (await message.guild.getSetting('bypassChannelBlacklist'))?.includes(message.author.id) && !command.bypassChannelBlacklist diff --git a/src/inhibitors/blacklist/guildBlacklist.ts b/src/inhibitors/blacklist/guildBlacklist.ts index b319475..9949c22 100644 --- a/src/inhibitors/blacklist/guildBlacklist.ts +++ b/src/inhibitors/blacklist/guildBlacklist.ts @@ -11,12 +11,8 @@ export default class GuildBlacklistInhibitor extends BushInhibitor { } public override exec(message: BushMessage | BushSlashMessage): boolean { - if (!message.guild) return false; - if ( - message.author && - (client.isOwner(message.author) || client.isSuperUser(message.author) || client.user!.id === message.author.id) - ) - return false; + if (!message.inGuild()) return false; + if (message.author.isOwner() || message.author.isSuperUser() || client.user!.id === message.author.id) return false; if (client.cache.global.blacklistedGuilds.includes(message.guild.id)) { void client.console.verbose( 'guildBlacklist', diff --git a/src/inhibitors/blacklist/userGlobalBlacklist.ts b/src/inhibitors/blacklist/userGlobalBlacklist.ts index ad906f8..d0f9a9f 100644 --- a/src/inhibitors/blacklist/userGlobalBlacklist.ts +++ b/src/inhibitors/blacklist/userGlobalBlacklist.ts @@ -12,7 +12,7 @@ export default class UserGlobalBlacklistInhibitor extends BushInhibitor { public override exec(message: BushMessage | BushSlashMessage): boolean { if (!message.author) return false; - if (client.isOwner(message.author) || client.user!.id === message.author.id) return false; + if (message.author.isOwner() || client.user!.id === message.author.id) return false; if (client.cache.global.blacklistedUsers.includes(message.author.id)) { void client.console.verbose( 'userGlobalBlacklist', diff --git a/src/inhibitors/blacklist/userGuildBlacklist.ts b/src/inhibitors/blacklist/userGuildBlacklist.ts index a87e47c..61f670e 100644 --- a/src/inhibitors/blacklist/userGuildBlacklist.ts +++ b/src/inhibitors/blacklist/userGuildBlacklist.ts @@ -11,9 +11,8 @@ export default class UserGuildBlacklistInhibitor extends BushInhibitor { } public override async exec(message: BushMessage | BushSlashMessage): Promise<boolean> { - if (!message.author || !message.guild) return false; - if (client.isOwner(message.author) || client.isSuperUser(message.author) || client.user!.id === message.author.id) - return false; + if (!message.inGuild()) return false; + if (message.author.isOwner() || message.author.isSuperUser() || client.user!.id === message.author.id) return false; if ((await message.guild.getSetting('blacklistedUsers'))?.includes(message.author.id)) { void client.console.verbose( 'userGuildBlacklist', diff --git a/src/inhibitors/checks/fatal.ts b/src/inhibitors/checks/fatal.ts index 2521b2f..411357c 100644 --- a/src/inhibitors/checks/fatal.ts +++ b/src/inhibitors/checks/fatal.ts @@ -11,7 +11,7 @@ export default class FatalInhibitor extends BushInhibitor { } public override async exec(message: BushMessage | BushSlashMessage): Promise<boolean> { - if (client.isOwner(message.author)) return false; + if (message.author.isOwner()) return false; for (const property in client.cache.global) { if (!client.cache.global[property as keyof typeof client.cache.global]) { void client.console.verbose( diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index 36dc396..6b5cb39 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -402,7 +402,7 @@ export class BushClientUtil extends ClientUtil { * @returns The combined elements or `ifEmpty`. * * @example - * const permissions = oxford([PermissionFlagsBits.Administrator, PermissionFlagsBits.SendMessages, PermissionFlagsBits.ManageMessages], 'and', 'none'); + * const permissions = oxford(['Administrator', 'SendMessages', 'ManageMessages'], 'and', 'none'); * console.log(permissions); // Administrator, SendMessages and ManageMessages */ public oxford(array: string[], conjunction: string, ifEmpty?: string): string | undefined { @@ -415,7 +415,14 @@ export class BushClientUtil extends ClientUtil { return array.join(', '); } + /** + * Get the global cache. + */ public getGlobal(): GlobalCache; + /** + * Get a key from the global cache. + * @param key The key to get in the global cache. + */ public getGlobal<K extends keyof GlobalCache>(key: K): GlobalCache[K]; public getGlobal(key?: keyof GlobalCache) { return key ? client.cache.global[key] : client.cache.global; @@ -651,12 +658,11 @@ export class BushClientUtil extends ClientUtil { } /** - * Wait an amount in seconds. - * @param seconds The number of seconds to wait - * @returns A promise that resolves after the specified amount of seconds + * Wait an amount in milliseconds. + * @returns A promise that resolves after the specified amount of milliseconds */ - public async sleep(seconds: number) { - return new Promise((resolve) => setTimeout(resolve, seconds * 1000)); + public get sleep() { + return promisify(setTimeout); } /** diff --git a/src/lib/utils/BushConstants.ts b/src/lib/utils/BushConstants.ts index 93de100..8d35522 100644 --- a/src/lib/utils/BushConstants.ts +++ b/src/lib/utils/BushConstants.ts @@ -3,6 +3,9 @@ import { BushClientUtil } from '../extensions/discord-akairo/BushClientUtil.js'; const rawCapeUrl = 'https://raw.githubusercontent.com/NotEnoughUpdates/capes/master/'; +/** + * Time units in milliseconds + */ export const enum Time { /** * One millisecond (1 ms). diff --git a/src/listeners/guild/guildMemberRemove.ts b/src/listeners/guild/guildMemberRemove.ts index ac162f6..d71f3fd 100644 --- a/src/listeners/guild/guildMemberRemove.ts +++ b/src/listeners/guild/guildMemberRemove.ts @@ -1,6 +1,7 @@ import { BushListener, StickyRole, + Time, type BushClientEvents, type BushGuildMember, type BushTextChannel, @@ -25,7 +26,7 @@ export default class GuildMemberRemoveListener extends BushListener { private async sendWelcomeMessage(member: BushGuildMember | PartialBushGuildMember) { if (client.config.isDevelopment) return; const user = member.partial ? await client.users.fetch(member.id) : member.user; - await util.sleep(0.05); // ban usually triggers after member leave + await util.sleep(50 * Time.Millisecond); // ban usually triggers after member leave const isBan = member.guild.bans.cache.has(member.id); const welcomeChannel = await member.guild.getSetting('welcomeChannel'); if (!welcomeChannel) return; diff --git a/src/listeners/track-manual-punishments/modlogSyncBan.ts b/src/listeners/track-manual-punishments/modlogSyncBan.ts index 480daab..16fa75d 100644 --- a/src/listeners/track-manual-punishments/modlogSyncBan.ts +++ b/src/listeners/track-manual-punishments/modlogSyncBan.ts @@ -22,7 +22,7 @@ export default class ModlogSyncBanListener extends BushListener { } const now = new Date(); - await util.sleep(0.5); // wait for audit log entry + await util.sleep(500 * Time.Millisecond); // wait for audit log entry const logs = (await ban.guild.fetchAuditLogs({ type: AuditLogEvent.MemberBanAdd })).entries.filter( (entry) => entry.target?.id === ban.user.id diff --git a/src/listeners/track-manual-punishments/modlogSyncKick.ts b/src/listeners/track-manual-punishments/modlogSyncKick.ts index d766aa3..fa7a6dd 100644 --- a/src/listeners/track-manual-punishments/modlogSyncKick.ts +++ b/src/listeners/track-manual-punishments/modlogSyncKick.ts @@ -22,7 +22,7 @@ export default class ModlogSyncKickListener extends BushListener { } const now = new Date(); - await util.sleep(0.5); // wait for audit log entry + await util.sleep(500 * Time.Millisecond); // wait for audit log entry const logs = (await member.guild.fetchAuditLogs({ type: AuditLogEvent.MemberKick })).entries.filter( (entry) => entry.target?.id === member.user.id diff --git a/src/listeners/track-manual-punishments/modlogSyncTimeout.ts b/src/listeners/track-manual-punishments/modlogSyncTimeout.ts index 4796f84..4d7f901 100644 --- a/src/listeners/track-manual-punishments/modlogSyncTimeout.ts +++ b/src/listeners/track-manual-punishments/modlogSyncTimeout.ts @@ -21,7 +21,7 @@ export default class ModlogSyncTimeoutListener extends BushListener { } const now = new Date(); - await util.sleep(0.5); // wait for audit log entry + await util.sleep(500 * Time.Millisecond); // wait for audit log entry const logs = (await newMember.guild.fetchAuditLogs({ type: AuditLogEvent.MemberUpdate })).entries.filter( (entry) => entry.target?.id === newMember.user.id diff --git a/src/listeners/track-manual-punishments/modlogSyncUnban.ts b/src/listeners/track-manual-punishments/modlogSyncUnban.ts index c1ef561..680b965 100644 --- a/src/listeners/track-manual-punishments/modlogSyncUnban.ts +++ b/src/listeners/track-manual-punishments/modlogSyncUnban.ts @@ -21,7 +21,7 @@ export default class ModlogSyncUnbanListener extends BushListener { } const now = new Date(); - await util.sleep(0.5); // wait for audit log entry + await util.sleep(500 * Time.Millisecond); // wait for audit log entry const logs = (await ban.guild.fetchAuditLogs({ type: AuditLogEvent.MemberBanRemove })).entries.filter( (entry) => entry.target?.id === ban.user.id diff --git a/src/tasks/cpuUsage.ts b/src/tasks/cpuUsage.ts index 9985e48..e5cfc00 100644 --- a/src/tasks/cpuUsage.ts +++ b/src/tasks/cpuUsage.ts @@ -1,16 +1,16 @@ -import { BushTask } from '#lib'; +import { BushTask, Time } from '#lib'; import osu from 'node-os-utils'; export default class CpuUsageTask extends BushTask { public constructor() { super('cpuUsage', { - delay: 60_000, // 1 minute + delay: Time.Minute, runOnStart: true }); } public override async exec() { - const cpu = await osu.cpu.usage(client.stats.cpu === undefined ? 100 : 60_000); + const cpu = await osu.cpu.usage(client.stats.cpu === undefined ? 100 * Time.Millisecond : Time.Minute); client.stats.cpu = cpu; } } diff --git a/src/tasks/handleReminders.ts b/src/tasks/handleReminders.ts index d1af480..e281903 100644 --- a/src/tasks/handleReminders.ts +++ b/src/tasks/handleReminders.ts @@ -1,10 +1,10 @@ -import { BushTask, Reminder } from '#lib'; +import { BushTask, Reminder, Time } from '#lib'; const { Op } = (await import('sequelize')).default; export default class HandlerRemindersTask extends BushTask { public constructor() { super('handlerReminders', { - delay: 30_000, // 30 seconds + delay: 30 * Time.Second, runOnStart: true }); } @@ -13,7 +13,7 @@ export default class HandlerRemindersTask extends BushTask { const expiredEntries = await Reminder.findAll({ where: { expires: { - [Op.lt]: new Date(Date.now() + 30_000) // Find all rows with an expiry date before 30 seconds from now + [Op.lt]: new Date(Date.now() + 30 * Time.Second) // Find all rows with an expiry date before 30 seconds from now }, notified: false } diff --git a/src/tasks/removeExpiredPunishements.ts b/src/tasks/removeExpiredPunishements.ts index 904e614..b1ca042 100644 --- a/src/tasks/removeExpiredPunishements.ts +++ b/src/tasks/removeExpiredPunishements.ts @@ -1,11 +1,11 @@ -import { ActivePunishment, ActivePunishmentType, BushTask, type BushGuild, type BushUser } from '#lib'; +import { ActivePunishment, ActivePunishmentType, BushTask, Time, type BushGuild, type BushUser } from '#lib'; import assert from 'assert'; const { Op } = (await import('sequelize')).default; export default class RemoveExpiredPunishmentsTask extends BushTask { public constructor() { super('removeExpiredPunishments', { - delay: 15_000, // 15 seconds + delay: 15 * Time.Second, runOnStart: true }); } @@ -14,7 +14,7 @@ export default class RemoveExpiredPunishmentsTask extends BushTask { const expiredEntries = await ActivePunishment.findAll({ where: { expires: { - [Op.lt]: new Date(Date.now() + 15_000) // Find all rows with an expiry date before 15 seconds from now + [Op.lt]: new Date(Date.now() + 15 * Time.Second) // Find all rows with an expiry date before 15 seconds from now } } }); diff --git a/src/tasks/updateCache.ts b/src/tasks/updateCache.ts index 9084c1c..3794f76 100644 --- a/src/tasks/updateCache.ts +++ b/src/tasks/updateCache.ts @@ -1,3 +1,4 @@ +import { Time } from '#constants'; import { Global, Guild, Shared, type BushClient } from '#lib'; import { BushTask } from '../lib/extensions/discord-akairo/BushTask.js'; import config from './../config/options.js'; @@ -5,7 +6,7 @@ import config from './../config/options.js'; export default class UpdateCacheTask extends BushTask { public constructor() { super('updateCache', { - delay: 300_000, // 5 minutes + delay: 5 * Time.Minute, runOnStart: false // done in preinit task }); } diff --git a/src/tasks/updateHighlightCache.ts b/src/tasks/updateHighlightCache.ts new file mode 100644 index 0000000..22289ce --- /dev/null +++ b/src/tasks/updateHighlightCache.ts @@ -0,0 +1,15 @@ +import { Time } from '#constants'; +import { BushTask } from '../lib/extensions/discord-akairo/BushTask.js'; + +export default class UpdateHighlightCacheTask extends BushTask { + public constructor() { + super('updateHighlightCache', { + delay: 5 * Time.Minute, + runOnStart: false + }); + } + + public override async exec() { + return client.highlightManager.syncCache(); + } +} diff --git a/src/tasks/updatePriceItemCache.ts b/src/tasks/updatePriceItemCache.ts index 096354b..6414eb0 100644 --- a/src/tasks/updatePriceItemCache.ts +++ b/src/tasks/updatePriceItemCache.ts @@ -1,11 +1,11 @@ -import { BushTask } from '#lib'; +import { BushTask, Time } from '#lib'; import got from 'got'; import PriceCommand, { AuctionAverages, Bazaar, LowestBIN } from '../commands/utilities/price.js'; export default class UpdatePriceItemCache extends BushTask { public constructor() { super('updatePriceItemCache', { - delay: 600_000, // 10 minutes + delay: 10 * Time.Minute, runOnStart: true }); } diff --git a/src/tasks/updateStats.ts b/src/tasks/updateStats.ts index bf8d2ed..15ebe96 100644 --- a/src/tasks/updateStats.ts +++ b/src/tasks/updateStats.ts @@ -1,9 +1,9 @@ -import { BushTask, Stat } from '#lib'; +import { BushTask, Stat, Time } from '#lib'; export default class UpdateStatsTask extends BushTask { public constructor() { super('updateStats', { - delay: 600_000, // 10 minutes + delay: 10 * Time.Minute, runOnStart: true }); } diff --git a/tsconfig.json b/tsconfig.json index 2bb70c9..32b9b83 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,8 @@ "preserveValueImports": true, "removeComments": true, "paths": { - "#lib": ["./src/lib/index.js"], + "#lib": ["./src/lib/index.ts"], + "#constants": ["./src/lib/utils/BushConstants.ts"], "#args": ["./src/arguments/index.ts"], "#commands": ["./src/commands/index.ts"] }, |