Skip to content

Commit 742ba4d

Browse files
committed
Adds checks to register command and some cleanup
Some general cleanup and fixes of todo's. Also implemented the required checks and used data from the environment variables. This command implements what showcased in issue message: TFNS#301 (comment)
1 parent 59f64ff commit 742ba4d

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

api/src/discord/commands/register.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
1-
import { ApplicationCommandType, Client, CommandInteraction } from "discord.js";
1+
import {
2+
ApplicationCommandType,
3+
Client,
4+
CommandInteraction,
5+
GuildMemberRoleManager,
6+
} from "discord.js";
27
import { Command } from "../command";
38
import {
4-
createInvitationURLForDiscordId,
9+
createInvitationTokenForDiscordId,
10+
getInvitationTokenForDiscordId,
511
getUserByDiscordId,
612
} from "../database/users";
713
import config from "../../config";
814

915
async function getInvitationUrl(invitation_code: string | null = null) {
1016
if (config.pad.domain == "") return "";
11-
1217
if (invitation_code == null) return "";
1318

1419
const ssl = config.pad.useSSL == "false" ? "" : "s";
1520

1621
return `http${ssl}://${config.pad.domain}/#/auth/register/${invitation_code}`;
1722
}
1823

24+
//? Refactor this to not have this type in two places.
25+
type AllowedRoles =
26+
| "user_guest"
27+
| "user_friend"
28+
| "user_member"
29+
| "user_manager"
30+
| "user_admin";
31+
1932
async function createAccountLogic(
2033
client: Client,
2134
interaction: CommandInteraction
2235
) {
23-
// TODO: check if user has role
24-
// TODO: check if feature is enabled in environment variables
36+
// if (config.discord.registrationEnabled.toLowerCase() === "false") {
37+
await interaction.editReply({
38+
content:
39+
"The functionality to create your own account this way has been disabled by an administrator",
40+
});
41+
return;
42+
// }
43+
44+
if (config.discord.registrationRoleId !== "") {
45+
if (
46+
!(interaction.member?.roles as GuildMemberRoleManager).cache.has(
47+
config.discord.registrationRoleId
48+
)
49+
) {
50+
await interaction.editReply({
51+
content:
52+
"You do not have the role required to create an account yourself.",
53+
});
54+
return;
55+
}
56+
}
2557

2658
const userId = await getUserByDiscordId(interaction.user.id);
2759
if (userId != null) {
@@ -55,8 +87,8 @@ async function createAccountLogic(
5587
"Generating private invitation url... If you already have a CTFNote account you should link it using the /link command instead.",
5688
});
5789

58-
const invitation_code = await createInvitationURLForDiscordId(
59-
"user_friend", // TODO: use environment variable
90+
const invitation_code = await createInvitationTokenForDiscordId(
91+
(config.discord.registrationAccountRole as AllowedRoles) ?? "user_guest",
6092
interaction.user.id
6193
);
6294

@@ -68,7 +100,7 @@ async function createAccountLogic(
68100
}
69101

70102
const invitation_url = await getInvitationUrl(invitation_code);
71-
if (invitation_url == null) {
103+
if (invitation_url == "") {
72104
await interaction.editReply({
73105
content: "Something went wrong.", // TODO: Meaningful error messages?
74106
});

api/src/discord/database/users.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ export async function getInvitationTokenForDiscordId(
7373
}
7474
}
7575

76+
export async function createInvitationTokenForDiscordId(
77+
role: AllowedRoles = "user_guest",
7678
discordId: string,
7779
pgClient: PoolClient | null = null
7880
): Promise<string | null> {
79-
// TODO: Verify if valid role is passed.
81+
role = (role as AllowedRoles) ?? "user_guest";
8082

8183
const useRequestClient = pgClient != null;
8284
if (pgClient == null) pgClient = await connectToDatabase();

0 commit comments

Comments
 (0)