【问题标题】:discord.py get message object from message linkdiscord.py 从消息链接中获取消息对象
【发布时间】:2020-07-27 09:49:19
【问题描述】:

我想知道是否有一种方法可以使用 Python Discord 机器人从消息链接 (https://discordapp.com/channels/guild_id/channel_id/message_id) 获取消息,如果可以,如何。 我可以想办法使用

link = 'https://discordapp.com/channels/guild_id/channel_id/message_id'.split('/')
message = await self.bot.get_guild(int(link[-3])).get_channel(int(link[-2])).fetch_message(int(link[-1]))

但我想知道是否有更直接的方法。 提前致谢!

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    为此,您应该首先通过以下方式获取公会、频道和消息 ID:

    server_id = int(link[4])
    channel_id = int(link[6])
    msg_id = int(link[5])
    

    接下来你应该使用 bot 获取 guild 对象,然后使用 guild 对象获取文本通道对象,最后使用文本通道对象获取消息对象。像这样:

    server = client.get_guild(server_id)
    channel = server.get_channel(channel_id)
    message = await channel.fetch_message(msg_id)
    

    discord.py get_guild() 参考:https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.get_guild

    discord.py get_channel() 参考:https://discordpy.readthedocs.io/en/latest/api.html#discord.Guild.get_channel

    discord.py fetch_message() 参考:https://discordpy.readthedocs.io/en/latest/api.html#discord.abc.Messageable.fetch_message

    【讨论】:

    • 这就是我在问题示例中的做法(只需一行,您甚至可以删除 get_guild() 部分)。我想知道是否有只使用链接的直接方法(类似于 get_message_by_link(link))。仍然感谢您的回答。
    猜你喜欢
    • 2021-07-12
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多