Search code examples
javascriptnode.jsdiscorddiscord.js

My Bot is having trouble changing nicknames on my server


My Discord Bot, made in nodeJS, sometimes changing the nickname, sometimes not. All other functions are working properly, however when someone sends the Modal. There is a chance that their nickname will be changed according to the button they selected, which is described in the code, but there is an even greater chance that the person's nickname will not be changed. I wanted to know if it's an error in the code, or in my server. Bot role is set to admin

Some parts of the code are in Portuguese(BR)

Code:

client.on('interactionCreate', async (interaction) =>
{ 
    try {
        if(interaction.isButton()) //Every Button Pressed
            if(interaction.customId == process.env.FIRST_ROLE) 
            {
                const role = interaction.guild.roles.cache.get(process.env.FIRST_ROLE)
        
                if(!role) //a
                {
                    return;
                }
        
                //If the member has the role.
                //const interactionMember = interaction.guild.members.cache.get(interaction.user.id); //Member that interact  

                //Modal Part
                const modal = new ModalBuilder(
                    {
                        customId: `rolePMSetModal-${interaction.user.id}`,
                        title: 'Informações',
                    });
                
                const nameInput = new TextInputBuilder(
                    {
                        customId: 'nameInput',
                        label: process.env.FIRST_INPUT_TEXT,
                        style: TextInputStyle.Short,
                    });

                const idInput = new TextInputBuilder(
                    {
                        customId: 'idInput',
                        label: process.env.SECOND_INPUT_TEXT,
                        style: TextInputStyle.Short,
                    });

                const firstActionRow = new ActionRowBuilder().addComponents(nameInput);
                const secondActionRow =  new ActionRowBuilder().addComponents(idInput);
                const nameThatUserSelected = null;
                const idThatUserSelected = null;

                modal.addComponents(firstActionRow, secondActionRow);

                await interaction.showModal(modal)         
            } 
        
            if(interaction.customId == process.env.SECOND_ROLE) //Butoes De Selecionar Set
            {
                const role = interaction.guild.roles.cache.get(process.env.SECOND_ROLE)
        
                if(!role) //s
                {
                    return;
                }
        
                //If the member has the role.
                //const interactionMember = interaction.guild.members.cache.get(interaction.user.id); //Member that interact    

                //Modal
                const modal = new ModalBuilder(
                    {
                        customId: `roleExercitoSetModal-${interaction.user.id}`,
                        title: 'Informações',
                    });
                
                const nameInput = new TextInputBuilder(
                    {
                        customId: 'nameInput',
                        label: process.env.FIRST_INPUT_TEXT,
                        style: TextInputStyle.Short,
                    });

                const idInput = new TextInputBuilder(
                    {
                        customId: 'idInput',
                        label: process.env.SECOND_INPUT_TEXT,
                        style: TextInputStyle.Short,
                    });

                const firstActionRow = new ActionRowBuilder().addComponents(nameInput);
                const secondActionRow =  new ActionRowBuilder().addComponents(idInput);

                modal.addComponents(firstActionRow, secondActionRow);

                await interaction.showModal(modal)
            }      

        if(interaction.isModalSubmit)
        {
            if(interaction.customId == `roleExercitoSetModal-${interaction.user.id}`)
            {
                const role = interaction.guild.roles.cache.get(process.env.SECOND_ROLE)
                const nameValue = interaction.fields.getTextInputValue('nameInput')
                const idValue = interaction.fields.getTextInputValue('idInput')
                if(Number.isInteger(idValue)) return;
                if(nameValue.length > 12) return;
                try {
                    interaction.guild.members.cache.get(interaction.user.id).setNickname(`[AL SD | EB] ${nameValue} | ${idValue}`).catch(error => {console.log('There is a error to change nickname!')});    
                    interaction.member.roles.add(process.env.SECOND_ROLE);
                    interaction.member.roles.add(process.env.MAIN_ROLE);
                    interaction.guild.channels.cache.get(process.env.LOG_CHANNEL).send(`Usuario: ${interaction.user}, Selecionou Uma Guarnição...\nGuarnição: ${role}\nId: **${idValue}**\nNome: **${nameValue}**`); //Log
                    const replyMsg = interaction.reply("```Usuario Setado!```");
                    if(replyMsg){
                        interaction.deleteReply(replyMsg); 
                    } else {
                        console.log('Nao foi possivel Deletar A Mensagem.')
                    }
                } catch (error) {
                    console.log(error);
                }    
            }

            if(interaction.customId == `rolePMSetModal-${interaction.user.id}`)
            {
                const role = interaction.guild.roles.cache.get(process.env.FIRST_ROLE)
                const nameValue = interaction.fields.getTextInputValue('nameInput')
                const idValue = interaction.fields.getTextInputValue('idInput')
                if(Number.isInteger(idValue)) return;
                if(nameValue.length > 12) return;
                try {
                    interaction.guild.members.cache.get(interaction.user.id).displayName.se(`[ALN | PMA] ${nameValue} | ${idValue}`).catch(error => {console.log('There is a error to change nickname!')});     
                    interaction.member.roles.add(process.env.FIRST_ROLE);
                    interaction.member.roles.add(process.env.MAIN_ROLE);
                    interaction.guild.channels.cache.get(process.env.LOG_CHANNEL).send(`Usuario: ${interaction.user}, Selecionou Uma Guarnição...\nGuarnição: ${role}\nId: **${idValue}**\nNome: **${nameValue}**`); //Log
                    const replyMsg = interaction.reply("```Usuario Setado!```");
                    if(replyMsg){
                        interaction.deleteReply(replyMsg); 
                    } else {
                        console.log('Nao foi possivel Deletar A Mensagem.')
                    }
                } catch (error) {
                    console.log(error);
                }    
            }
        }
    } catch (error) {
        console.log(error);
    }
})

I am looking to know what is the problem with the Bot


Solution

  • You can try using the <GuildMember>.manageable property to determine if you are able to manage this members nickname. Note this will return false if used on the bot.

    https://old.discordjs.dev/#/docs/discord.js/main/class/GuildMember?scrollTo=manageable https://github.com/discordjs/discord.js/blob/main/packages/discord.js/src/structures/GuildMember.js#L265