Fixed help command when called in DMs

This commit is contained in:
Lily Wonhalf 2021-09-06 02:33:01 -04:00
parent 0f5e9d011b
commit 14dd8a5453

View file

@ -13,10 +13,10 @@ const cachelessRequire = (path) => {
}; };
const cleanString = (str) => str === null || str === undefined ? null : str const cleanString = (str) => str === null || str === undefined ? null : str
.replace("_", " ") .replace('_', ' ')
.split(" ") .split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" "); .join(' ');
const mainPage = (id) => { const mainPage = (id) => {
const buttons = Object.keys(CommandCategory).map(c => ({ const buttons = Object.keys(CommandCategory).map(c => ({
@ -28,7 +28,7 @@ const mainPage = (id) => {
return { return {
embeds: [{ embeds: [{
title: "Help command", title: 'Help command',
color: 0xA95B44, color: 0xA95B44,
description: description:
'Spot stands for **S**imply **P**lural b**ot**. This bot is mainly used to generate changelogs and help ' + 'Spot stands for **S**imply **P**lural b**ot**. This bot is mainly used to generate changelogs and help ' +
@ -39,8 +39,8 @@ const mainPage = (id) => {
'available in the corresponding category, or type `.help <category name>`.\n' 'available in the corresponding category, or type `.help <category name>`.\n'
}], }],
components: [{ components: [{
"type": 1, 'type': 1,
"components": buttons, 'components': buttons,
}] }]
} }
@ -57,11 +57,11 @@ const categoryPage = (cat, id, direct = false) => {
color: 0xA95B44, color: 0xA95B44,
}], }],
components: [{ components: [{
"type": 1, 'type': 1,
"components": [{ 'components': [{
type: 2, type: 2,
style: 2, style: 2,
label: "Back", label: 'Back',
custom_id: `help-${id}-home`, custom_id: `help-${id}-home`,
}], }],
}] }]
@ -69,13 +69,13 @@ const categoryPage = (cat, id, direct = false) => {
if (direct) delete data.components; if (direct) delete data.components;
if (cat.toLowerCase() == cleanString(CommandCategory.RESOURCE).toLowerCase()) if (cat.toLowerCase() === cleanString(CommandCategory.RESOURCE).toLowerCase())
data.embeds[0].fields = messages.getList(); data.embeds[0].fields = messages.getList();
else else
data.embeds[0].fields = Array.from(commands.keys()).map(x => { data.embeds[0].fields = Array.from(commands.keys()).map(x => {
const cmd = cachelessRequire(commands.get(x)); const cmd = cachelessRequire(commands.get(x));
if (cleanString(cmd.category)?.toLowerCase() != cat.toLowerCase()) return; if (cleanString(cmd.category)?.toLowerCase() !== cat.toLowerCase()) return;
return { name: x, value: cmd.description ?? "No description."}; return { name: x, value: cmd.description ?? 'No description.'};
}).filter(x => x); }).filter(x => x);
return data; return data;
@ -99,11 +99,11 @@ class Help
if (!args[0]) if (!args[0])
await bot.api.channels(message.channel.id).messages.post({ data: mainPage(message.author.id) }); await bot.api.channels(message.channel.id).messages.post({ data: mainPage(message.author.id) });
else else
await bot.api.channels(message.channel.id).messages.post({ await bot.api.channels(message.channel.id).messages.post({
data: data:
categoryPage(args.join(" "), message.author.id, true) categoryPage(args.join(' '), message.author.id, true)
?? { ?? {
content: "Category not found.", content: 'Category not found.',
message_reference: { message_id: message.id, guild_id: message.guild?.id }, message_reference: { message_id: message.id, guild_id: message.guild?.id },
allowed_mentions: { parse: [] }, allowed_mentions: { parse: [] },
} }
@ -114,25 +114,29 @@ class Help
module.exports = new Help(); module.exports = new Help();
module.exports.interactionHandler = async (event) => { module.exports.interactionHandler = async (event) => {
const user = event.member.user.id; const user = event.member ? event.member.user.id : event.user.id;
const custom_id = event.data.custom_id; const customId = event.data.custom_id;
let ret; let ret;
if (!custom_id.startsWith(`help-${user}`)) if (!customId.startsWith(`help-${user}`)) {
ret = { ret = {
type: 4, type: 4,
data: { data: {
flags: 64, flags: 64,
content: "This help command was sent by someone else. Please run `.help` again.", content: 'This help command was sent by someone else. Please run `.help` again.',
}, },
} }
else if (custom_id.endsWith('home')) } else if (customId.endsWith('home')) {
ret = { type: 7, data: mainPage(user) }; ret = { type: 7, data: mainPage(user) };
else { } else {
let page = categoryPage(cleanString(custom_id.split(`help-${user}-`).join("")), user); let page = categoryPage(cleanString(customId.split(`help-${user}-`).join('')), user);
if (page) ret = { type: 7, data: page };
else ret = { type: 4, data: { flags: 64, content: "Category not found." }} if (page) {
ret = { type: 7, data: page };
} else {
ret = { type: 4, data: { flags: 64, content: 'Category not found.' }}
}
} }
return await bot.api.interactions(event.id, event.token).callback.post({ data: ret }) return await bot.api.interactions(event.id, event.token).callback.post({ data: ret })