diff --git a/backend/api/controllers/result.ts b/backend/api/controllers/result.ts index 7c11a9c1a..79807bd1e 100644 --- a/backend/api/controllers/result.ts +++ b/backend/api/controllers/result.ts @@ -7,7 +7,7 @@ import { updateTypingStats, } from "../../dao/user"; import PublicStatsDAO from "../../dao/public-stats"; -import BotDAO from "../../dao/bot"; +import * as BotDAL from "../../dao/bot"; import { roundTo2, stdDev } from "../../utils/misc"; import objectHash from "object-hash"; import Logger from "../../utils/logger"; @@ -268,7 +268,7 @@ export async function addResult( if (useRedisForBotTasks) { George.updateDiscordRole(user.discordId, result.wpm); } - BotDAO.updateDiscordRole(user.discordId, result.wpm); + BotDAL.updateDiscordRole(user.discordId, result.wpm); } } @@ -276,7 +276,7 @@ export async function addResult( if (useRedisForBotTasks) { George.awardChallenge(user.discordId, result.challenge); } - BotDAO.awardChallenge(user.discordId, result.challenge); + BotDAL.awardChallenge(user.discordId, result.challenge); } else { delete result.challenge; } diff --git a/backend/api/controllers/user.ts b/backend/api/controllers/user.ts index 86472161a..0c2773236 100644 --- a/backend/api/controllers/user.ts +++ b/backend/api/controllers/user.ts @@ -1,5 +1,5 @@ import * as UserDAL from "../../dao/user"; -import BotDAO from "../../dao/bot"; +import * as BotDAL from "../../dao/bot"; import MonkeyError from "../../utils/error"; import Logger from "../../utils/logger"; import { MonkeyResponse } from "../../utils/monkey-response"; @@ -163,7 +163,7 @@ export async function linkDiscord( if (useRedisForBotTasks) { George.linkDiscord(discordId, uid); } - await BotDAO.linkDiscord(uid, discordId); + await BotDAL.linkDiscord(uid, discordId); Logger.logToDb("user_discord_link", `linked to ${discordId}`, uid); return new MonkeyResponse("Discord account linked", discordId); @@ -184,7 +184,7 @@ export async function unlinkDiscord( if (useRedisForBotTasks) { George.unlinkDiscord(userInfo.discordId, uid); } - await BotDAO.unlinkDiscord(uid, userInfo.discordId); + await BotDAL.unlinkDiscord(uid, userInfo.discordId); await UserDAL.unlinkDiscord(uid); Logger.logToDb("user_discord_unlinked", userInfo.discordId, uid); diff --git a/backend/dao/bot.ts b/backend/dao/bot.ts index e057818b0..2b2dd063e 100644 --- a/backend/dao/bot.ts +++ b/backend/dao/bot.ts @@ -33,49 +33,48 @@ async function addCommands( return await db.collection("bot-commands").insertMany(normalizedCommands); } -class BotDAO { - static async updateDiscordRole(discordId, wpm): Promise { - return await addCommand("updateRole", [discordId, wpm]); - } - - static async linkDiscord(uid, discordId): Promise { - return await addCommand("linkDiscord", [discordId, uid]); - } - - static async unlinkDiscord(uid, discordId): Promise { - return await addCommand("unlinkDiscord", [discordId, uid]); - } - - static async awardChallenge( - discordId, - challengeName - ): Promise { - return await addCommand("awardChallenge", [discordId, challengeName]); - } - - static async announceLbUpdate( - newRecords, - leaderboardId - ): Promise { - if (newRecords.length === 0) { - return; - } - - const leaderboardCommands = Array(newRecords.length).fill("sayLbUpdate"); - const leaderboardCommandsArguments = newRecords.map((newRecord) => { - return [ - newRecord.discordId ?? newRecord.name, - newRecord.rank, - leaderboardId, - newRecord.wpm, - newRecord.raw, - newRecord.acc, - newRecord.consistency, - ]; - }); - - return await addCommands(leaderboardCommands, leaderboardCommandsArguments); - } +export async function updateDiscordRole( + discordId, + wpm +): Promise { + return await addCommand("updateRole", [discordId, wpm]); } -export default BotDAO; +export async function linkDiscord(uid, discordId): Promise { + return await addCommand("linkDiscord", [discordId, uid]); +} + +export async function unlinkDiscord(uid, discordId): Promise { + return await addCommand("unlinkDiscord", [discordId, uid]); +} + +export async function awardChallenge( + discordId, + challengeName +): Promise { + return await addCommand("awardChallenge", [discordId, challengeName]); +} + +export async function announceLbUpdate( + newRecords, + leaderboardId +): Promise { + if (newRecords.length === 0) { + return; + } + + const leaderboardCommands = Array(newRecords.length).fill("sayLbUpdate"); + const leaderboardCommandsArguments = newRecords.map((newRecord) => { + return [ + newRecord.discordId ?? newRecord.name, + newRecord.rank, + leaderboardId, + newRecord.wpm, + newRecord.raw, + newRecord.acc, + newRecord.consistency, + ]; + }); + + return await addCommands(leaderboardCommands, leaderboardCommandsArguments); +} diff --git a/backend/jobs/update-leaderboards.ts b/backend/jobs/update-leaderboards.ts index 746218433..b51c8d5e0 100644 --- a/backend/jobs/update-leaderboards.ts +++ b/backend/jobs/update-leaderboards.ts @@ -1,5 +1,5 @@ import { CronJob } from "cron"; -import BotDAO from "../dao/bot"; +import { announceLbUpdate } from "../dao/bot"; import George from "../tasks/george"; import * as LeaderboardsDAL from "../dao/leaderboards"; import { getCachedConfiguration } from "../init/configuration"; @@ -59,7 +59,7 @@ async function updateLeaderboardAndNotifyChanges( await George.announceLbUpdate(newRecords, leaderboardId); } - await BotDAO.announceLbUpdate(newRecords, leaderboardId); + await announceLbUpdate(newRecords, leaderboardId); } }