updated bot.js

mail@pastecode.io avatar
unknown
javascript
16 days ago
2.5 kB
1
Indexable
Never
const { Client, GatewayIntentBits } = require('discord.js');
const axios = require('axios');
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds, 
    GatewayIntentBits.GuildMessages, 
    GatewayIntentBits.GuildMembers
  ]
});

const GUILD_ID = '705123157438234734';
const ROLE_PARTNER = '705129728234881086';
const ROLE_AFFILIATE = '705130154904518756';
const ROLE_CREATOR = '705225579959418931';

client.once('ready', () => {
    console.log(`Logged in as ${client.user.tag}`);
});

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const { commandName } = interaction;

    if (commandName === 'assign_role') {
        const broadcaster_type = interaction.options.getString('broadcaster_type');
        const discord_user_id = interaction.options.getUser('discord_user_id').id;

        try {
            const guild = client.guilds.cache.get(GUILD_ID);
            if (!guild) {
                await interaction.reply({ content: 'Guild not found', ephemeral: true });
                return;
            }

            const member = await guild.members.fetch(discord_user_id);
            if (!member) {
                await interaction.reply({ content: 'Member not found', ephemeral: true });
                return;
            }

            let roleToAssign;
            if (broadcaster_type === 'partner') {
                roleToAssign = guild.roles.cache.get(ROLE_PARTNER);
            } else if (broadcaster_type === 'affiliate') {
                roleToAssign = guild.roles.cache.get(ROLE_AFFILIATE);
            } else {
                roleToAssign = guild.roles.cache.get(ROLE_CREATOR);
            }

            if (!roleToAssign) {
                await interaction.reply({ content: 'Role not found', ephemeral: true });
                return;
            }

            if (roleToAssign.position >= member.guild.me.roles.highest.position) {
                await interaction.reply({ content: 'Bot cannot assign this role', ephemeral: true });
                return;
            }

            await member.roles.add(roleToAssign);
            await interaction.reply({ content: 'Role assigned successfully', ephemeral: true });
        } catch (error) {
            console.error(error);
            await interaction.reply({ content: `Error assigning role: ${error.message}`, ephemeral: true });
        }
    }
});

client.login('YOUR_DISCORD_BOT_TOKEN');
Leave a Comment