对于未在python 3中完成的循环Discord.py bot mass DM失败

时间:2018-06-10 21:26:01

标签: python python-3.x for-loop discord discord.py

由于某种原因,我的discord.py机器人for循环似乎并不想完成。循环的要点是向大约20个用户发送相同的消息。问题是有些用户没有收到这些消息,而且我相信问题是在循环中。

for server_member in ctx.message.server.members:
    await client.send_message(server_member, message)

我对for循环没有多少经验,所以在我看来这似乎是最可能的问题。

这是每次尝试发送邮件后留下的错误代码。

Ignoring exception in command massdm
Traceback (most recent call last):
File "C:\Users\%myuseraccount%\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
ret = yield from coro(args, **kwargs)
File "C:\Users\%myuseraccount%\Desktop\assistbot.py", line 36, in massdm
await client.send_message(server_member, message)
File "C:\Users\%myuseraccount%\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\client.py", line 1152, in send_message
data = yield from self.http.send_message(channel_id, content, 
guild_id=guild_id, tts=tts, embed=embed)
File "C:\Users\%myuseraccount%\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\http.py", line 196, in request
raise Forbidden(r, data)
discord.errors.Forbidden: FORBIDDEN (status code: 403): Cannot send messages 
to this user
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\%myuseraccount%\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\ext\commands\bot.py", line 846, in 
process_commands
yield from command.invoke(ctx)
File "C:\Users\%myuseraccount%\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(ctx.args, **ctx.kwargs)
File "C:\Users\%myuseraccount%\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: 
Forbidden: FORBIDDEN (status code: 403): Cannot send messages to this user

1 个答案:

答案 0 :(得分:1)

Discord用户可以阻止来自所选服务器的直接消息。 for循环本身不是问题,您的机器人无法向禁用以下内容的人发送消息:

image (同样适用于桌面不和谐)

这就是discord.py引发此错误的原因:

Forbidden: FORBIDDEN (status code: 403): Cannot send messages to this user

您需要捕获此错误,以便循环继续,并向其他所有用户发送消息:

for server_member in ctx.message.server.members:
    try:
        await client.send_message(server_member, message)
    except discord.Forbidden:
        pass