【问题标题】:Discord.py v1.0 How to retrieve every message from a channel and delete them one by oneDiscord.py v1.0 如何从频道中检索每条消息并一一删除
【发布时间】:2020-04-06 23:26:25
【问题描述】:

我有一个clear 命令设置,我的想法是删除频道中我硬编码的名称的每条消息,但因为我读到该方法只删除 14 天后的消息,所以我想我会必须以某种方式手动检索所有消息,然后使用 delete 方法删除每一条消息。 我环顾四周,但每个示例要么来自 v1.0 之前,要么不像我那样使用带有上下文的命令。

代码:

@self.discord_bot.command()
async def clear(ctx):
    try:
        if ctx.channel != self.channel_name:
            return

        # clear history
        # retrieve messages using context ctx
        # for each loop that deletes them with self.discord_bot.delete()
    except Exception as e:
        await ctx.trigger_typing()
        await ctx.send("Oops something happened! %s" % str(e))
        return

提前致谢!

【问题讨论】:

    标签: python-3.x bots discord.py


    【解决方案1】:

    这应该可以使用channel.purge()。如果 bot 帐户有足够的权限,这将删除指定频道中的每条消息。

    @self.discord_bot.command()
    async def clear(ctx):
        try:
            if ctx.channel != self.channel_name:
                return
    
            # clear history
            await ctx.channel.purge(limit=None)
        except Exception as e:
            await ctx.trigger_typing()
            await ctx.send("Oops something happened! %s" % str(e))
            return
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多