Skip to content

Commit

Permalink
add search command (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietzler authored Nov 2, 2023
1 parent 24c2f4d commit df40394
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 20 deletions.
171 changes: 169 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@discordx/importer": "^1.2.3",
"@octokit/rest": "^20.0.2",
"@types/lodash": "^4.14.200",
"@types/luxon": "^3.3.3",
"cron": "^3.0.0",
Expand Down
67 changes: 57 additions & 10 deletions src/commands/slashes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ApplicationCommandOptionType, MessageFlags, type CommandInteraction } from 'discord.js';
import {
ApplicationCommandOptionType,
MessageFlags,
type CommandInteraction,
AutocompleteInteraction,
} from 'discord.js';
import { Discord, Slash, SlashChoice, SlashOption } from 'discordx';
import { DOCS_DOMAIN, IMMICH_REPOSITORY, GITHUB_API_DOMAIN } from '../constants.js';
import { DOCS_DOMAIN, IMMICH_REPOSITORY, IMMICH_REPOSITORY_BASE_OPTIONS } from '../constants.js';
import { DateTime } from 'luxon';
import { Octokit } from '@octokit/rest';

const linkCommands: Record<string, string> = {
'reverse proxy': `${DOCS_DOMAIN}/administration/reverse-proxy`,
Expand All @@ -26,6 +32,8 @@ const helpTexts: Record<string, string> = {
const _star_history: Record<string, number | undefined> = {};
const _fork_history: Record<string, number | undefined> = {};

const octokit = new Octokit();

@Discord()
export class Commands {
@Slash({ description: 'Links to Immich pages' })
Expand Down Expand Up @@ -76,13 +84,14 @@ export class Commands {
const lastStarsCount = _star_history[interaction.channelId];

try {
const response = await (await fetch(GITHUB_API_DOMAIN)).json();
const starsCount = response['stargazers_count'] as number;
const starsCount = await octokit.rest.repos
.get(IMMICH_REPOSITORY_BASE_OPTIONS)
.then((repo) => repo.data.stargazers_count);
const delta = lastStarsCount && starsCount - lastStarsCount;
const formattedDelta = delta && Intl.NumberFormat(undefined, { signDisplay: 'always' }).format(delta);

interaction.reply(
`Stars ⭐: ${response['stargazers_count']}${
`Stars ⭐: ${starsCount}${
formattedDelta ? ` (${formattedDelta} stars since the last call in this channel)` : ''
}`,
);
Expand All @@ -98,15 +107,14 @@ export class Commands {
const lastForksCount = _fork_history[interaction.channelId];

try {
const response = await (await fetch(GITHUB_API_DOMAIN)).json();
const forksCount = response['forks_count'] as number;
const forksCount = await octokit.rest.repos
.get(IMMICH_REPOSITORY_BASE_OPTIONS)
.then((repo) => repo.data.forks_count);
const delta = lastForksCount && forksCount - lastForksCount;
const formattedDelta = delta && Intl.NumberFormat(undefined, { signDisplay: 'always' }).format(delta);

interaction.reply(
`Forks: ${response['forks_count']}${
formattedDelta ? ` (${formattedDelta} forks since the last call in this channel)` : ''
}`,
`Forks: ${forksCount}${formattedDelta ? ` (${formattedDelta} forks since the last call in this channel)` : ''}`,
);

_fork_history[interaction.channelId] = forksCount;
Expand Down Expand Up @@ -134,4 +142,43 @@ export class Commands {
);
}
}

@Slash({ description: 'Search for PRs and Issues by title' })
search(
@SlashOption({
description: 'Query that applies to title',
name: 'query',
required: true,
type: ApplicationCommandOptionType.String,
autocomplete: async (interaction: AutocompleteInteraction) => {
const value = interaction.options.getFocused(true).value;
if (!value) {
return interaction.respond([]);
}

const result = await octokit.rest.search
.issuesAndPullRequests({
q: `repo:immich-app/immich in:title ${value}`,
per_page: 5,
page: 1,
sort: 'updated',
order: 'desc',
})
.then((response) => response.data);
return interaction.respond(
result.items.map((item) => {
const name = `${item.pull_request ? '[PR]' : '[Issue]'} (${item.number}) ${item.title}`;
return {
name: name.length > 100 ? name.substring(0, 97) + '...' : name,
value: `${item.pull_request ? '[PR]' : '[Issue]'} ([#${item.number}](${item.url}))`,
};
}),
);
},
})
content: string,
interaction: CommandInteraction,
) {
return interaction.reply({ content, flags: [MessageFlags.SuppressEmbeds] });
}
}
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const DOCS_DOMAIN = `${IMMICH_DOMAIN}/docs`;
export const GITHUB_DOMAIN = 'https://github.com';
export const IMMICH_REPOSITORY = `${GITHUB_DOMAIN}/immich-app/immich`;
export const GITHUB_API_DOMAIN = 'https://api.github.com/repos/immich-app/immich';

export const IMMICH_REPOSITORY_BASE_OPTIONS = { owner: 'immich-app', repo: 'immich' };
11 changes: 6 additions & 5 deletions src/events/messageEvents.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Message, MessageFlags, PartialMessage } from 'discord.js';
import { ArgsOf, Discord, On } from 'discordx';
import { GITHUB_API_DOMAIN, GITHUB_DOMAIN, IMMICH_DOMAIN, IMMICH_REPOSITORY } from '../constants.js';
import { GITHUB_DOMAIN, IMMICH_DOMAIN, IMMICH_REPOSITORY, IMMICH_REPOSITORY_BASE_OPTIONS } from '../constants.js';
import _ from 'lodash';
import { Octokit } from '@octokit/rest';

const PREVIEW_BLACKLIST = [GITHUB_DOMAIN, IMMICH_DOMAIN];
const octokit = new Octokit();

@Discord()
export class MessageEvents {
Expand Down Expand Up @@ -47,12 +49,11 @@ export class MessageEvents {
for (const match of matches) {
if (match?.groups) {
const id = match.groups.id;
const response = await fetch(`${GITHUB_API_DOMAIN}/issues/${id}`);
const response = await octokit.rest.issues.get({ ...IMMICH_REPOSITORY_BASE_OPTIONS, issue_number: Number(id) });

if (response.status === 200) {
const json = await response.json();
const type = json.pull_request ? 'PR' : 'ISSUE';
links.add(`[${type}] ${json.title} ([#${id}](${json.html_url}))`);
const type = response.data.pull_request ? 'PR' : 'ISSUE';
links.add(`[${type}] ${response.data.title} ([#${id}](${response.data.html_url}))`);
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ const birthdayJob = new CronJob('36 4 3 2 *', async () => {

bot.once('ready', async () => {
const sha = process.env.COMMIT_SHA;
const commit = sha ? `[${sha.substring(0, 8)}](https://github.com/immich-app/discord-bot/commit/${sha})` : 'dev';
const fullVersion = `${process.env.npm_package_version}@${commit}`;
const commit = sha && `[${sha.substring(0, 8)}](https://github.com/immich-app/discord-bot/commit/${sha})`;
const fullVersion = commit && `${process.env.npm_package_version}@${commit}`;
// Synchronize applications commands with Discord
await bot.initApplicationCommands();

console.log(`Bot ${fullVersion} started`);

const channel = (await bot.channels.fetch('1159083520027787307')) as TextChannel;

if (channel) {
if (channel && fullVersion) {
channel.send(`I'm alive, running ${fullVersion}!`);
}

Expand Down

0 comments on commit df40394

Please sign in to comment.