Poll System

Here is the code of the poll system I have.
 avatar
unknown
javascript
2 years ago
7.2 kB
37
Indexable
const { SlashCommandBuilder, EmbedBuilder } = require("@discordjs/builders");
const mongoClient = require("../../dbsettings");

module.exports = {
    data: new SlashCommandBuilder()
        .setName('polls')
        .setDescription('Manage poll settings')
        .addSubcommandGroup(subcommandGroup =>
            subcommandGroup
                .setName('channels')
                .setDescription('Set poll channels')
                .addSubcommand(subCmd =>
                    subCmd
                        .setName('set')
                        .setDescription('Set poll channels')
                        .addStringOption(option =>
                            option
                                .setName('type')
                                .setDescription('Choose an option')
                                .addChoices(
                                    { name: 'Channel where polls will be sent', value: 'polls' },
                                    { name: 'Channel where polls information will be sent', value: 'polls-information' },
                                    { name: 'Channel where polls results will be sent', value: 'polls-results' },
                                )
                                .setRequired(true)
                        )
                        .addChannelOption(option =>
                            option
                                .setName('channel')
                                .setDescription('Select a channel for the chosen type')
                                .setRequired(true)
                        )
                )
                .addSubcommand(subcmd =>
                    subcmd
                        .setName('list')
                        .setDescription('See the list of the channels set for the polls')
                    )
        )
        .addSubcommandGroup(subgroup =>
            subgroup
                .setName('roles')
                .setDescription('Set the roles for the polls')
                .addSubcommand(subCmd =>
                    subCmd
                        .setName('set')
                        .setDescription('Set the roles for the polls')
                        .addStringOption(option =>
                            option
                                .setName('type')
                                .setDescription('Choose an option')
                                .addChoices(
                                    { name: 'Create Polls', value: 'create' },
                                    { name: 'Finish Polls', value: 'finish' }
                                )
                                .setRequired(true)
                        )
                        .addRoleOption(option =>
                            option
                                .setName('role')
                                .setDescription('Select a role for the chosen type')
                                .setRequired(true)
                        )
                )
                .addSubcommand(subcmd =>
                    subcmd
                        .setName('list')
                        .setDescription('See the list of the roles set for the polls')
                    )
        ),


    async execute(interaction) {
        const subcommandName = interaction.options.getSubcommand();

        if (subcommandName === 'channels') {
            const type = interaction.options.getString('type');
            const channelId = interaction.options.getChannel('channel').id;
            const guildId = interaction.guildId;

            try {
                await mongoClient.connect();
                const db = mongoClient.db('nxrluu');
                const channelsCollection = db.collection('polls_channels');

                const existingChannelData = await channelsCollection.findOne({ guildId: guildId, type: type });

                if (existingChannelData && existingChannelData.channelId === channelId) {
                    const errEmbed = new EmbedBuilder()
                        .setColor(0xFF0000)
                        .setTitle('Error')
                        .setDescription(`The channel <#${channelId}> it's already set to be the **${type}**`);

                    await interaction.reply({ embeds: [errEmbed] });
                    return;
                }

                await channelsCollection.updateOne(
                    { guildId: guildId, type: type },
                    { $set: { channelId: channelId } },
                    { upsert: true }
                );

                const successEmbed = new EmbedBuilder()
                    .setColor(0x00FF00)
                    .setDescription(`The channel <#${channelId}> has been successfully set to be the **${type}**.`);

                await interaction.reply({ embeds: [successEmbed] });

            } catch (err) {
                console.error(err);

                const errorEmbed = new EmbedBuilder()
                    .setColor(0xFF0000)
                    .setTitle('Error')
                    .setDescription('Error, check the terminal.');

                await interaction.reply({ embeds: [errorEmbed] });
            } finally {
                await mongoClient.close();
            }
        } else if (subcommandName === 'roles') {
    const type = interaction.options.getString('type');
    const roleId = interaction.options.getRole('role').id;
    const guildId = interaction.guildId;

    try {
        await mongoClient.connect();
        const db = mongoClient.db('nxrluu');
        const rolesCollection = db.collection('polls_roles');

        const existingRoleData = await rolesCollection.findOne({ guildId: guildId, type: type });

        if (existingRoleData && existingRoleData.roleId === roleId) {
            const errEmbed = new EmbedBuilder()
                .setColor(0xFF0000)
                .setTitle('Error')
                .setDescription(`El rol <@&${roleId}> ya está configurado para ${type} polls.`);
            await interaction.reply({ embeds: [errEmbed] });
            return;
        }

        await rolesCollection.updateOne(
            { guildId: guildId, type: type },
            { $set: { roleId: roleId } },
            { upsert: true }
        );

        const successEmbed = new EmbedBuilder()
            .setColor(0x00FF00)
            .setTitle('Polls Roles Set')
            .setDescription(`El rol <@&${roleId}> se ha configurado correctamente para la opción **${type}**.`);
        await interaction.reply({ embeds: [successEmbed] });

            } catch (err) {
                console.error(err);

                const errorEmbed = new EmbedBuilder()
                    .setColor(0xFF0000)
                    .setTitle('Error')
                    .setDescription('Error, check the terminal.');

                await interaction.reply({ embeds: [errorEmbed] });
            } finally {
                await mongoClient.close();
            }
        }
    },
};
Editor is loading...