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",
"discord-api-types": "^0.36.1",
"hono": "^1.6.4",
"ms": "^2.1.3",
"tweetnacl": "^1.0.3"
}
}

View file

@ -7,6 +7,8 @@ import config from '../../files/config.toml';
import { githubIssuesAndPullRequests } from '../utils/regexes';
import isNumeric from '../utils/isNumeric';
import Collection from '@discordjs/collection';
import formatStatus from '../utils/formatStatus';
import ms from 'ms';
const cooldowns: Collection<string, number> = 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 <timestamp>`,
`[#${data.number} ${repositoryOwner}/${repositoryName}](<${data.html_url}>) by [${data.user.login}](<${data.user.html_url}>) ${formatStatus(data)}`,
data.title
].join('\n'));
}

View file

@ -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({

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';
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;
}

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}`;
}