我怎样才能得到这个命令来检查全局发送的消息,而不仅仅是在一个频道中

How can I get this command to check for messages sent globally not only in one channel

多年来我一直试图让这个命令来检查用户在整个服务器中发送的所有消息。我想出了一个命令,只检查 1 个频道中用户发送的消息。

@client.command(aliases=["m"])
async def messages(ctx, user: discord.Member):
    channel = ctx.message.channel
    counter = 0
    async for message in channel.history():
        if message.author == user:
            counter += 1

请注意,无论您如何操作,如果您使用的是整个服务器历史记录,机器人将需要一些时间来完成所有操作。知道这一点后,如果您仍希望继续进行此操作,这里有一个解决方案。

@client.command(aliases=["m"])
async def messages(ctx, user: discord.Member):
    counter = 0
    for channel in guild.channels:
        if isinstance(channel, discord.TextChannel):
            async for message in channel.history(limit=None):
                if message.author.id == user.id:
                    counter += 1

另请注意,我比较的是 ID 而不是成员对象本身,这是因为该对象 可以 更改,这只是防止了。