-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Switch to ES Modules * Swap out fuzzysort for js-trgm * Use node-fetch v3 * Github commands * Remove dev * Remove empty default export * Extensions Co-authored-by: Nayan Gautam <[email protected]>
- Loading branch information
Showing
6 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
DISCORD_TOKEN= | ||
NODE_ENV= | ||
|
||
GITHUB_TOKEN | ||
|
||
SUPABASE_URL= | ||
SUPABASE_KEY= | ||
SUPABASE_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { CommandInteraction } from 'discord.js'; | ||
import fetch, { Headers } from 'node-fetch'; | ||
import { GITHUB_TOKEN } from '../../config.js'; | ||
import { listOfLinks } from '../../utils/embedBuilder.js'; | ||
import { REPOS, REPO_DETAILS } from '../../utils/repositories.js'; | ||
|
||
async function githubSearch(body: { | ||
query: string; | ||
variables: Record<string, string>; | ||
}) { | ||
const res = await fetch('https://api.github.com/graphql', { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
headers: new Headers({ | ||
Authorization: `Bearer ${GITHUB_TOKEN}`, | ||
'Content-Type': 'application/json', | ||
}), | ||
}); | ||
if (!res.ok) { | ||
return null; | ||
} | ||
const results: Array<Record<string, any>> = ((await res.json()) as any).data | ||
.search.nodes; | ||
if (!results.length) { | ||
return null; | ||
} | ||
return listOfLinks( | ||
results.map( | ||
(result) => `#[${result.number}](${result.url}): ${result.title}`, | ||
), | ||
); | ||
} | ||
|
||
export async function githubCommandHandler( | ||
interaction: CommandInteraction, | ||
query: string, | ||
is?: 'issue' | 'pr', | ||
) { | ||
const thisRepoDetails = | ||
REPO_DETAILS[ | ||
interaction.options.getInteger('repository', true) as REPOS | ||
]; | ||
const topic = interaction.options.getString('topic'); | ||
|
||
const searchQuery = `${is ? `is:${is}` : ''} repo:${ | ||
thisRepoDetails.REPOSITORY_NAME | ||
} ${topic || ''}`; | ||
|
||
try { | ||
let results = await githubSearch({ | ||
query, | ||
variables: { | ||
searchQuery, | ||
}, | ||
}); | ||
|
||
if (results) { | ||
await interaction.reply({ | ||
embeds: [results], | ||
}); | ||
} else { | ||
await interaction.reply({ | ||
content: 'No results found.', | ||
ephemeral: true, | ||
}); | ||
} | ||
} catch { | ||
// TODO: Do nothing or log the error or something | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ApplicationCommandOptionTypes } from 'discord.js/typings/enums'; | ||
import { command } from 'jellycommands'; | ||
import { REPOS } from '../../utils/repositories.js'; | ||
import { githubCommandHandler } from './_common.js'; | ||
|
||
const query = `query searchResults($searchQuery: String!) { | ||
search(type: DISCUSSION, query: $searchQuery, first: 5) { | ||
nodes { | ||
... on Discussion { | ||
title | ||
number | ||
url | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export default command({ | ||
name: 'discussion', | ||
description: 'Search for a discussion on github.', | ||
global: true, | ||
|
||
options: [ | ||
{ | ||
name: 'repository', | ||
description: 'The repository to search within', | ||
type: ApplicationCommandOptionTypes.INTEGER, | ||
choices: [ | ||
{ | ||
name: 'SvelteKit', | ||
value: REPOS.SVELTEKIT, | ||
}, | ||
], | ||
required: true, | ||
}, | ||
{ | ||
name: 'topic', | ||
description: 'What to search for', | ||
type: ApplicationCommandOptionTypes.STRING, | ||
}, | ||
], | ||
|
||
run: async ({ interaction }) => { | ||
await githubCommandHandler(interaction, query); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ApplicationCommandOptionTypes } from 'discord.js/typings/enums'; | ||
import { command } from 'jellycommands'; | ||
import { REPOS } from '../../utils/repositories.js'; | ||
import { githubCommandHandler } from './_common.js'; | ||
|
||
const query = `query searchResults($searchQuery: String!) { | ||
search(type: ISSUE, query: $searchQuery, first: 5) { | ||
nodes { | ||
... on Issue { | ||
title | ||
number | ||
url | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export default command({ | ||
name: 'issue', | ||
description: 'Search for an issue on github', | ||
global: true, | ||
|
||
options: [ | ||
{ | ||
name: 'repository', | ||
description: 'The repository to search within', | ||
type: ApplicationCommandOptionTypes.INTEGER, | ||
choices: [ | ||
{ | ||
name: 'Svelte', | ||
value: REPOS.SVELTE, | ||
}, | ||
{ | ||
name: 'SvelteKit', | ||
value: REPOS.SVELTEKIT, | ||
}, | ||
{ | ||
name: 'Language Tools', | ||
value: REPOS.LANGUAGETOOLS, | ||
}, | ||
], | ||
required: true, | ||
}, | ||
{ | ||
name: 'topic', | ||
description: 'What to search for', | ||
type: ApplicationCommandOptionTypes.STRING, | ||
}, | ||
], | ||
|
||
run: async ({ interaction }) => { | ||
await githubCommandHandler(interaction, query, 'issue'); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ApplicationCommandOptionTypes } from 'discord.js/typings/enums'; | ||
import { command } from 'jellycommands'; | ||
import { REPOS } from '../../utils/repositories.js'; | ||
import { githubCommandHandler } from './_common.js'; | ||
|
||
const query = `query searchResults($searchQuery: String!) { | ||
search(type: ISSUE, query: $searchQuery, first: 5) { | ||
nodes { | ||
... on PullRequest { | ||
title | ||
number | ||
url | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export default command({ | ||
name: 'pr', | ||
description: 'Search for a pull request on github', | ||
global: true, | ||
|
||
options: [ | ||
{ | ||
name: 'repository', | ||
description: 'The repository to search within', | ||
type: ApplicationCommandOptionTypes.INTEGER, | ||
choices: [ | ||
{ | ||
name: 'Svelte', | ||
value: REPOS.SVELTE, | ||
}, | ||
{ | ||
name: 'SvelteKit', | ||
value: REPOS.SVELTEKIT, | ||
}, | ||
{ | ||
name: 'RFCs', | ||
value: REPOS.RFCS, | ||
}, | ||
{ | ||
name: 'Language Tools', | ||
value: REPOS.LANGUAGETOOLS, | ||
}, | ||
], | ||
required: true, | ||
}, | ||
{ | ||
name: 'topic', | ||
description: 'What to search for', | ||
type: ApplicationCommandOptionTypes.STRING, | ||
}, | ||
], | ||
|
||
run: async ({ interaction }) => { | ||
await githubCommandHandler(interaction, query, 'pr'); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters