jillo-bot/src/commands/monitor.ts

68 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-06-08 02:57:15 +02:00
import { SlashCommandBuilder } from '@discordjs/builders';
import { CommandInteraction, MessageEmbed } from 'discord.js';
2022-11-25 16:23:40 +01:00
import got from 'got';
2022-06-08 01:28:20 +02:00
const rand = require('random-seed').create();
2022-11-25 16:23:40 +01:00
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';
2022-06-08 01:28:20 +02:00
const images = [
'img1',
'img2',
'img3',
'img4',
'img5',
'img7',
'img8',
'img9',
'img10',
'img11',
'img12',
'img14',
'img15',
'img16',
'img17',
'img19',
'img20'
];
module.exports = {
data: new SlashCommandBuilder()
.setName('monitor')
.setDescription('Monitor')
2022-07-16 21:24:13 +02:00
.addStringOption((option) =>
option
.setName('what')
.setDescription('Monitor what? Examples include: "lobby", "bedroom", "park", "playground", etc...')
.setRequired(true)
),
2022-06-08 01:28:20 +02:00
2022-06-08 03:46:00 +02:00
execute: async (interaction: CommandInteraction) => {
2022-11-25 16:23:40 +01:00
await interaction.deferReply({ephemeral: false});
let what = interaction.options.getString('what')!.toLowerCase();
const whatOrig = what;
2022-06-08 01:28:20 +02:00
if (what.startsWith('the ')) what = what.slice(4);
2022-11-25 16:23:40 +01:00
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;
2022-06-08 01:28:20 +02:00
const embed = new MessageEmbed()
2022-11-25 16:23:40 +01:00
.setTitle(whatOrig)
.setImage(thumbURL)
2022-06-08 01:28:20 +02:00
.setFooter('Image may not always be accurate.')
.setTimestamp();
2022-11-25 16:23:40 +01:00
await interaction.followUp({
embeds: [embed]
2022-06-08 01:28:20 +02:00
});
}
2022-06-08 03:46:00 +02:00
};