Discord.py 从消息链接获取消息对象

2023-11-29

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

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]))

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


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

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

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

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

不和谐.pyget_guild()参考:https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.get_guild

不和谐.pyget_channel()参考:https://discordpy.readthedocs.io/en/latest/api.html#discord.Guild.get_channel

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Discord.py 从消息链接获取消息对象 的相关文章

随机推荐