From 8852026e2c730b6602d89fba22260dff0654de64 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Mon, 29 Aug 2022 10:44:40 +0800 Subject: [PATCH] modrinth command --- src/_reupload.ts | 6 +++ src/commands/modrinth.ts | 99 ++++++++++++++++++++++++++++++++++++++++ src/constants.ts | 2 +- src/index.ts | 3 ++ src/tagsTags.ts | 1 + 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/commands/modrinth.ts diff --git a/src/_reupload.ts b/src/_reupload.ts index f5556e2..f91627f 100644 --- a/src/_reupload.ts +++ b/src/_reupload.ts @@ -30,6 +30,12 @@ import 'dotenv/config'; .setRequired(true) .addChoices(...tags.map((b) => ({ name: b.name, value: b.name }))) ), + new SlashCommandBuilder() + .setName('modrinth') + .setDescription('Get info on a Modrinth project') + .addStringOption((option) => + option.setName('id').setDescription('The ID or slug').setRequired(true) + ), ].map((command) => command.toJSON()); const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN!); diff --git a/src/commands/modrinth.ts b/src/commands/modrinth.ts new file mode 100644 index 0000000..d240487 --- /dev/null +++ b/src/commands/modrinth.ts @@ -0,0 +1,99 @@ +type Side = 'required' | 'optional' | 'unsupported'; + +export interface ModrinthProject { + slug: string; + title: string; + description: string; + categories: string[]; + client_side: Side; + server_side: Side; + project_type: 'mod' | 'modpack'; + downloads: number; + icon_url: string | null; + id: string; + team: string; +} + +import { + type CacheType, + type CommandInteraction, + EmbedBuilder, +} from 'discord.js'; + +import { COLORS } from '../constants'; + +export const modrinthCommand = async (i: CommandInteraction) => { + await i.deferReply(); + + const { value: id } = i.options.get('id') ?? { value: null }; + + if (!id || typeof id !== 'string') { + await i.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle('Error!') + .setDescription('You need to provide a valid mod ID!') + .setColor(COLORS.red), + ], + }); + + return; + } + + const data = (await fetch('https://api.modrinth.com/v2/project/' + id).then( + (a) => a.json() + )) as ModrinthProject | { error: string; description: string }; + + if ('error' in data) { + console.error(data); + + await i.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle('Error!') + .setDescription(`\`${data.error}\` ${data.description}`) + .setColor(COLORS.red), + ], + }); + + return; + } + + await i.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle(data.title) + .setDescription(data.description) + .setThumbnail(data.icon_url ?? '') + .setURL(`https://modrinth.com/project/${data.slug}`) + .setFields([ + { + name: 'Categories', + value: data.categories.join(', '), + inline: true, + }, + { + name: 'Project type', + value: data.project_type, + inline: true, + }, + { + name: 'Downloads', + value: data.downloads.toString(), + inline: true, + }, + { + name: 'Client', + value: data.client_side, + inline: true, + }, + { + name: 'Server', + value: data.server_side, + inline: true, + }, + ]) + .setColor(COLORS.green), + ], + }); +}; diff --git a/src/constants.ts b/src/constants.ts index 31de9aa..86e49e9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -23,7 +23,7 @@ export const ETA_MESSAGES = [ 'In PolyMC 2.0.0', ]; -export const COLORS: { [color: string]: number } = { +export const COLORS = { red: 0xef4444, green: 0x22c55e, blue: 0x60a5fa, diff --git a/src/index.ts b/src/index.ts index d4d941c..f11f761 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,7 @@ import random from 'just-random'; import { green, bold, yellow } from 'kleur/colors'; import 'dotenv/config'; import { getTags } from './tagsTags'; +import { modrinthCommand } from './commands/modrinth'; const client = new Client({ intents: [ @@ -137,6 +138,8 @@ client.on('interactionCreate', async (interaction) => { await membersCommand(interaction); } else if (commandName === 'stars') { await starsCommand(interaction); + } else if (commandName === 'modrinth') { + await modrinthCommand(interaction); } else if (commandName === 'rolypoly') { await interaction.reply( 'https://media.discordapp.net/attachments/985048903126769764/985051373886382100/rollin-time.gif?width=324&height=216' diff --git a/src/tagsTags.ts b/src/tagsTags.ts index 8420914..c4036fc 100644 --- a/src/tagsTags.ts +++ b/src/tagsTags.ts @@ -18,6 +18,7 @@ export const getTags = async (): Promise => { return raw.map((tag) => { if (tag.embed?.color) { + // @ts-expect-error f tag.embed.color = COLORS[tag.embed.color]; }