Discord.py nickall command

Question:

So I have been trying to make a bot command that can change all the members names in my server with one command, but everything I have tried didn’t work, I have also tried to research this but could not find where someone asked or made this. I apologize if this isn’t possible …

@client.command()
async def change_nicks(guild_id, new_nick):
guild = client.get_guild(827704596620115968) 
new_nickname = test
async for member in guild.fetch_members(): 
  await member.edit(nick=new_nickname) #change their nickname to the new one
Asked By: Monster png

||

Answers:

If your bot was made with the correct intents and has the server perms to change other users’ nicknames, working code should be something like:

#first, fetch the object referring to the guild you want to do this to.
guild = client.get_guild(GUILD_ID) 
new_nickname = 'foo'
async for member in guild.fetch_members(): #for all members:
    await member.edit(nick=new_nickname) #change their nickname to the new one
Answered By: memmy
from discord.ext import commands
import discord
... 
intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix='<your_prefix>', intents=intents)
...

@bot.command(name="nickall")
async def change_all_nicknames(ctx, nickname):

    for member in ctx.guild.members:
   
        if member.guild_permissions.administrator:
             continue
    
        await member.edit(nick=nickname)

...

<prefix>nickall "desired_nickname"

note: the members intent is a privileged intent, meaning that if your bot is in 75 servers or more, you will have to apply for bot verification.

To enable the guild members intent for "small bots" (bots in less than 75 servers), go to your Bot tab in the developer portal and enable the guild members intent ( and any other intents you require for your bot to function)

Answered By: henry groves
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.