modrinth command

This commit is contained in:
Ryan Cao 2022-08-29 10:44:40 +08:00
parent bc0725595d
commit 8852026e2c
No known key found for this signature in database
GPG key ID: 528A2C1B6656B97F
5 changed files with 110 additions and 1 deletions

View file

@ -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!);

99
src/commands/modrinth.ts Normal file
View file

@ -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<CacheType>) => {
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),
],
});
};

View file

@ -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,

View file

@ -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'

View file

@ -18,6 +18,7 @@ export const getTags = async (): Promise<Tag[]> => {
return raw.map((tag) => {
if (tag.embed?.color) {
// @ts-expect-error f
tag.embed.color = COLORS[tag.embed.color];
}