Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python discord.py如何为用户分配角色v1.0版_Python_Python 3.x_Discord_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python discord.py如何为用户分配角色v1.0版

Python discord.py如何为用户分配角色v1.0版,python,python-3.x,discord,discord.py,discord.py-rewrite,Python,Python 3.x,Discord,Discord.py,Discord.py Rewrite,这是我用来给输入命令的人分配角色的代码。我的服务器中可用的角色是rs6,因此代码应该是!设置角色rs6,但它不工作 import discord import logging from discord.utils import get from discord.ext import commands logging.basicConfig(level=logging.INFO) bot = commands.Bot(command_prefix='!') @bot.event async de

这是我用来给输入命令的人分配角色的代码。我的服务器中可用的角色是rs6,因此代码应该是
!设置角色rs6
,但它不工作

import discord
import logging
from discord.utils import get
from discord.ext import commands
logging.basicConfig(level=logging.INFO)
bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')
@bot.command(pass_context=True)
async def setrole(ctx, a: str):
    member = ctx.message.author
    role = discord.utils.get(member.guild.roles, name=a)
    await member.add_roles(member, role)
我试图在第
role=discord.utils.get(member.guild.roles,name=a)行之后执行
print(role)

它打印了rs6,这是正确的。有人请帮帮我!谢谢

您可以使用命令装饰器来完成一件时髦的事情。与将arg的类型设置为
str
的方式相同,也可以将其设置为不协调对象:

@bot.command()#请注意,在重写过程中会自动传递上下文
异步def setrole(ctx,角色:discord.role):
等待ctx.author.add_角色(角色)
等待ctx.send(f“我给了你{角色.提到}!”)
如果找不到角色,您可能需要处理错误处理程序:

@setrole.error
异步定义角色错误(ctx,错误):
如果isinstance(错误,commands.errors.BadArgument):
等待ctx.send(“我找不到那个角色!”)

参考文献:


您可以使用命令装饰器完成一件时髦的事情。与将arg的类型设置为
str
的方式相同,也可以将其设置为不协调对象:

@bot.command()#请注意,在重写过程中会自动传递上下文
异步def setrole(ctx,角色:discord.role):
等待ctx.author.add_角色(角色)
等待ctx.send(f“我给了你{角色.提到}!”)
如果找不到角色,您可能需要处理错误处理程序:

@setrole.error
异步定义角色错误(ctx,错误):
如果isinstance(错误,commands.errors.BadArgument):
等待ctx.send(“我找不到那个角色!”)

参考文献:


@哈里听到这个消息很高兴!祝你其余的机器人好运:^)@harry很高兴听到这个消息!祝您的其他机器人好运:^)
Ignoring exception in command setrole:
Traceback (most recent call last):
  File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Workshop\example_bot.py", line 19, in setrole
    await member.add_roles(member, role)
  File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
  File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 223, in request
    raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role