Skip to content

Commit

Permalink
cleanup /learn link commands
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaUrsa committed Dec 13, 2023
1 parent 6b43ba1 commit 9834a42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 49 deletions.
24 changes: 15 additions & 9 deletions src/discord/commands/global/d.learn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ async function moodleHelp():Promise<EmbedBuilder> {
async function moodleLink(
interaction:ChatInputCommandInteraction,
):Promise<EmbedBuilder> {
// Check if the admin_only option was used
if (interaction.options.getString('admin_only')) {
// Check if the discordId option was used
if (interaction.options.getString('discordId')) {
if (interaction.user.id !== env.DISCORD_OWNER_ID) {
return embedTemplate()
.setColor(Colors.Red)
.setDescription('You are not allowed to use this option!');
}

// Check if the email given is valid
if (!interaction.options.getString('email', true).includes('@')) {
return embedTemplate()
.setColor(Colors.Red)
.setDescription('That doesn\'t look like a valid email address!');
}

return embedTemplate()
.setDescription(await link(
undefined,
interaction.options.getString('email', true),
interaction.options.getString('admin_only', true),
interaction.options.getString('discordId', true),
));
}

Expand All @@ -57,23 +64,22 @@ async function moodleLink(
return embedTemplate()
.setDescription(await link(
interaction.options.getString('email', true),
undefined,
interaction.user.id,
));
}

async function moodleUnlink(
interaction:ChatInputCommandInteraction,
):Promise<EmbedBuilder> {
if (interaction.options.getString('admin_only')) {
if (interaction.options.getString('discordId')) {
if (interaction.user.id !== env.DISCORD_OWNER_ID) {
return embedTemplate()
.setColor(Colors.Red)
.setDescription('You are not allowed to use this option!');
}
return embedTemplate()
.setDescription(await unlink(
interaction.options.getString('admin_only', true),
interaction.options.getString('discordId', true),
));
}

Expand Down Expand Up @@ -192,12 +198,12 @@ export const dLearn: SlashCommand = {
.addStringOption(option => option.setName('email')
.setDescription('What email did you use to register on moodle?')
.setRequired(true))
.addStringOption(option => option.setName('admin_only')
.addStringOption(option => option.setName('discordId')
.setDescription('Ignore this, admin use only!')))
.addSubcommand(subcommand => subcommand
.setName('unlink')
.setDescription('Unlink your discord with your TripSitLearn account')
.addStringOption(option => option.setName('admin_only')
.addStringOption(option => option.setName('discordId')
.setDescription('Ignore this, admin use only!')))
.addSubcommand(subcommand => subcommand
.setName('profile')
Expand Down
53 changes: 13 additions & 40 deletions src/global/commands/g.learn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,74 +59,47 @@ export async function help():Promise<MoodleInfo> {
}

export async function link(
email?:string,
moodleUsername?:string,
discordId?:string,
matrixId?:string,
email:string,
discordId:string,
):Promise<string> {
// log.debug(F, `Link started with moodleUsername: ${moodleUsername}, \
// discordId: ${discordId}, matrixId: ${matrixId}`);

const userData = discordId
? await tripbotDb.users.findUnique({
where: {
discord_id: discordId,
},
})
: await tripbotDb.users.findFirst({
where: {
matrix_id: matrixId as string,
},
});
const userData = await tripbotDb.users.findUnique({
where: {
discord_id: discordId,
},
});

// log.debug(F, `userData: ${JSON.stringify(userData)}`);

if (!userData) {
return 'No user found with that Discord or Matrix ID.';
}

const moodleUserData = moodleUsername
? await moodleDb.mdl_user.findUnique({
where: {
mnethostid_username: {
username: moodleUsername,
mnethostid: 1,
},
},
})
: await moodleDb.mdl_user.findFirst({
where: {
email,
},
});
const moodleUserData = await moodleDb.mdl_user.findFirst({
where: {
email,
},
});

// const [moodleUserData] = await moodleDb.mdl_user.findMany();

if (!moodleUserData) {
if (moodleUsername) {
return 'No user found with that username.';
}
return 'No user found with that email address.';
}

// log.debug(F, `moodleUserData: ${JSON.stringify(moodleUserData.username)}`);

userData.moodle_id = moodleUserData.username;

tripbotDb.users.update({
where: {
id: userData.id,
},
data: {
moodle_id: userData.moodle_id,
moodle_id: moodleUserData.username,
},
});

if (moodleUsername) {
return stripIndents`You have linked this Discord account with TripSitLearn!
Use the /learn profile command to see their profile!`;
}

return stripIndents`You have linked your Discord account with TripSitLearn!
Use the /learn profile command to see your profile!`;
}
Expand Down

0 comments on commit 9834a42

Please sign in to comment.