From 031b12965407a6825e3658488a0b77682807184a Mon Sep 17 00:00:00 2001 From: xHyroM Date: Sun, 10 Jul 2022 22:27:10 +0200 Subject: [PATCH] feat: finish github command --- bun.lockb | Bin 2295 -> 2591 bytes package.json | 1 + src/commands/github.ts | 25 +++++++++++++++++----- src/index.ts | 16 ++++++++------ src/structures/contexts/CommandContext.ts | 8 ++++--- src/utils/formatStatus.ts | 18 ++++++++++++++++ 6 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 src/utils/formatStatus.ts diff --git a/bun.lockb b/bun.lockb index d3b7e524218542c622cdb29dbb97aa12fd0cbb40..447d598de570459bdb7350a3af9fafa1b28a5628 100755 GIT binary patch delta 832 zcmew^IA3Ifp61@=u^|t_T?+i2U;po!W$P&YJ!Hcin@=4qA+Fan-9Cl4b25Md`^0d0 zr8}$;4us@qU}&%dGJz@@a*H8i6DJsOEZK78>elQ19up5J+CbG8FtZ)-U}9i+VCp;V zPd!u+LV?9k7(m1sp;An`K>lP|MiT)L1E>M0mjNWnJUNk3g9R)b%4h;%^fH?8K$U^C zvrk^gq|FL3;V+|kDjQG?gh7B<8m5+5^=y+nna=pUh%DZ@<#bB!$(!rXhA603cqE@& zmvM~oQ--ST$=3G;yY&}_wX3av^IY`A>#x`CR~NsPFJyS_K394F)@s+gDZx{KwlXsO z zQw95EB~~p#P$>O}0&s{;Ze;DBT)-M2$^+!W!VV+@0=$z2*@P!2u(4ROKm_0wu#~L zQWC5X4us@qU}&%dGC6_t#2E$?FDTj=I6OL54;0d6hpYS#1dLE^%bTj1K+F^F6I=fS z)q%u8n8^jmpRCJh!UAT@Wi$~0F+e5(feH{aPA+8BfXMPPneZUVGENR;7oMEJBohj< z5@szS8e}d}>L)*BIy0G-#e4D(X5PuMEZ&SPlUK5YPhP>oJDHc&r5@&Cka<9mz{AkP z-~(}b1A~0eXBGyAevkx}#XO+?V=7^r{EStL7Zk`K0J4g8vLf4dTP_d_#6<$!KsL}p zD1ZmZV@45SVBiG>sT?0r6c%wX1q;|9tSxMlPq8b>f#VJ+0x|$(Fv!9OP$gd=N = new Collection(); @@ -34,8 +36,14 @@ new Command({ } ], run: async(ctx) => { - if (cooldowns.has(ctx.user.id) && cooldowns.get(ctx.user.id) < Date.now()) { - return ctx.respond('⚠️ You are in cooldown.'); + if (cooldowns?.has(ctx.user.id) && cooldowns.get(ctx.user.id) > Date.now()) { + return ctx.respond({ + type: InteractionResponseType.ChannelMessageWithSource, + data: { + content: `⚠️ You are in cooldown, please wait ${ms(cooldowns.get(ctx.user.id) - Date.now())}.`, + flags: MessageFlags.Ephemeral, + } + }); } const query: string = (ctx.options[0] as APIApplicationCommandInteractionDataStringOption).value; @@ -70,11 +78,18 @@ new Command({ }); const data: any = await res.json(); - - // TODO: finish (pull request, issue) + if (data.message) { + return ctx.respond({ + type: InteractionResponseType.ChannelMessageWithSource, + data: { + content: `\`❌\` Invalid issue or pull request \`${query}\``, + flags: MessageFlags.Ephemeral, + } + }); + } return ctx.respond([ - `[#${data.number} ${repositoryOwner}/${repositoryName}](${data.html_url}) operation `, + `[#${data.number} ${repositoryOwner}/${repositoryName}](<${data.html_url}>) by [${data.user.login}](<${data.user.html_url}>) ${formatStatus(data)}`, data.title ].join('\n')); } diff --git a/src/index.ts b/src/index.ts index cdd2ad3..bf9e019 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,12 +61,16 @@ app.post('/interaction', bodyParse(), async(c) => { } if (interaction.type === InteractionType.ApplicationCommand && interaction.data.type === ApplicationCommandType.ChatInput) { - return Commands.get(interaction.data.name).run(new CommandContext( - c, - interaction.user, - interaction.data.options, - interaction.data.resolved - )); + try { + return await Commands.get(interaction.data.name).run(new CommandContext( + c, + interaction.member, + interaction.data.options, + interaction.data.resolved + )); + } catch(e) { + console.log(e); + } } return new CommandContext(c).respond({ diff --git a/src/structures/contexts/CommandContext.ts b/src/structures/contexts/CommandContext.ts index 808da5f..8243a76 100644 --- a/src/structures/contexts/CommandContext.ts +++ b/src/structures/contexts/CommandContext.ts @@ -1,15 +1,17 @@ -import { APIApplicationCommandInteractionDataOption, APIChatInputApplicationCommandInteractionDataResolved, APIInteractionResponse, APIUser, InteractionResponseType } from 'discord-api-types/v10'; +import { APIApplicationCommandInteractionDataOption, APIChatInputApplicationCommandInteractionDataResolved, APIInteractionGuildMember, APIInteractionResponse, APIUser, InteractionResponseType } from 'discord-api-types/v10'; import { Context } from 'hono'; export class CommandContext { public context: Context; public user?: APIUser; + public member?: APIInteractionGuildMember; public options?: APIApplicationCommandInteractionDataOption[]; public resolved?: APIChatInputApplicationCommandInteractionDataResolved; - public constructor(c: Context, user?: APIUser, options?: APIApplicationCommandInteractionDataOption[], resolved?: APIChatInputApplicationCommandInteractionDataResolved) { + public constructor(c: Context, member?: APIInteractionGuildMember, options?: APIApplicationCommandInteractionDataOption[], resolved?: APIChatInputApplicationCommandInteractionDataResolved) { this.context = c; - this.user = user; + this.user = member.user; + this.member = member; this.options = options; this.resolved = resolved; } diff --git a/src/utils/formatStatus.ts b/src/utils/formatStatus.ts new file mode 100644 index 0000000..3378616 --- /dev/null +++ b/src/utils/formatStatus.ts @@ -0,0 +1,18 @@ +export default (data: any) => { + let operation = ''; + let timestamp = ''; + switch(data.state as 'open' | 'closed' | 'all') { + case 'open': + operation = 'opened'; + timestamp = ``; + break; + case 'closed': + operation = data?.pull_request?.merged_at ? 'merged' : 'closed'; + timestamp = data?.pull_request?.merged_at + ? `` + : ``; + break; + } + + return `${operation} ${timestamp}`; +} \ No newline at end of file