【问题标题】:discord.py questions about channels messagesdiscord.py 关于频道消息的问题
【发布时间】:2021-09-08 09:17:23
【问题描述】:

我想知道我是否可以通过 ID(一般、公告、赠品等)一次向更多频道发送消息
这是我现在的错误,但我无法弄清楚为什么不起作用。

ERROR: Ignoring exception in on_ready
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 17, in on_ready
    member = await bot.fetch_user(i)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 1384, in fetch_user
    data = await self.http.get_user(user_id)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 250, in request
    raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10013): Unknown User

代码是in the pastebin:

【问题讨论】:

  • 请将代码中的代码放在 codebloc 中,而不是在 pastebin 上。 Here's how to do it.
  • 我给了你一个应该有效的答案,检查一下。如果出了什么问题,请在 cmets 中询问我。
  • @Kyrela 我可以将代码放在代码块中,因为它给了我一个格式不正确的错误。
  • @FLAK-ZOSO 如果我想通过 id 对一些用户进行 dm,我可以这样做吗?
  • @seek,我已经回答了你的问题。如果它有效,你可以接受它。

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


【解决方案1】:

抓取


问题是您在第 17 行使用了fetch_user() 而不是fetch_channel


您应该尝试使用此代码:

channel = await bot.fetch_channel(i)
await channel.send("...")

您的错误是在您的 .json 文件中有一些 ID,它们是频道的 ID。
将频道 ID 与会员 ID 一样使用显然会引发错误。
在这种情况下,您的错误是说 ID 为fetch_user() 的成员不存在。

建议


您应该使用异常处理从文件中删除不存在的通道。
例如:

with open(r"C:\\Your\Path\to\file.json", 'r+') as File:
    data = json.load(File)
for ID in data:
    try:
        channel = bot.fetch_channel(ID)
    except discord.errors.NotFound: # Raised if ID doesn't exist anymore
        data.remove(ID)
        os.remove(r"C:\\Your\Path\to\file.json")
        with open(r"C:\\Your\Path\to\file.json", 'w') as File:
            json.dump(data, File)

DM 用户


如果您想通过他们的 ID 向某些用户发送消息,您只需这样做:

with open(r"C:\\IDs.json", 'r') as File:
    data = json.load(File)
for ID in data:
    user = await bot.fetch_user(ID)
    await user.send("Your message")

请记住,这可能会引发异常,因为某些用户不允许机器人向他们发送消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2021-05-03
    • 2021-05-17
    • 1970-01-01
    • 2021-02-17
    • 2021-05-06
    • 2021-07-30
    相关资源
    最近更新 更多