galactic-bot/structure/client/components/commands/utility/Lookup.js

110 lines
3.1 KiB
JavaScript
Raw Normal View History

const fetch = require('node-fetch');
const { Command } = require('../../../../interfaces/');
//Apparently hit ratelimits pretty damn quick and doesn't expire often.
class LookupCommand extends Command {
constructor(client) {
super(client, {
name: 'lookup',
module: 'utility',
usage: "<invite-code>",
showUsage: true,
parameterType: 'PLAIN',
examples: [
"SvJgtEj",
"discord.gg/SvJgtEj"
],
restricted: true, //For now
throttling: {
usages: 1,
duration: 30
}
});
this.client = client;
}
async execute(message, { params }) {
const regex = /discord(?:app\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/i;
const match = regex.exec(params);
let code = null;
if(match && match[1]) {
code = match[1];
} else {
const restrict = /([\w-]{2,255})/i.exec(params);
if(restrict && restrict[1]) {
code = restrict[1];
} else {
await message.respond(message.format('C_LOOKUP_FAILEDMATCH'));
return undefined;
}
}
let data = null;
try {
const request = await fetch(`https://discord.com/api/invite/${code}`);
data = await request.json();
if(data.code && data.message) {
await message.respond(message.format('C_LOOKUP_NONEXISTANT'));
return undefined;
}
} catch(error) {
await message.respond(message.format('C_LOOKUP_NONEXISTANT'));
return undefined;
}
let fields = [];
if(data.inviter) {
fields.push({
name: "Inviter",
value: `${data.inviter.username}#${data.inviter.discriminator} \`(${data.inviter.id})\``,
inline: true
});
}
if(data.channel) {
fields.push({
name: "Default Channel",
value: `#${data.channel.name} \`(${data.channel.id})\``,
inline: true
});
}
if(data.guild.features.length > 0) {
fields.push({
name: "Features",
value: data.guild.features.map(f=>`\`${f}\``).join(', '),
inline: true,
});
}
const embed = {
author: {
name: `${data.guild.name} (${data.guild.id})`,
icon_url: `https://cdn.discordapp.com/icons/${data.guild.id}/${data.guild.icon}.webp`
},
thumbnail: {
url: data.guild.splash ? `https://cdn.discordapp.com/splashes/${data.guild.id}/${data.guild.splash}.webp` : null
},
description: data.guild.description || null,
fields,
footer: {
text: `https://discord.gg/${data.code}`
}
};
return message.embed(embed);
}
}
module.exports = LookupCommand;