How to get how many messages has been sent in a channel

六月ゝ 毕业季﹏ 提交于 2021-02-11 08:57:47

问题


I am trying to get the number of how many messages have been sent in a channel, and using the logs_from() function wont work because that only accepts a fixed amount of messages to retrieve, how do I do this?


回答1:


In the discord.py-rewrite branch, there is a TextChannel.history AsyncIterator. If you pass limit=None, it will return all the messages from the channel

@bot.command()
async def message_count(ctx, channel: discord.TextChannel=None):
    channel = channel or ctx.channel
    count = 0
    async for _ in channel.history(limit=None):
        count += 1
    await ctx.send("There were {} messages in {}".format(count, channel.mention))

You might try passing limit=None to logs_from, but it isn't documented as working that way like it is in the rewrite branch.



来源:https://stackoverflow.com/questions/52214425/how-to-get-how-many-messages-has-been-sent-in-a-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!