diff --git a/src/app.ts b/src/app.ts index d671922..f6a21ae 100644 --- a/src/app.ts +++ b/src/app.ts @@ -39,9 +39,10 @@ client.once("ready", async () => { console.log("------------------"); // Puts all statuses into an array to avoid reading the database on every status change - statusHandler.init(client).catch(console.error); - prefixHandler.init().catch(console.error); - prefixHandler.setLoading(); + await statusHandler.init().catch(console.error); + statusHandler.startStatusLoop(client).catch(console.error); + await prefixHandler.init().catch(console.error); + prefixHandler.loadingDone(); avoidDbSleeping().catch(console.error); const logChannel = client.channels.cache.get(LOG_CHANNEL) as TextChannel; diff --git a/src/handlers/messages.ts b/src/handlers/messages.ts index c502840..4c204f1 100644 --- a/src/handlers/messages.ts +++ b/src/handlers/messages.ts @@ -10,13 +10,13 @@ import { DEFAULT_PREFIX, DEV_PREFIX, RELOAD_PREFIX } from "../config.ts"; import { prefixes } from "../db/schema.ts"; import type { MessageCommandData } from "../helpers/types.ts"; import { clientNoPermissions, errorLog, isDev, isMikuTrigger } from "../helpers/utils.ts"; -import { prefixMap, botIsLoading } from "./prefixes.ts"; +import { prefixMap, isLoading } from "./prefixes.ts"; export default async function handleMessage(message: Message) { const guildClient = await message.guild?.members.fetchMe(); try { // Permission check for the channel which the message was sent in to avoid breaking the bot - if (message.author.bot || clientNoPermissions(message, guildClient) || botIsLoading) return; + if (message.author.bot || clientNoPermissions(message, guildClient) || isLoading()) return; const content = message.content.split(" ").filter(Boolean); const len = content.length; diff --git a/src/handlers/prefixes.ts b/src/handlers/prefixes.ts index 24463e3..645831b 100644 --- a/src/handlers/prefixes.ts +++ b/src/handlers/prefixes.ts @@ -3,10 +3,14 @@ import { db } from "../db/index.ts"; import { prefixes } from "../db/schema.ts"; export const prefixMap = new Map(); -export let botIsLoading = false; +let botIsLoading = true; -export function setLoading() { - botIsLoading = true; +export function isLoading() { + return botIsLoading; +} + +export function loadingDone() { + botIsLoading = false; } export async function init() { diff --git a/src/handlers/statuses.ts b/src/handlers/statuses.ts index 6dbdee1..9b8db60 100644 --- a/src/handlers/statuses.ts +++ b/src/handlers/statuses.ts @@ -7,9 +7,8 @@ import { randomIntFromRange, randomElementFromArray, sleep } from "../helpers/ut export let statusArr: Status[] = []; -export async function init(client: Client) { +export async function init() { statusArr = await db.select().from(statuses); - if (statusArr.length) startStatusLoop(client).catch(console.error); } /** @@ -17,6 +16,7 @@ export async function init(client: Client) { * @param {Client} client Discord client which is used to access the API */ export async function startStatusLoop(client: Client) { + if (statusArr.length === 0) return; while (true) { const status = setRandomStatus(client); if (!status) break;