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

时间:2020-05-17 11:39:49

标签: python url-rewriting discord.py message

我想知道如何通过消息ID获得消息。我尝试过discord.fetch_message(id)discord.get_message(id),但都提高了:

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

1 个答案:

答案 0 :(得分:2)

获取消息时,您将需要一个abc.Messageable对象-本质上是一个可以在其中发送消息的对象,例如文本通道,DM等。

示例:

@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

参考: