Skip to content

Commit 1ffafba

Browse files
authored
Create member.ts (#306)
* Create member.ts * Move member logic to commandDetails * Create member.ts commandDetails * Implement suggested changes * Move remaining logic to commandDetails * Update member API endpoint
1 parent 6cec78c commit 1ffafba

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { container } from '@sapphire/framework';
2+
import { Message, MessageEmbed } from 'discord.js';
3+
import fetch from 'node-fetch';
4+
import {
5+
CodeyCommandDetails,
6+
CodeyCommandOptionType,
7+
CodeyCommandResponseType,
8+
SapphireMessageExecuteType,
9+
SapphireMessageResponse
10+
} from '../../codeyCommand';
11+
import { getEmojiByName } from '../../components/emojis';
12+
13+
const MEMBER_API = 'https://csclub.uwaterloo.ca/api/members.json';
14+
15+
interface memberStatus {
16+
name: string;
17+
id: string;
18+
program: string;
19+
}
20+
21+
type UwIdType = string | undefined;
22+
23+
/*
24+
* Get member embed
25+
*/
26+
const getMemberEmbed = async (uwid: UwIdType): Promise<MessageEmbed> => {
27+
const title = 'CSC Membership Information';
28+
if (!uwid) {
29+
return new MessageEmbed().setColor('RED').setTitle(title).setDescription('Please provide a UW ID!');
30+
}
31+
32+
const members = (await (await fetch(MEMBER_API)).json()).members as memberStatus[];
33+
const foundMember = members.filter((m) => m.id == uwid).length > 0;
34+
35+
if (foundMember) {
36+
return new MessageEmbed()
37+
.setColor('GREEN')
38+
.setTitle(title)
39+
.setDescription(`You're a CSC member! Hooray! ${getEmojiByName('codeyLove')}`);
40+
}
41+
42+
const NOT_MEMBER_DESCRIPTION = `You're not a CSC member! ${getEmojiByName('codeySad')}
43+
44+
Being a CSC member comes with gaining access to CSC machines, cloud, email, web hosting, and more! Additional details can be found here! https://csclub.uwaterloo.ca/resources/services/
45+
46+
To sign up, you can follow the instructions here! https://csclub.uwaterloo.ca/get-involved/`;
47+
return new MessageEmbed().setColor('RED').setTitle(title).setDescription(NOT_MEMBER_DESCRIPTION);
48+
};
49+
50+
const executeCommand: SapphireMessageExecuteType = async (
51+
_client,
52+
messageFromUser,
53+
args,
54+
_initialMessageFromBot
55+
): Promise<SapphireMessageResponse> => {
56+
let uwId: UwIdType;
57+
if (messageFromUser instanceof Message) {
58+
const { content } = messageFromUser;
59+
const messageArgs = content.split(' ').filter((m) => m != '.member');
60+
if (messageArgs.length == 1) uwId = messageArgs[0];
61+
} else if ('uwid' in args) {
62+
uwId = args['uwid'] as string;
63+
}
64+
const memberEmbed = await getMemberEmbed(uwId);
65+
return { embeds: [memberEmbed] };
66+
};
67+
68+
export const memberCommandDetails: CodeyCommandDetails = {
69+
name: 'member',
70+
aliases: [],
71+
description: 'Gets CSC membership information',
72+
detailedDescription: `**Examples:**
73+
\`${container.botPrefix}member [id]\``,
74+
75+
isCommandResponseEphemeral: true,
76+
messageWhenExecutingCommand: 'Getting CSC membership information...',
77+
executeCommand: executeCommand,
78+
messageIfFailure: 'Could not retrieve CSC membership information.',
79+
codeyCommandResponseType: CodeyCommandResponseType.EMBED,
80+
81+
options: [
82+
{
83+
name: 'uwid',
84+
description: 'Quest ID',
85+
type: CodeyCommandOptionType.STRING,
86+
required: true
87+
}
88+
],
89+
subcommandDetails: {}
90+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Command } from '@sapphire/framework';
2+
import { CodeyCommand } from '../../codeyCommand';
3+
import { memberCommandDetails } from '../../commandDetails/miscellaneous/member';
4+
5+
export class MiscellaneousMemberCommand extends CodeyCommand {
6+
details = memberCommandDetails;
7+
8+
public constructor(context: Command.Context, options: Command.Options) {
9+
super(context, {
10+
...options,
11+
aliases: memberCommandDetails.aliases,
12+
description: memberCommandDetails.description,
13+
detailedDescription: memberCommandDetails.detailedDescription
14+
});
15+
}
16+
}

0 commit comments

Comments
 (0)