我有一个机器人,它会将dm ping发送给用户,该用户的ID作为命令中的参数给出。
这是命令:.spam ID #_OF_PINGS
我遇到以下错误:
Ignoring exception in command spam:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "bot.py", line 16, in spam
await ctx.send(f'Started pinging {user.name} {num} times.')
AttributeError: 'NoneType' object has no attribute 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'
这是我的代码:
from discord.ext import commands
token = 'MyBotTokenHere'
prefix = '.'
client = commands.Bot(command_prefix=prefix)
client.remove_command("help")
@client.event
async def on_ready():
print('Ready')
@client.command()
async def spam(ctx, id1: int, num: int):
user = client.get_user(id1)
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<@{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
client.run(token)
昨天工作正常,但今天由于某种原因它坏了。
如何解决?
附言我将其托管在Heroku上
答案 0 :(得分:1)
您可以为此使用转换器:
@client.command()
async def spam(ctx, user: discord.User, num: int):
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<@{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
通过这种方式,Discord将自动尝试获取与您作为参数传递的discord.User
相对应的id
实例,如果您想&也可以@mention
某人&仍然可以。
此外,从Discord 1.5.0开始,您现在需要在初始化机器人时传递intents
,您显然还没有这样做。更新您的Discord,并使用以下内容初始化您的漫游器:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=prefix, intents=intents)
有关API documentation中intents
的方式和原因的更多信息。您还需要在机器人的Privileged Intents页面上启用members
,链接的API文档中也对此进行了说明。