[英]Discord.py | add role to someone
我無法在 discord.py 中創建“添加角色”命令。 我不知道出了什么問題; 它只是行不通。
@client.command()
@commands.has_role("Admin")
async def addrole(ctx):
user = ctx.message.author
role = discord.utils.get(user.server.roles, name="Test")
await client.add_roles(user, role)
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
@commands.has_role("Admin") # This must be exactly the name of the appropriate role
async def addrole(ctx):
member = ctx.message.author
role = get(member.server.roles, name="Test")
await bot.add_roles(member, role)
我認為您的代碼中唯一真正的錯誤是@bot.command
裝飾器中缺少pass_context=True
。 你可能見過一些沒有這個的代碼,但這可能屬於discord.py的實驗性“重寫”分支
@bot.command(pass_context=True)
async def giverole(ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"hey {ctx.author.name}, {user.name} has been giving a role called: {role.name}")
roleVer = 'BOT' #role to add user = ctx.message.author #user role = roleVer # change the name from roleVer to role await ctx.send("""Attempting to Verify {}""".format(user)) try: await user.add_roles(discord.utils.get(user.guild.roles, name=role)) #add the role except Exception as e: await ctx.send('There was an error running this command ' + str(e)) #if error else: await ctx.send("""Verified: {}""".format(user)) # no errors, say verified
@client.command()
async def addrole(ctx, member : discord.Member, role : discord.Role):
await member.add_roles(role)
用法:!addrole [成員] [角色]
注意:機器人只能給出比他低的角色!
這是給你的角色命令!
@client.command('role')
@commands.has_permissions(administrator=True) #permissions
async def role(ctx, user : discord.Member, *, role : discord.Role):
if role.position > ctx.author.top_role.position: #if the role is above users top role it sends error
return await ctx.send('**:x: | That role is above your top role!**')
if role in user.roles:
await user.remove_roles(role) #removes the role if user already has
await ctx.send(f"Removed {role} from {user.mention}")
else:
await user.add_roles(role) #adds role if not already has it
await ctx.send(f"Added {role} to {user.mention}")
以及錯誤:
@role.error
async def role_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send('**:x: | You do not have permission to use this command!**')
現在它完成了,你有一個角色命令!
使用此代碼:
role = discord.utils.get(ROLE_ID)
member.add_roles(role)
重要提示:機器人無法管理高於他的角色
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.