2022-02-27 05:54:16 +08:00
|
|
|
import { InsertManyResult, InsertOneResult } from "mongodb";
|
2022-02-23 03:55:48 +08:00
|
|
|
import db from "../init/db";
|
2021-06-09 06:54:30 +08:00
|
|
|
|
2022-03-02 00:11:01 +08:00
|
|
|
async function addCommand(
|
|
|
|
command,
|
|
|
|
commandArguments
|
|
|
|
): Promise<InsertOneResult<any>> {
|
|
|
|
return await db.collection<any>("bot-commands").insertOne({
|
2021-08-03 23:03:31 +08:00
|
|
|
command,
|
2022-02-09 04:27:14 +08:00
|
|
|
arguments: commandArguments,
|
2021-08-03 23:03:31 +08:00
|
|
|
executed: false,
|
|
|
|
requestTimestamp: Date.now(),
|
|
|
|
});
|
2021-06-09 06:54:30 +08:00
|
|
|
}
|
|
|
|
|
2022-02-27 05:54:16 +08:00
|
|
|
async function addCommands(
|
|
|
|
commands,
|
|
|
|
commandArguments
|
|
|
|
): Promise<void | InsertManyResult> {
|
2022-02-09 04:27:14 +08:00
|
|
|
if (commands.length === 0 || commands.length !== commandArguments.length) {
|
2022-02-27 05:54:16 +08:00
|
|
|
return;
|
2022-01-29 02:48:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const normalizedCommands = commands.map((command, index) => {
|
|
|
|
return {
|
|
|
|
command,
|
2022-02-09 04:27:14 +08:00
|
|
|
arguments: commandArguments[index],
|
2022-01-29 02:48:08 +08:00
|
|
|
executed: false,
|
|
|
|
requestTimestamp: Date.now(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-02-23 03:55:48 +08:00
|
|
|
return await db.collection("bot-commands").insertMany(normalizedCommands);
|
2022-01-29 02:48:08 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 21:15:09 +08:00
|
|
|
class BotDAO {
|
2022-02-27 05:54:16 +08:00
|
|
|
static async updateDiscordRole(discordId, wpm): Promise<InsertOneResult> {
|
2021-06-09 06:54:30 +08:00
|
|
|
return await addCommand("updateRole", [discordId, wpm]);
|
2021-06-07 21:15:09 +08:00
|
|
|
}
|
2021-06-09 06:54:30 +08:00
|
|
|
|
2022-02-27 05:54:16 +08:00
|
|
|
static async linkDiscord(uid, discordId): Promise<InsertOneResult> {
|
2021-06-12 21:00:21 +08:00
|
|
|
return await addCommand("linkDiscord", [discordId, uid]);
|
|
|
|
}
|
|
|
|
|
2022-02-27 05:54:16 +08:00
|
|
|
static async unlinkDiscord(uid, discordId): Promise<InsertOneResult> {
|
2022-01-08 22:40:46 +08:00
|
|
|
return await addCommand("unlinkDiscord", [discordId, uid]);
|
|
|
|
}
|
|
|
|
|
2022-02-27 05:54:16 +08:00
|
|
|
static async awardChallenge(
|
|
|
|
discordId,
|
|
|
|
challengeName
|
|
|
|
): Promise<InsertOneResult> {
|
2021-08-23 22:07:13 +08:00
|
|
|
return await addCommand("awardChallenge", [discordId, challengeName]);
|
|
|
|
}
|
|
|
|
|
2022-02-27 05:54:16 +08:00
|
|
|
static async announceLbUpdate(
|
|
|
|
newRecords,
|
|
|
|
leaderboardId
|
|
|
|
): Promise<InsertManyResult | void> {
|
2022-01-29 02:48:08 +08:00
|
|
|
if (newRecords.length === 0) {
|
2022-02-27 05:54:16 +08:00
|
|
|
return;
|
2022-01-29 02:48:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-09-14 21:54:03 +08:00
|
|
|
}
|
2021-06-07 21:15:09 +08:00
|
|
|
}
|
|
|
|
|
2022-02-23 03:55:48 +08:00
|
|
|
export default BotDAO;
|