Skip to content

Instantly share code, notes, and snippets.

@SrinSS01
Created February 26, 2023 05:16
Show Gist options
  • Save SrinSS01/9cb1f8ce806dad75e7f90696164d6ed7 to your computer and use it in GitHub Desktop.
Save SrinSS01/9cb1f8ce806dad75e7f90696164d6ed7 to your computer and use it in GitHub Desktop.
const {Client, Intents, MessageActionRow, MessageEmbed, MessageSelectMenu, Permissions} = require('discord.js');
const {token} = require('./config.json');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CONTENT
]
});
client.once('ready', () => {
console.log(`${client.user.tag} has logged in!`);
});
client.on('messageCreate', message => {
if (message.author.bot) return;
if (!message.content.startsWith('/create-dp'))
return;
if (!message.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) return message.reply('You do not have the permission \`MANAGE_SERVER\`');
message.reply('Send messages in `roleID label emoji` syntax! Once finished say `done`.');
const filter = m => m.author.id === message.author.id;
const collector = message.channel.createMessageCollector(filter, {max: 10000});
let roles = [];
const sender = message.author.id;
collector.on('collect', async (msg) => {
if (sender !== msg.author.id)
return;
if (msg.author.bot) return;
if (!msg.content) return message.channel.send('Invalid syntax');
if (msg.content.toLowerCase() === 'done') return collector.stop('DONE');
if (!msg.content.split(' ')[0].match(/[0-9]{18}/g)) return message.channel.send('Invalid syntax');
const roleid = msg.content.split(' ')[0];
const role = message.guild.roles.cache.get(roleid);
if (!role) return message.channel.send('Invalid role');
const label = msg.content.split(' ').slice(1, msg.content.split(' ').length - 1).join(' ');
const reaction = (await msg.react(msg.content.split(' ').slice(msg.content.split(' ').length - 1).join(' ')).catch(/*() => null*/console.log));
const final = {
role: roleid, label: label, emoji: reaction ? reaction.emoji.id || reaction.emoji.name : null,
};
roles.push(final);
})
collector.on('end', async (msgs, reason) => {
if (reason === 'DONE') {
const embed = new MessageEmbed()
.setTitle('Dropdown roles!')
.setDescription('Select The Games Below via the Dropdown')
.setColor('RANDOM')
.setImage('https://i.imgur.com/g1SKkpm.png')
.setTimestamp();
// Convert to [MessageSelectOptionData]
const options = roles.map(role => ({
label: role.label,
value: role.role,
emoji: role.emoji ? `${role.emoji}` : null,
}));
// console.log(options);
let menu = new MessageSelectMenu()
.setCustomId('select')
.setPlaceholder('Select your roles')
.setMinValues(0)
.setMaxValues(Math.min(options.length, 25))
.addOptions([
...options
])
// Generate random id
menu.customId = "select-" + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
const row = new MessageActionRow()
.addComponents(menu);
try {
let roleIds = roles.map(role => role.role);
// Write as key to roles.json
const rolesFile = require('./roles.json');
rolesFile[menu.customId] = roleIds;
require('fs').writeFileSync('./roles.json', JSON.stringify(rolesFile, null, 4));
message.channel.send({embeds: [embed], components: [row]})
} catch (error) {
message.reply(`An error occured: ${error}`);
}
}
});
});
client.on('interactionCreate', interaction => {
if (!interaction.isSelectMenu()) return;
if (!interaction.customId.startsWith('select-')) return;
try {
// Remove roles in roles.json with interaction.customId as key
const rolesFile = require('./roles.json');
for (const roleId of rolesFile[interaction.customId]) {
interaction.member.roles.remove(roleId);
}
for (var role of interaction.values) {
// Assign role to user
interaction.member.roles.add(role);
}
// Inform user
interaction.reply({content: 'Updated your roles.', ephemeral: true});
} catch (error) {
console.error(error);
interaction.reply({content: `Could not update roles: ${error}`, ephemeral: true});
}
});
client.login(token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment