Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/commands/banana/banana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { MonkeyTypes } from "../../types/types";
import { createUser, getUser, setUser } from "../../utils/banana";
import { getNextDay, isSameDay } from "../../utils/date";
import { mongoDB } from "../../utils/mongodb";
import { findUserByDiscordId } from "../../dal/user";

export default {
name: "banana",
Expand All @@ -16,9 +17,7 @@ export default {

const discordID = interaction.user.id;

const snapshot = <MonkeyTypes.User | undefined>(
await db.collection("users").findOne({ discordId: discordID })
);
const snapshot = await findUserByDiscordId(discordID);

if (snapshot === undefined) {
interaction.followUp({
Expand Down
8 changes: 2 additions & 6 deletions src/commands/dev/fix-wpm-role.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findUserByDiscordId } from "../../dal/user";
import type { MonkeyTypes } from "../../types/types";
import { mongoDB } from "../../utils/mongodb";

export default {
name: "fix-wpm-role",
Expand Down Expand Up @@ -27,11 +27,7 @@ export default {
return;
}

const db = mongoDB();

const dbUser = <MonkeyTypes.User | undefined>(
await db.collection("users").findOne({ discordId: user.id })
);
const dbUser = await findUserByDiscordId(user.id);

if (dbUser === undefined) {
interaction.reply({
Expand Down
5 changes: 2 additions & 3 deletions src/commands/dev/reset-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DefaultConfig } from "../../constants/default-config";
import { findUserByDiscordId } from "../../dal/user";
import type { MonkeyTypes } from "../../types/types";
import { mongoDB } from "../../utils/mongodb";

Expand All @@ -25,9 +26,7 @@ export default {

const db = mongoDB();

const user = <MonkeyTypes.User | undefined>(
await db.collection("users").findOne({ discordId: discordUser.id })
);
const user = await findUserByDiscordId(discordUser.id);

if (!user) {
interaction.followUp({
Expand Down
5 changes: 2 additions & 3 deletions src/commands/stats/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { findUserByDiscordId } from "../../dal/user";
import type { MonkeyTypes } from "../../types/types";
import { mongoDB } from "../../utils/mongodb";
import { toPascalCase } from "../../utils/strings";
Expand Down Expand Up @@ -60,9 +61,7 @@ export default {
const mode = "time"; // interaction.options.getString("mode", true);
const mode2 = interaction.options.getString("mode2", true);

const user = <MonkeyTypes.User | undefined>(
await db.collection("users").findOne({ discordId: interaction.user.id })
);
const user = await findUserByDiscordId(interaction.user.id);

const leaderboardUser =
user !== undefined
Expand Down
8 changes: 2 additions & 6 deletions src/commands/stats/personal-bests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Collection } from "discord.js";
import { Client } from "../../structures/client";
import type { MonkeyTypes } from "../../types/types";
import { mongoDB } from "../../utils/mongodb";
import { getNameDisplay } from "../../utils/strings";
import { findUserByDiscordId } from "../../dal/user";

const DEFAULT_TIME = [15, 30, 60, 120];
const DEFAULT_WORDS = [10, 25, 50, 100];
Expand All @@ -20,13 +20,9 @@ export default {
}
],
run: async (interaction, client) => {
const db = mongoDB();

const discordUser = interaction.options.getUser("user") ?? interaction.user;

const user = <Partial<MonkeyTypes.User> | undefined>(
await db.collection("users").findOne({ discordId: discordUser.id })
);
const user = await findUserByDiscordId(discordUser.id);

if (user === undefined) {
interaction.reply({
Expand Down
5 changes: 2 additions & 3 deletions src/commands/stats/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Client } from "../../structures/client";
import type { MonkeyTypes } from "../../types/types";
import { mongoDB } from "../../utils/mongodb";
import { toPascalCase } from "../../utils/strings";
import { findUserByDiscordId } from "../../dal/user";

const quoteLengthMap = {
0: "short",
Expand Down Expand Up @@ -30,9 +31,7 @@ export default {

const db = mongoDB();

const user = <MonkeyTypes.User | undefined>(
await db.collection("users").findOne({ discordId: discordUser.id })
);
const user = await findUserByDiscordId(discordUser.id);

if (user === undefined || user.uid === undefined) {
interaction.followUp({
Expand Down
8 changes: 2 additions & 6 deletions src/commands/stats/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import formatDuration from "date-fns/formatDuration";
import intervalToDuration from "date-fns/intervalToDuration";
import { Client } from "../../structures/client";
import type { MonkeyTypes } from "../../types/types";
import { mongoDB } from "../../utils/mongodb";
import { getNameDisplay } from "../../utils/strings";
import { findUserByDiscordId } from "../../dal/user";

export default {
name: "stats",
Expand All @@ -20,11 +20,7 @@ export default {
run: async (interaction, client) => {
const discordUser = interaction.options.getUser("user") ?? interaction.user;

const db = mongoDB();

const user = <MonkeyTypes.User | undefined>(
await db.collection("users").findOne({ discordId: discordUser.id })
);
const user = await findUserByDiscordId(discordUser.id);

if (user === undefined) {
interaction.reply({
Expand Down
31 changes: 31 additions & 0 deletions src/dal/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { MonkeyTypes } from "../types/types";
import { mongoDB } from "../utils/mongodb";

export async function findUserByDiscordId(
discordUserId: string
): Promise<MonkeyTypes.User | undefined> {
const db = mongoDB();

const user = await db
.collection<MonkeyTypes.User>("users")
.findOne({ discordId: discordUserId });

if (!user) {
return undefined;
}

return user;
}

export async function updateChallenge(
discordId: string,
challengeName: string
): Promise<void> {
const db = mongoDB();
await db
.collection("users")
.updateOne(
{ discordId: discordId },
{ $set: { [`challenges.${challengeName}`]: { addedAt: Date.now() } } }
);
}
8 changes: 8 additions & 0 deletions src/events/challenge-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
incrementApproved,
incrementDenied
} from "../dal/challenge-request-stats";
import { updateChallenge } from "../dal/user";

const declineReasonOptions: MessageSelectOptionData[] = [
{
Expand Down Expand Up @@ -152,6 +153,13 @@ export default {
.catch(() => undefined);

if (accepted) {
const challengeName = Object.entries(
client.clientOptions.challenges
).find(([_, id]) => id === challengeRoleID)?.[0];
if (challengeName) {
await updateChallenge(userID, challengeName);
}

const newEmbeds = message.embeds;

newEmbeds[0]?.addFields({
Expand Down
10 changes: 2 additions & 8 deletions src/tasks/link-discord.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findUserByDiscordId } from "../dal/user";
import type { MonkeyTypes } from "../types/types";
import { mongoDB } from "../utils/mongodb";

export default {
name: "linkDiscord",
Expand Down Expand Up @@ -44,13 +44,7 @@ export default {

await member.roles.add(memberRole);

const db = mongoDB();

const dbUser = <MonkeyTypes.User | undefined>(
await db.collection("users").findOne({
discordId: discordUserID
})
);
const dbUser = await findUserByDiscordId(discordUserID);

const botCommandsChannel = await client.getChannel("botCommands");

Expand Down