首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DM Everyone命令

DM Everyone命令
EN

Stack Overflow用户
提问于 2020-09-27 03:58:22
回答 2查看 1.1K关注 0票数 0

我不明白为什么在这段代码中有一个错误:

代码语言:javascript
复制
@client.command()
async def dmall(ctx, message):
    for m in client.get_all_members():
        await m.send(message)
    await ctx.send("Done!")

我的DMs是打开的,但这里有一个错误:我也尝试将它与我的alt一起使用,但出现了相同的错误。

代码语言:javascript
复制
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 49, in dmall
    await m.send("Hello! This is a DM :)")
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 850, in send
    channel = await self._get_channel()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 243, in _get_channel
    ch = await self.create_dm()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/user.py", line 715, in create_dm
    data = await state.http.start_private_message(self.id)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 245, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50007): Cannot send messages to this user

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

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/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: HTTPException: 400 Bad Request (error code: 50007): Cannot send messages to this user

另外,顺便说一句,我不是出于恶意目的来使用它。我知道滥用这一点可能会导致禁令。

EN

回答 2

Stack Overflow用户

发布于 2020-09-27 04:03:24

使用try语句可以让它跳过所有不能发送消息的用户:

代码语言:javascript
复制
@client.command()
async def dmall(ctx, message):
    for m in client.get_all_members():
        try:
           await m.send(message)
        except:
           print("couldn't send message to "+m)
    await ctx.send("Done!")

这可能是不一致方面的错误,或者可能是你的机器人在服务器上的权限有问题。

票数 0
EN

Stack Overflow用户

发布于 2020-10-01 22:13:39

实际上,我把代码放在我的代码文件夹里一段时间了,代码将是

代码语言:javascript
复制
@client.command()
async def dmall(ctx, *, message):
    guild = ctx.guild
    for m in guild.members:
        try:
            await m.send(message)
            await ctx.send(f"[Success] Successfully sent to {m}.")

        except Exception as e:
            await ctx.send(f"[Failure] Failed to send to {m}\n\n\n{e}")

    await ctx.send(f"[Done] Finnished!")

注意:await ctx.send(f"[Done] Finnished!")不是一个拼写错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64081984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档