|
| 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 | +}; |
0 commit comments