How do you assign roles with the discord.py rewrite?

一笑奈何 提交于 2019-12-24 10:43:02

问题


I am creating a discord bot using discord.py rewrite and am hosting iton repl.it. I am trying to add assignment of roles, but I get an error everytime I try to add it

I'be looked around stack overflow and have been unable to find a solution to adding roles. I've also looked through the documentation, but that just made me more confused.

import discord.utils 
@client.command()
async def role(ctx, * role):
  user = ctx.message.author
  role = discord.utils.get(user.guild.roles, name=f"{role}")
  await ctx.add_roles(user, role)

It should add the specified role to the message author but it just produces this error

File "main.py", line 18, in role
    await ctx.add_roles(user, role)
AttributeError: 'Context' object has no attribute 'add_roles'

回答1:


I added Alberto Poljak's suggestions and it worked!

import discord.utils 
@client.command()
async def role(ctx, * role: discord.Role):
  user = ctx.message.author
  await user.add_roles(role)


来源:https://stackoverflow.com/questions/56451752/how-do-you-assign-roles-with-the-discord-py-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!