简体   繁体   中英

Trying to make an autorole discord bot using Discord.py rewrite

I am trying to make an bot that as soon as a member joins, automatically gives them a role. This is my code and I can't seem to work out what the problem is. Hopefully someone more smart and experienced can help me

@client.event
async def on_member_join(member):
    role = get(member.guild.roles, name = ROLE)
    await member.add_roles(role)
    print(f'{member} was given {role}')

As the event is not producing any errors, it most likely means it's an issue with member intents.

Any events to do with members will require these intents to be enabled from the Discord developer portal and defined in your code. Your code is looking fine and should work, until you have enabled intents. Intents can be enabled from the bot page and should be off by default, for member intents, turn it on.

They can be defined within code after you have enabled in the portal:

intents = discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix=',', intents=intents)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM