Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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 bot命令为用户提供角色?_Python_Discord.py - Fatal编程技术网

Python 如何使用Discord bot命令为用户提供角色?

Python 如何使用Discord bot命令为用户提供角色?,python,discord.py,Python,Discord.py,我想做一个命令,给用户一个角色,而不提及他们,但我不知道怎么做 代码: 错误: PS C:\Users\Fred\Desktop\Cyberpapera2.0> & C:/Python/Python37/python.exe c:/Users/Fred/Desktop/Cyberpapera2.0/bot.py Ignoring exception in command blog: Traceback (most recent call last): File "C:\Pyth

我想做一个命令,给用户一个角色,而不提及他们,但我不知道怎么做

代码:

错误:

PS C:\Users\Fred\Desktop\Cyberpapera2.0> & C:/Python/Python37/python.exe c:/Users/Fred/Desktop/Cyberpapera2.0/bot.py
Ignoring exception in command blog:
Traceback (most recent call last):
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/Fred/Desktop/Cyberpapera2.0/bot.py", line 632, in blog
    await member.add_roles(role)
AttributeError: 'NoneType' object has no attribute 'add_roles'

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

Traceback (most recent call last):
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'add_roles'

您的错误是由于
member
的默认值为
None

当在没有参数的情况下调用命令时,例如通过
!博客
成员
将是
,因此当到达行,
等待成员。添加角色(角色)
时,会出现
属性错误
,告诉您
'NoneType'对象没有属性“添加角色”

您应该处理默认值大小写,并检查
成员何时为None


或者,如果您不想在未传递
成员时处理该命令,则可以完全删除默认值并处理。

如果调用方未指定角色,则哪个用户应获得该角色?@PatrickHaugh any user
PS C:\Users\Fred\Desktop\Cyberpapera2.0> & C:/Python/Python37/python.exe c:/Users/Fred/Desktop/Cyberpapera2.0/bot.py
Ignoring exception in command blog:
Traceback (most recent call last):
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/Fred/Desktop/Cyberpapera2.0/bot.py", line 632, in blog
    await member.add_roles(role)
AttributeError: 'NoneType' object has no attribute 'add_roles'

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

Traceback (most recent call last):
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'add_roles'