To add roles, you need the member
object. To get this, you need to know the guild that the member is in, so this would be impossible in on_ready()
unless you wanted to iterate through every single guild in the guild cache (for guild in bot.guilds:
if you're really interested in doing that, but still be sure to read the rest of this).
First off, to even be able to obtain a member
object, you need to have your intents enabled. You can do this by just changing your bot
ClientUser to commands.Bot(command_prefix="$", intents = discord.Intents.all())
, as well as enabling your intents enabled on the Developer Portal.
For the example that I'll show you I will be using a command, but if you really want to use the on_ready()
function you can by just replacing every ctx.guild
with guild
as you iterate through the guild cache as seen above. I also used the discord.utils.get
method to define my role, any method that you use is fine so long as you end up with a role object
@bot.command()
def addRole(ctx):
role = discord.utils.get(ctx.guild.roles, name = "Role Name")
member = ctx.guild.get_member(bot.user.id)
await member.add_roles(role)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…