【问题标题】:Delete all messages that aren't by certain author in a discord channel删除不和谐频道中不是某个作者的所有消息
【发布时间】:2020-10-29 03:16:54
【问题描述】:

我刚刚完成了一个purge 命令,该命令使用 cog 在启用时不断删除频道中的消息,而不是在禁用时(切换命令)。我现在还想再添加一件事,那就是只清除不是我的消息。

我知道这可以通过一个简单的 if 语句来完成,但我无法从 discord.py API 中找出我需要使用的内容。我一直在尝试使用message.author.id != "id",以及它的其他变体,但它似乎不起作用。顺便说一句,我在我的 cogs.py 中做这一切。我查看了其他有关使用 API 中的日志内容的堆栈溢出文章以及删除带有某些关键字的消息的示例,但没有一篇文章准确地演示了 message.author 内容的使用以及它是字符串还是整数,或者事实上,如果我应该使用其他东西。

这是我现在的机器人代码,我的目标是任何尝试:

    @commands.Cog.listener()
async def on_message(self, message):
    try:
        if self.channels[message.channel]:
            await message.channel.purge(limit=999)
    except:
        pass

我试过这样的东西:

    @commands.Cog.listener()
async def on_message(self, message):
    try:
        if message.author != 396025519398977548 and self[message.channel]:
            await message.delete(message.channel)
    except:
        pass

但我错过了一些东西,哈哈。对 python 还是有点陌生​​,但我不妨通过观看示例等来学习。这种学习方式很简单,但显然我正在准备一本关于 python 的书来阅读。

但我离题了,有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    您可以查看作者姓名或 ID。你错了 message.author.id 是一个整数而不是一个字符串。

    • 作者姓名:

    例如,如果您的不和谐标签是 Yolo#5236,则必须检查 message.author == 'Yolo'

    @commands.Cog.listener()
    async def on_message(self, message):
        try:
            if self.channels[message.channel] and message.author == 'Your discord name':
                await message.delete()
        except:
            pass
    
    • 带有作者ID

    例如,如果您的 ID 是 48864468684,则必须检查 message.author.id == 48864468684

    @commands.Cog.listener()
    async def on_message(self, message):
        try:
            if self.channels[message.channel] and message.author.id == id:
                await message.delete()
        except:
            pass
    

    【讨论】:

    • 我实际上在这篇文章之后将其更改为整数,但我不记得我是否尝试过哈哈。会从 API 中知道一些东西来删除用户的消息,而不是清除所有用户的所有消息吗?我只能从文档中找到像 fetch users 一样的东西。
    • message.delete() 将删除该消息。
    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2020-12-23
    • 2018-11-20
    • 1970-01-01
    • 2020-10-03
    • 2019-03-02
    相关资源
    最近更新 更多