Skip to content

Commit e9a4901

Browse files
committed
chore: reorganize modules
1 parent f5a8157 commit e9a4901

22 files changed

+55
-53
lines changed

.env.placeholder

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
GOOGLE_CLIENT_ID=
2-
GOOGLE_CLIENT_SECRET=
3-
DISCORD_TOKEN=
4-
BOT_PREFIX=
51
JWT_SECRET=
2+
3+
DISCORD_CLIENT_ID=
4+
DISCORD_CLIENT_SECRET=
5+
6+
DISCORD_TOKEN=
7+
DISCORD_GUILD_ID=
8+
69
HB_MONGO_URI=
710
HB_TEST_CHANNEL_ID=
811
HB_TEST_ORIGIN_CHANNEL_ID=
9-
HB_TEST_ORIGIN_CHANNEL_ID_NE=
12+
HB_TEST_ORIGIN_CHANNEL_ID_NE=

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ RUN yarn install --frozen-lockfile
77
COPY . .
88
RUN yarn build
99

10-
CMD ["node", "./lib/server.js"]
10+
CMD ["node", "./lib/index.js"]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"start": "node .",
1717
"test": "jest"
1818
},
19-
"main": "./lib/server.js",
19+
"main": "./lib/index.js",
2020
"files": [
2121
"lib"
2222
],

src/modules/discord/auth.ts src/discord/auth.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { APIConnection } from "discord-api-types";
22
import { Request, Response, Router } from "express";
33
import jwt from "jsonwebtoken";
44
import fetch from "node-fetch";
5-
import { JWT_SECRET } from "../../constants";
6-
import { createOrUpdateUser } from "../../db";
7-
import { log } from "../../util";
8-
import { JwtToken } from "../auth";
5+
import { JWT_SECRET } from "../constants";
6+
import { createOrUpdateUser } from "../db";
7+
import { log } from "../util";
8+
import { JwtToken } from "./interfaces";
99

1010
const DISCORD_AUTHORIZE_URL = "https://discord.com/api/oauth2/authorize";
1111
const DISCORD_TOKEN_URL = "https://discord.com/api/oauth2/token";
@@ -51,7 +51,7 @@ async function getConnections(token: string): Promise<APIConnection[]> {
5151
return await res.json();
5252
}
5353

54-
export function createDiscordOAuthHandler({
54+
export function createAuthHandler({
5555
clientId,
5656
clientSecret,
5757
redirectUri,

src/modules/discord/index.ts src/discord/bot.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Client, Intents } from "discord.js";
2-
import { HB_MONGO_URI } from "../../constants";
3-
import { Honeybee } from "../../modules/honeybee";
4-
import { log } from "../../util";
2+
import { HB_MONGO_URI } from "../constants";
3+
import { Honeybee } from "../honeybee";
4+
import { log } from "../util";
55
import { commands } from "./commands";
66
import { CommandContext } from "./interfaces";
77

File renamed without changes.

src/modules/discord/commands/pair.ts src/discord/commands/pair.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { SlashCommandBuilder } from "@discordjs/builders";
22
import { CommandInteraction } from "discord.js";
3-
import { findOrCreateGuild } from "../../../db";
4-
import { log } from "../../../util";
5-
import { assertValidModerator } from "../util";
3+
import { findOrCreateGuild } from "../../db";
4+
import { log } from "../../util";
65

76
export default {
87
data: new SlashCommandBuilder()
@@ -54,3 +53,16 @@ Channel: https://www.youtube.com/channel/${channel}`,
5453
});
5554
},
5655
};
56+
57+
async function assertValidModerator(intr: CommandInteraction) {
58+
if (!(intr.channel && intr.channel.type === "GUILD_TEXT")) {
59+
throw new Error("Invalid channel");
60+
}
61+
62+
const hasPermission =
63+
intr.channel.permissionsFor(intr.user)?.has("MANAGE_CHANNELS") ?? false;
64+
65+
if (!hasPermission) {
66+
throw new Error("You are not allowed to run this command");
67+
}
68+
}

