feat: finish github command

This commit is contained in:
xHyroM 2022-07-10 22:27:10 +02:00
parent d89b80c60d
commit 031b129654
6 changed files with 54 additions and 14 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -12,6 +12,7 @@
"@discordjs/collection": "^0.7.0", "@discordjs/collection": "^0.7.0",
"discord-api-types": "^0.36.1", "discord-api-types": "^0.36.1",
"hono": "^1.6.4", "hono": "^1.6.4",
"ms": "^2.1.3",
"tweetnacl": "^1.0.3" "tweetnacl": "^1.0.3"
} }
} }

View file

@ -7,6 +7,8 @@ import config from '../../files/config.toml';
import { githubIssuesAndPullRequests } from '../utils/regexes'; import { githubIssuesAndPullRequests } from '../utils/regexes';
import isNumeric from '../utils/isNumeric'; import isNumeric from '../utils/isNumeric';
import Collection from '@discordjs/collection'; import Collection from '@discordjs/collection';
import formatStatus from '../utils/formatStatus';
import ms from 'ms';
const cooldowns: Collection<string, number> = new Collection(); const cooldowns: Collection<string, number> = new Collection();
@ -34,8 +36,14 @@ new Command({
} }
], ],
run: async(ctx) => { run: async(ctx) => {
if (cooldowns.has(ctx.user.id) && cooldowns.get(ctx.user.id) < Date.now()) { if (cooldowns?.has(ctx.user.id) && cooldowns.get(ctx.user.id) > Date.now()) {
return ctx.respond('⚠️ You are in cooldown.'); 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; const query: string = (ctx.options[0] as APIApplicationCommandInteractionDataStringOption).value;
@ -70,11 +78,18 @@ new Command({
}); });
const data: any = await res.json(); const data: any = await res.json();
if (data.message) {
// TODO: finish (pull request, issue) return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `\`\` Invalid issue or pull request \`${query}\``,
flags: MessageFlags.Ephemeral,
}
});
}
return ctx.respond([ return ctx.respond([
`[#${data.number} ${repositoryOwner}/${repositoryName}](${data.html_url}) operation <timestamp>`, `[#${data.number} ${repositoryOwner}/${repositoryName}](<${data.html_url}>) by [${data.user.login}](<${data.user.html_url}>) ${formatStatus(data)}`,
data.title data.title
].join('\n')); ].join('\n'));
} }

View file

@ -61,12 +61,16 @@ app.post('/interaction', bodyParse(), async(c) => {
} }
if (interaction.type === InteractionType.ApplicationCommand && interaction.data.type === ApplicationCommandType.ChatInput) { if (interaction.type === InteractionType.ApplicationCommand && interaction.data.type === ApplicationCommandType.ChatInput) {
return Commands.get(interaction.data.name).run(new CommandContext( try {
return await Commands.get(interaction.data.name).run(new CommandContext(
c, c,
interaction.user, interaction.member,
interaction.data.options, interaction.data.options,
interaction.data.resolved interaction.data.resolved
)); ));
} catch(e) {
console.log(e);
}
} }
return new CommandContext(c).respond({ return new CommandContext(c).respond({

View file

@ -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'; import { Context } from 'hono';
export class CommandContext { export class CommandContext {
public context: Context; public context: Context;
public user?: APIUser; public user?: APIUser;
public member?: APIInteractionGuildMember;
public options?: APIApplicationCommandInteractionDataOption[]; public options?: APIApplicationCommandInteractionDataOption[];
public resolved?: APIChatInputApplicationCommandInteractionDataResolved; 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.context = c;
this.user = user; this.user = member.user;
this.member = member;
this.options = options; this.options = options;
this.resolved = resolved; this.resolved = resolved;
} }

18
src/utils/formatStatus.ts Normal file
View file

@ -0,0 +1,18 @@
export default (data: any) => {
let operation = '';
let timestamp = '';
switch(data.state as 'open' | 'closed' | 'all') {
case 'open':
operation = 'opened';
timestamp = `<t:${Math.floor(new Date(data.created_at).getTime() / 1000)}:R>`;
break;
case 'closed':
operation = data?.pull_request?.merged_at ? 'merged' : 'closed';
timestamp = data?.pull_request?.merged_at
? `<t:${Math.floor(new Date(data.pull_request.merged_at).getTime() / 1000)}:R>`
: `<t:${Math.floor(new Date(data.closed_at).getTime() / 1000)}:R>`;
break;
}
return `${operation} ${timestamp}`;
}