如何通过id获取消息discord.py

2024-05-01

我想知道如何通过消息 ID 获取消息。我努力了discord.fetch_message(id) and discord.get_message(id),但两者都会提出:

Command raised an exception: AttributeError: module 'discord' has no attribute 'fetch_message'/'get_message'

收到消息时,您需要abc.Messageable对象 - 本质上是一个可以发送消息的对象,例如文本通道、DM 等。

Example:

@bot.command()
async def getmsg(ctx, msgID: int): # yes, you can do msg: discord.Message
                                   # but for the purposes of this, i'm using an int

    msg = await ctx.fetch_message(msgID) # you now have the message object from the id
                                         # ctx.fetch_message gets it from the channel
                                         # the command was executed in


###################################################

@bot.command()
async def getmsg(ctx, channel: discord.TextChannel, member: discord.Member):
    msg = discord.utils.get(await channel.history(limit=100).flatten(), author=member)
    # this gets the most recent message from a specified member in the past 100 messages
    # in a certain text channel - just an idea of how to use its versatility

参考:

  • abc.Messageable https://discordpy.readthedocs.io/en/latest/api.html#discord.abc.Messageable
  • TextChannel.history() https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.history
  • TextChannel.fetch_message() https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.fetch_message
  • Context.fetch_message() https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Context.fetch_message
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过id获取消息discord.py 的相关文章

随机推荐