djs-systems
Version:
The simplest way to build complex Discord bots.
258 lines (257 loc) • 12.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.suggest = void 0;
const discord_js_1 = require("discord.js");
const SimplyError_1 = require("./error/SimplyError");
const suggest_1 = __importDefault(require("./model/suggest"));
const misc_1 = require("./misc");
// ------------------------------
// ------ F U N C T I O N -------
// ------------------------------
/**
* An **Beautiful** suggestion system with buttons ;D | *Requires: [**manageSug()**](https://simplyd.js.org/docs/handler/manageSug)*
* @param msgOrint
* @param options
* @link `Documentation:` https://simplyd.js.org/docs/systems/suggestSystem
* @example simplydjs.suggestSystem(interaction, { channelId: '1234567890123' })
*/
async function suggest(msgOrint, options = { strict: false }) {
return new Promise(async (resolve) => {
try {
const { client } = msgOrint;
let url;
let suggestion;
let interaction;
if (msgOrint.commandId || !msgOrint.content) {
interaction = msgOrint;
if (!interaction.deferred)
await interaction.deferReply({ fetchReply: true });
suggestion =
options.suggestion ||
String(interaction.options.get('suggestion').value);
if (!suggestion)
return interaction.followUp({
content: 'Provide a suggestion to post.',
ephemeral: true
});
}
else if (!msgOrint.commandId && msgOrint.content) {
const attachment = msgOrint.attachments?.first();
url = attachment ? attachment.url : '';
if (options?.suggestion)
suggestion = options?.suggestion;
if (url)
suggestion = suggestion + ' ' + url;
if (!options.suggestion && msgOrint) {
const [...args] = msgOrint.content.split(/ +/g);
suggestion = args.slice(1).join(' ');
}
if (suggestion === '' || !suggestion)
return msgOrint.reply({ content: 'Provide a suggestion to post.' });
}
if (!options.embed) {
options.embed = {
footer: {
text: '©️ Rahuletto. npm i simply-djs',
iconURL: 'https://i.imgur.com/XFUIwPh.png'
},
color: (0, misc_1.toRgb)('#406DBC'),
title: 'New Suggestion'
};
}
const buttonStyles = {
upvote: {
style: options.buttons?.upvote?.style || discord_js_1.ButtonStyle.Primary,
emoji: options.buttons?.upvote?.emoji || '☑️'
},
downvote: {
style: options.buttons?.downvote?.style || discord_js_1.ButtonStyle.Danger,
emoji: options.buttons?.downvote?.emoji || '🇽'
},
votedInfo: {
style: options.buttons?.votedInfo?.style || discord_js_1.ButtonStyle.Success,
emoji: options.buttons?.votedInfo?.emoji || '❓'
}
};
if (buttonStyles?.upvote.style)
buttonStyles.upvote.style = (0, misc_1.toButtonStyle)(buttonStyles?.upvote.style);
if (buttonStyles?.downvote.style)
buttonStyles.downvote.style = (0, misc_1.toButtonStyle)(buttonStyles?.downvote.style);
if (buttonStyles?.votedInfo.style)
buttonStyles.votedInfo.style = (0, misc_1.toButtonStyle)(buttonStyles?.votedInfo.style);
const ch = client.channels.cache.get(options?.channelId) ||
options?.channelId;
if (!ch) {
if (options?.strict)
throw new SimplyError_1.SimplyError({
function: 'suggest',
title: `Invalid Channel (or) No VIEW_CHANNEL permission`,
tip: `Check the permissions (or) Try using another Channel ID.\nReceived ${options.channelId || 'undefined'}`
});
else
console.log(`SimplyError - suggest | Invalid Channel (or) No VIEW_CHANNEL permission\n\nCheck the permissions (or) Try using another Channel ID.\n Received ${options.channelId || 'undefined'}`);
}
const surebtn = new discord_js_1.ButtonBuilder()
.setStyle(discord_js_1.ButtonStyle.Success)
.setLabel('Suggest')
.setCustomId('send-suggestion');
const nobtn = new discord_js_1.ButtonBuilder()
.setStyle(discord_js_1.ButtonStyle.Danger)
.setLabel('Cancel')
.setCustomId('cancel-suggestion');
const sendRow = new discord_js_1.ActionRowBuilder().addComponents([
surebtn,
nobtn
]);
const embed = new discord_js_1.EmbedBuilder()
.setTitle(options.embed?.title || 'Are you sure?')
.setDescription(options.embed?.description ||
`Is this your suggestion ? \`\`\`${suggestion}\`\`\``)
.setTimestamp()
.setColor(options.embed?.color || (0, misc_1.toRgb)('#406DBC'))
.setFooter(options.embed?.footer
? options.embed?.footer
: {
text: '©️ Rahuletto. npm i simply-djs',
iconURL: 'https://i.imgur.com/XFUIwPh.png'
});
if (options?.embed?.fields)
embed.setFields(options.embed?.fields);
if (options?.embed?.author)
embed.setAuthor(options.embed?.author);
if (options?.embed?.image)
embed.setImage(options.embed?.image);
if (options?.embed?.thumbnail)
embed.setThumbnail(options.embed?.thumbnail);
if (options?.embed?.timestamp)
embed.setTimestamp(options.embed?.timestamp);
if (options?.embed?.title)
embed.setTitle(options.embed?.title);
if (options?.embed?.url)
embed.setURL(options.embed?.url);
let m;
if (interaction) {
m = await interaction.followUp({
embeds: [embed],
components: [sendRow],
ephemeral: true
});
}
else if (!interaction) {
m = await msgOrint.reply({
embeds: [embed],
components: [sendRow]
});
}
const filter = (m) => {
if (m.user.id === msgOrint.member.user.id)
return true;
m.reply({
content: `Only <@!${msgOrint.member.user.id}> can use these buttons!`,
ephemeral: true
});
return false;
};
const collector = m.createMessageComponentCollector({
filter: filter,
max: 1,
componentType: discord_js_1.ComponentType.Button,
time: (0, misc_1.ms)('15s') // 15 Seconds
});
collector.on('collect', async (b) => {
if (b.customId === 'send-suggestion') {
await b.reply({ content: 'Ok, Posted. :+1:', ephemeral: true });
await b.message.delete();
const suggestEmb = new discord_js_1.EmbedBuilder()
.setDescription(suggestion)
.setAuthor({
name: msgOrint.member.user.username,
iconURL: msgOrint.member.user.displayAvatarURL({
forceStatic: false
})
})
.setColor(options.embed?.color || (0, misc_1.toRgb)('#406DBC'))
.setFooter(options.embed?.footer
? options.embed?.footer
: {
text: '©️ Rahuletto. npm i simply-djs',
iconURL: 'https://i.imgur.com/XFUIwPh.png'
})
.addFields({
name: 'Status',
value: `\`\`\`\nWaiting for the response..\n\`\`\``
}, {
name: 'Percentage',
value: `${(options?.progress?.blank || '⬛').repeat(10)} [0% - 0%]`
});
const approve = new discord_js_1.ButtonBuilder()
.setEmoji(buttonStyles?.upvote?.emoji)
.setLabel('0')
.setStyle(buttonStyles?.upvote?.style ||
discord_js_1.ButtonStyle.Primary)
.setCustomId('plus-suggestion');
const no = new discord_js_1.ButtonBuilder()
.setEmoji(buttonStyles?.downvote?.emoji)
.setLabel('0')
.setStyle(buttonStyles?.downvote?.style ||
discord_js_1.ButtonStyle.Danger)
.setCustomId('minus-suggestion');
const whoVoted = new discord_js_1.ButtonBuilder()
.setEmoji(buttonStyles?.votedInfo?.emoji)
.setLabel('Who Voted?')
.setStyle(buttonStyles?.votedInfo?.style ||
discord_js_1.ButtonStyle.Success)
.setCustomId('who-voted-suggestion');
const row = new discord_js_1.ActionRowBuilder().addComponents([
approve,
no,
whoVoted
]);
await ch
.send({ embeds: [suggestEmb], components: [row] })
.then(async (ms) => {
const database = new suggest_1.default({
message: ms.id,
author: msgOrint.member.user.id,
progress: options?.progress
? options?.progress
: { blank: '⬛', up: '🟩', down: '🟥' }
});
await database.save();
resolve({
user: msgOrint.member,
suggestion: suggestion,
channel: ch
});
});
}
else if (b.customId === 'cancel-suggestion') {
b.message.delete();
}
});
collector.on('end', async (collected) => {
if (collected.size == 0) {
m.edit({
content: "Timeout.. Didn't post the suggestion.",
embeds: [],
components: []
});
}
});
}
catch (err) {
if (options?.strict)
throw new SimplyError_1.SimplyError({
function: 'suggest',
title: 'An Error occured when running the function',
tip: err.stack
});
else
console.log(`SimplyError - suggest | Error: ${err.stack}`);
}
});
}
exports.suggest = suggest;