Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TiltedToast committed Feb 8, 2024
1 parent 9cfefe8 commit 414e6c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions src/handlers/prefixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { db } from "../db/index.ts";
import { prefixes } from "../db/schema.ts";

export const prefixMap = new Map<Snowflake, string>();
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() {
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/statuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ 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);
}

/**
* Starts a loop which periodically changes the status to a random entry in the database
* @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;
Expand Down

0 comments on commit 414e6c9

Please sign in to comment.