【问题标题】:How to grab a message object from it's id discord.py如何从它的 id discord.py 中获取消息对象
【发布时间】:2021-09-21 19:51:53
【问题描述】:

所以我正在尝试构建一个不和谐的机器人,我想要的功能之一是引用以前的消息并从中提取数据。我已经存储了我想引用的消息 ID,但我似乎找不到如何从它的 ID 中提取消息对象。

if split[0] == 'ref':
      if (len(split) < 2):
        await message.channel.send("!ref,poll or sg,id number")
      if (len(split) == 3):
        if split[1] == "poll":
          for x in db["poll"]:
            if x[1] == message.guild.id and x[0] == split[2]:
              original = await discord.utils.get(message.channel, message_id=int(split[2]))
              await message.channel.send(original.content)

我一直对原始变量使用错误的语法,但我无法弄清楚

我尝试过使用

original = await message.channel.fetch_message(int(split[2]))

得到了

Bot is online as *******
Ignoring exception in on_message
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 277, in on_message
    original = await message.channel.fetch_message(int(split[2]))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 1132, in fetch_message
    data = await self._state.http.get_message(channel.id, 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: 10008): Unknown Message

我也有我想从中提取的频道的 ID

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您可以直接获取消息,您只需要消息所在的message-idchannel

    您可以通过这种方式在message.channel 中获取“original”消息:

    original = await message.channel.fetch_message(int(split[2]))
    

    如果您没有频道对象而只有频道 ID,那么您只需要 bot.get_channel。看起来像这样:

    channel = bot.get_channel(mychannelid)
    

    来源

    【讨论】:

    • 如果我想从未知渠道获取它怎么办?
    • 或者如果我有频道 ID 但我想从另一个频道引用它
    • 我编辑了我的答案。
    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2021-03-27
    • 2021-07-12
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多