This snippet contains two things: (1) Creation of the /feedback command. Please read the comments in the code carefully; (2) feedback command handler. The feedback will be sent to a centralized channel of your choosing.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const FEEDBACK_CHANNEL_ID = `CHANNEL_ID`; //channel id where feedback are sent
//---------- Press RUN one time, then delete from here ------------//
//---------- Start of command creation -------//
return lib.discord.commands['@0.0.0'].create({
name: 'feedback',
description: 'send feedback',
options: [
{
type: 3,
name: 'details',
description: 'content of your feedback',
required: true,
},
{
type: 11,
name: 'image',
description: 'send an image (only supports PNG, JPG, and GIF)',
required: false,
},
],
});
//---------- End of command creation -------//
//---------- Delete until here ------------//
//--------- Don't delete -------------//
const user = context.params.event.member?.user || context.params.event.user;
const guildId = context.params.event.guild_id;
try {
const details = context.params.event.data.options[0].value;
const imageId = context.params.event.data.options[1]?.value || null;
const image = imageId
? context.params.event.data.resolved.attachments[imageId]
: null;
let embed = {
type: 'rich',
title: `Feedback`,
description: '',
color: 0x00ff00,
fields: [
{
name: 'Sender',
value: `${user.username}#${user.discriminator}`,
inline: true,
},
{
name: 'Sender ID',
value: user.id,
inline: true,
},
{
name: 'Guild ID',
value: guildId || `via DM`,
inline: true,
},
{
name: `Details`,
value: details,
},
],
};
if(user.avatar){
embed['thumbnail'] = {
url: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}`,
height: 0,
width: 0
}
}
if (image?.url?.match(/.*(\.png|\.jpg|\.jpeg|\.gif)$/gi)) {
embed['image'] = {
url: image.url,
proxy_url: image.proxy_url,
height: 0,
width: 0,
};
} else if (image?.url) {
throw new Error(
`Your attachment is not an image! Please upload PNG, JPG, or GIF.`
);
}
await lib.discord.channels['@0.3.2'].messages.create({
channel_id: `${FEEDBACK_CHANNEL_ID}`,
content: ``,
embeds: [embed],
});
} catch (e) {
console.error(e);
return lib.discord.interactions['@1.0.1'].responses.ephemeral.create({
token: `${context.params.event.token}`,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: `Hi <@!${user.id}>, `,
embeds: [
{
type: 'rich',
title: `:x: Error encountered!`,
description: `Cause: ${e.message}`,
color: 0xff0000,
},
],
});
}
await lib.discord.interactions['@1.0.1'].responses.ephemeral.create({
token: `${context.params.event.token}`,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: `Hi <@!${user.id}>, `,
embeds: [
{
type: 'rich',
title: `Feedback sent!`,
description: `Thank you for sending your feedback!`,
color: 0x00ff00,
},
],
});