Hello! This is my first ever snippet. This allows you to find out if a slash command exists and if it does send the JSON data in the chat, all error handling is included in the code and everything is properly quoted. All you have to do is make a slash command with the following properties - Name: find-slash, 2 options with names "name" and an optional option named "guild" both with string type. It should look something like this: https://ibb.co/WnqJpGk
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
/* Extending the interaction tokens life :D */
await lib.discord.interactions['@1.0.1'].responses.create({
token: context.params.event.token,
response_type: 'DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE',
});
/* Values */
let commandName = context.params.event.data.options[0].value.toLowerCase();
let guildID = context.params.event.data.options[1]?.value || null;
/* Retrieving the slash commands for the given or current guild */
try {
global.commandObject = await lib.discord.commands['@0.1.1'].list({
guild_id: `${guildID ? guildID : `${context.params.event.guild_id}`}`,
with_localizations: false,
});
} catch (e) {
/* Adding error message for Guild ID issues */
await lib.discord.interactions['@1.0.1'].followups.create({
token: context.params.event.token,
content: ``,
embeds: [
{
title: `Hm, that's akward. The Guild ID provided gave an error.`,
description: `
> __Reasons__:
• The provided Guild ID is actually not valid.
• The slash command doesn't exist for this guild or the given one.
> __Quick Tips:__
• Snowflake IDs are 17-19 digits long & only contain numeric characters.
• If a valid Guild ID was provided make sure I was invited with the application.commands scope for that guild.
`,
},
],
});
return null;
}
/* Searching for the object using the index with provided name */
let objectIndex = commandObject.findIndex(function (command) {
return command.name == `${commandName}`;
});
/* Trigged if the command with the name is found */
if (commandObject[objectIndex]) {
try {
await lib.discord.interactions['@release'].followups.create({
token: context.params.event.token,
content: `Slash Command found! Here's the json output:
\`\`\`js
${JSON.stringify(commandObject[objectIndex], null, 3)}
\`\`\`
`,
});
} catch (e) {
/* Error message for char length */
await lib.discord.interactions['@release'].followups.create({
token: context.params.event.token,
content: `Sadge, the character length of the object for your command is too big.`,
attachments: [
{
id: 0,
description: `JSON data for ${commandName}`,
filename: 'object.txt',
file: Buffer.from(
`${JSON.stringify(commandObject[objectIndex], null, 3)}`
),
},
],
});
}
/* Debug */
console.log(`Slash command found | Name: ${commandName}`);
} else {
/* Error message for invalid command name */
await lib.discord.interactions['@1.0.1'].followups.create({
token: context.params.event.token,
content: `I doubt that slash command exists for the current or the given guild, re-check the name once again then try.`,
});
/* Debug */
console.log(`Slash command not found | Name: ${commandName}`);
}
/* Main Debug */
console.log(`
Index: ${objectIndex}
CommandObject: ${commandObject}
Name: ${commandName}
GuildID: ${guildID ? guildID : `${context.params.event.guild_id}`}
`);