Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

Adding a role in discord.py failed with no error

So I got no error but it always sends me back the else message. Can someone say me what I made wrong?

import discord

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

async def on_message(message):
    member = message.author
    if message.content == "/verify":
        role = message.guild.get_role(ROLE_ID)
        await member.add_roles(role)
        await message.author.send("Du bist jetzt auf FlexGames Deescord verifiziert! Viel Spa? und lies dir #infos-regeln durch!")
    else:
        await message.author.send("Die Verifizierung ist fehlgeschlagen! Bitte probiere es erneut!")

(If you try this code, don't forget the bot.run('token') and bot=discord.Client() for example). I also gave the bot all permissions it need

EDIT: I updated the code to the new one and activated the intents for the bot, still doesn't work and either sends me back the else message or just gives no error.

Final Edit: I was too stupid to know I got something before that wrong (ar.wait_for) so his solution was right

question from:https://stackoverflow.com/questions/66051424/adding-a-role-in-discord-py-failed-with-no-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. You can only add roles to discord.Member instances, when defining the member variable you're getting the ID itself, not the discord.Member instance
member = message.author # Without the id attribute
  1. In Member.add_roles you need to pass discord.Role instances, not the ID's itself, you can get the instance with Guild.get_role
role = message.guild.get_role(role_id)
await member.add_roles(role)

Your code fixed:

async def on_message(message):
    member = message.author
    if message.content.startswith("/verify"):
        role = message.guild.get_role(ROLE_ID_HERE)
        await member.add_roles(role)

There's another issue, you need to enable intents.members and a few more intents, otherwise some attributes can be a NoneType

intents = discord.Intents.default()
intents.members = True # Explicitly enabling members as it's a privileged intent

client = discord.Client(intents=intents)

Also remember to enable privileged member intents in the developer portal, guide

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...