src/modules/discord/commands/verify.ts src/discord/commands/verify.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SlashCommandBuilder } from "@discordjs/builders";
22
import { CommandInteraction, MessageEmbed, User } from "discord.js";
3-
import { getUserByDiscordId } from "../../../db";
4-
import { log } from "../../../util";
3+
import { getUserByDiscordId } from "../../db";
4+
import { log } from "../../util";
55
import { Command, RoleChangeset } from "../interfaces";
66
import { applyRoles } from "../phases/applyRoles";
77
import { onboardingPhase } from "../phases/onboarding";

src/modules/discord/deploy-commands.ts src/discord/deploy-commands.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { REST } from "@discordjs/rest";
22
import { Routes } from "discord-api-types/v9";
3-
import { DISCORD_TOKEN } from "../../constants";
3+
import { DISCORD_TOKEN } from "../constants";
44
import { commands } from "./commands";
55

66
const clientId = process.env.DISCORD_CLIENT_ID!;

src/modules/discord/interfaces.ts src/discord/interfaces.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SlashCommandBuilder } from "@discordjs/builders";
22
import { CommandInteraction } from "discord.js";
3-
import { Status } from "../../models/verification";
4-
import { Honeybee } from "../../modules/honeybee";
3+
import { Status } from "../models/verification";
4+
import { Honeybee } from "../honeybee";
55

66
export interface CommandContext {
77
hb: Honeybee;
@@ -18,3 +18,8 @@ export interface RoleChangeset {
1818
roleId: string;
1919
status: Status;
2020
}
21+
22+
export interface JwtToken {
23+
discordId: string;
24+
iat: number;
25+
}

src/modules/discord/phases/applyRoles.ts src/discord/phases/applyRoles.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommandInteraction, Message } from "discord.js";
1+
import { CommandInteraction } from "discord.js";
22
import { RoleChangeset } from "../interfaces";
33

44
export async function applyRoles(

src/modules/discord/phases/onboarding.ts src/discord/phases/onboarding.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Message, User } from "discord.js";
1+
import { User } from "discord.js";
22
import jwt from "jsonwebtoken";
3-
import { JwtToken } from "../../auth";
4-
import { HOST, JWT_SECRET } from "../../../constants";
5-
import { log } from "../../../util";
3+
import { HOST, JWT_SECRET } from "../../constants";
4+
import { log } from "../../util";
5+
import { JwtToken } from "../interfaces";
66

77
export function onboardingPhase(user: User) {
88
log("onboardingPhase");

src/modules/discord/phases/verification.ts src/discord/phases/verification.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
getRoleMapsForGuild,
55
saveVerification,
66
updateVerification,
7-
} from "../../../db";
8-
import { User } from "../../../models/user";
7+
} from "../../db";
8+
import { User } from "../../models/user";
99
import { Honeybee } from "../../honeybee";
10-
import { log } from "../../../util";
10+
import { log } from "../../util";
1111
import { RoleChangeset } from "../interfaces";
1212

1313
import { DateTime } from "luxon";
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/server.ts src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
MONGODB_URL,
1111
PORT,
1212
} from "./constants";
13-
import { createBot } from "./modules/discord";
14-
import { createDiscordOAuthHandler } from "./modules/discord/auth";
13+
import { createBot } from "./discord/bot";
14+
import { createAuthHandler } from "./discord/auth";
1515

1616
// setup db
1717
mongoose.connect(MONGODB_URL, {
@@ -23,7 +23,7 @@ mongoose.connect(MONGODB_URL, {
2323
// setup discord bot
2424
const bot = createBot();
2525

26-
const discordOAuthHandler = createDiscordOAuthHandler({
26+
const discordOAuthHandler = createAuthHandler({
2727
clientId: DISCORD_CLIENT_ID,
2828
clientSecret: DISCORD_CLIENT_SECRET,
2929
redirectUri: `${HOST}/discord/callback`,

src/modules/auth.ts

-4
This file was deleted.

src/modules/discord/util.ts

-14
This file was deleted.
File renamed without changes.

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"compilerOptions": {
44
/* Visit https://aka.ms/tsconfig.json to read more about this file */
55
"incremental": true,
6-
"target": "es2019",
6+
"target": "es2020",
77
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
88
"sourceMap": false /* Generates corresponding '.map' file. */,
99
"declaration": false,

0 commit comments

Comments
 (0)