1- import { ApplicationCommandType , Client , CommandInteraction } from "discord.js" ;
1+ import {
2+ ApplicationCommandType ,
3+ Client ,
4+ CommandInteraction ,
5+ GuildMemberRoleManager ,
6+ } from "discord.js" ;
27import { Command } from "../command" ;
38import {
4- createInvitationURLForDiscordId ,
9+ createInvitationTokenForDiscordId ,
10+ getInvitationTokenForDiscordId ,
511 getUserByDiscordId ,
612} from "../database/users" ;
713import config from "../../config" ;
814
915async 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+
1932async 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 } ) ;
0 commit comments