jillo-bot/src/commands/monitor.ts

54 lines
2.0 KiB
TypeScript

import { EmbedBuilder, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import got from 'got';
import { knownServers } from '../lib/knownServers';
import { Command } from '../types/index';
const rand = require('random-seed').create();
const imagesEndpoint = 'https://commons.wikimedia.org/w/api.php?action=query&cmlimit=500&cmtitle=Category%3ALiminal_spaces&cmtype=file&list=categorymembers&format=json';
const imageEndpoint = 'https://commons.wikimedia.org/w/api.php?action=query&piprop=thumbnail&pithumbsize=200&prop=pageimages&titles={}&format=json';
export default {
data: new SlashCommandBuilder()
.setName('monitor')
.setDescription('Monitor')
.addStringOption((option) =>
option
.setName('what')
.setDescription('Monitor what? Examples include: "lobby", "bedroom", "park", "playground", etc...')
.setRequired(true)
),
serverWhitelist: [...knownServers.firepit_extended, ...knownServers.fbi],
execute: async (interaction: CommandInteraction) => {
if (!interaction.isChatInputCommand()) return;
await interaction.deferReply({ephemeral: false});
let what = interaction.options.getString('what', true).toLowerCase();
const whatOrig = what;
if (what.startsWith('the ')) what = what.slice(4);
rand.seed(what.trim());
const images = JSON.parse((await got(imagesEndpoint)).body);
const imagesArray = images.query.categorymembers;
const img = imagesArray[rand.range(imagesArray.length)];
const thumb = JSON.parse((await got(imageEndpoint.replace('{}', encodeURI(img.title)))).body);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const thumbURL = Object.values(thumb.query.pages)[0].thumbnail.source;
const embed = new EmbedBuilder()
.setTitle(whatOrig)
.setImage(thumbURL)
.setFooter({text: 'Image may not always be accurate.'})
.setTimestamp();
await interaction.followUp({
embeds: [embed]
});
}
} satisfies Command;