mirror of
https://github.com/xHyroM/bun-discord-bot.git
synced 2024-12-22 20:21:06 +01:00
feat: add state to github cmd
This commit is contained in:
parent
8c32e4c0fd
commit
14a9b97f9b
2 changed files with 52 additions and 12 deletions
|
@ -2,9 +2,8 @@ import { APIApplicationCommandInteractionDataStringOption, ApplicationCommandOpt
|
|||
import { Command } from '../structures/Command';
|
||||
// @ts-expect-error Types :(
|
||||
import utilities from '../../files/utilities.toml';
|
||||
import Collection from '@discordjs/collection';
|
||||
import { CommandContext } from '../structures/contexts/CommandContext';
|
||||
import { getIssueOrPR, search, formatStatus, formatEmojiStatus } from '../utils/githubUtils';
|
||||
import { getIssueOrPR, search, formatStatus, formatEmojiStatus, IssueState } from '../utils/githubUtils';
|
||||
|
||||
const invalidIssue = (ctx: CommandContext, query: string) => {
|
||||
return ctx.editResponse(
|
||||
|
@ -22,9 +21,39 @@ new Command({
|
|||
type: ApplicationCommandOptionType.String,
|
||||
required: true,
|
||||
run: async(ctx) => {
|
||||
return ctx.respond(await search(ctx.value, ctx?.options?.[1]?.value as string || 'oven-sh/bun'));
|
||||
return ctx.respond(
|
||||
await search(
|
||||
ctx.value,
|
||||
(ctx.options.find(o => o.name === 'repository'))?.value as string || 'oven-sh/bun',
|
||||
(ctx.options.find(o => o.name === 'state')?.value as string || 'all') as IssueState,
|
||||
)
|
||||
);
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'state',
|
||||
description: 'Issue or PR state',
|
||||
type: ApplicationCommandOptionType.String,
|
||||
required: false,
|
||||
choices: [
|
||||
{
|
||||
name: 'open',
|
||||
value: 'open',
|
||||
},
|
||||
{
|
||||
name: 'closed',
|
||||
value: 'closed',
|
||||
},
|
||||
{
|
||||
name: 'merged',
|
||||
value: 'merged',
|
||||
},
|
||||
{
|
||||
name: 'all (default)',
|
||||
value: 'all',
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'repository',
|
||||
description: 'Project repository (default oven-sh/bun)',
|
||||
|
@ -36,7 +65,7 @@ new Command({
|
|||
value: repository
|
||||
}))
|
||||
]
|
||||
}
|
||||
},
|
||||
],
|
||||
run: async(ctx) => {
|
||||
ctx.command.runEditResponse(ctx)
|
||||
|
@ -46,13 +75,14 @@ new Command({
|
|||
},
|
||||
runEditResponse: async(ctx) => {
|
||||
let query: string = (ctx.options[0] as APIApplicationCommandInteractionDataStringOption).value;
|
||||
const repository: string = (ctx.options?.[1] as APIApplicationCommandInteractionDataStringOption)?.value || 'oven-sh/bun';
|
||||
const repository: string = (ctx.options.find(o => o.name === 'repository') as APIApplicationCommandInteractionDataStringOption)?.value || 'oven-sh/bun';
|
||||
const state: IssueState = ((ctx.options.find(o => o.name === 'state') as APIApplicationCommandInteractionDataStringOption)?.value || 'all') as IssueState;
|
||||
|
||||
const repositorySplit = repository.split('/');
|
||||
const repositoryOwner = repositorySplit[0];
|
||||
const repositoryName = repositorySplit[1];
|
||||
|
||||
let issueOrPR = await getIssueOrPR(parseInt(query), repository);
|
||||
let issueOrPR = await getIssueOrPR(parseInt(query), repository, state);
|
||||
if (!issueOrPR) {
|
||||
const res = await fetch(`https://api.github.com/search/issues?q=${encodeURIComponent(query)}${encodeURIComponent(' repo:oven-sh/bun')}`);
|
||||
|
||||
|
|
|
@ -8,12 +8,13 @@ import { APIApplicationCommandOptionChoice } from 'discord-api-types/v10';
|
|||
import { Database } from 'bun:sqlite';
|
||||
import { githubTitleClean } from './regexes';
|
||||
|
||||
export type IssueState = 'open' | 'closed' | 'all' | 'merged';
|
||||
interface Issue {
|
||||
id: number;
|
||||
repository: string;
|
||||
title: string;
|
||||
number: number;
|
||||
state: 'open' | 'closed',
|
||||
state: IssueState,
|
||||
created_at: string;
|
||||
closed_at: string | null;
|
||||
html_url: string;
|
||||
|
@ -179,10 +180,14 @@ export const deleteIssueOrPR = (number: number, repository: string) => {
|
|||
db.exec(`DELETE FROM issuesandprs WHERE repository = '${repository}' AND number = ${number}`);
|
||||
}
|
||||
|
||||
export const search = async(query: string, repository: string): Promise<APIApplicationCommandOptionChoice[]> => {
|
||||
export const search = async(query: string, repository: string, state: IssueState): Promise<APIApplicationCommandOptionChoice[]> => {
|
||||
try {
|
||||
const arrayFiltered = await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ?`).all(repository);
|
||||
|
||||
const arrayFiltered = state === 'all'
|
||||
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ?`).all(repository)
|
||||
: state === 'merged'
|
||||
? await db.prepare(`SELECT * FROM issuesandprs WHERE merged_at IS NOT NULL AND repository = ?`).all(repository)
|
||||
: await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND state = ?`).all(repository, state);
|
||||
|
||||
if (!query) {
|
||||
const array = arrayFiltered.slice(0, 25);
|
||||
return array.map((issueOrPr: Issue | PullRequest) => new Object({
|
||||
|
@ -213,8 +218,13 @@ export const search = async(query: string, repository: string): Promise<APIAppli
|
|||
}
|
||||
}
|
||||
|
||||
export const getIssueOrPR = async(number: number, repository: string): Promise<Issue | PullRequest> => {
|
||||
const issueOrPR = await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ?`).get(repository, number);
|
||||
export const getIssueOrPR = async(number: number, repository: string, state: IssueState): Promise<Issue | PullRequest> => {
|
||||
const issueOrPR = state === 'all'
|
||||
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ?`).get(repository, number)
|
||||
: state === 'merged'
|
||||
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ? AND merged_at IS NOT NULL`).get(repository, number)
|
||||
: await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ? AND state = ?`).get(repository, number, state);
|
||||
|
||||
return issueOrPR;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue