简体   繁体   English

删除 discord 频道中不是某个作者的所有消息

[英]Delete all messages that aren't by certain author in a discord channel

I just finished making a purge command that uses a cog to constantly delete messages in a channel when enabled, and not when it is disabled (toggle command).我刚刚完成了一个purge命令,该命令使用 cog 在启用时不断删除通道中的消息,而不是在禁用时(切换命令)。 I now want to add one more thing too it, which is it to only purge the messages that aren't by me.我现在还想再添加一件事,那就是只清除不是我的消息。

I know this can be done with a simple if statement, but I can't find out what from the discord.py API I need to use.我知道这可以通过一个简单的 if 语句来完成,但我无法从 discord.py API 中找出我需要使用的内容。 I have been trying to use message.author.id != "id" , and other variations of that, but it doesn't seem to work.我一直在尝试使用message.author.id != "id"和其他变体,但它似乎不起作用。 I am doing this all in my cogs.py and by the way.顺便说一句,我在我的 cogs.py 中做这一切。 I have looked at other stack overflow articles about using a log thing from the API and examples of deleting messages with certain keywords in them, but none of them demonstrate exactly the use of the message.author stuff and if it's a string or integer, or in fact if I should be using something else.我查看了其他有关使用 API 中的日志内容的堆栈溢出文章以及删除带有某些关键字的消息的示例,但没有一个确切地说明了message.author内容的使用以及它是字符串还是 integer,或者事实上,如果我应该使用别的东西。

Here's my code for the bot right now with any attempts at my goal:这是我现在的机器人代码,任何尝试都可以达到我的目标:

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

I have tried stuff like this:我试过这样的东西:

    @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

But I am missing something, haha.但是我错过了一些东西,哈哈。 Still a bit new to python, but I might as well learn along the way by watching from examples and such.对 python 来说还是有点新意,但我不妨通过观看示例等来学习。 This way of learning is easy, but obviously I am getting a book on python to read.这种学习方式很容易,但显然我正在阅读一本关于 python 的书。

But I digress, any thoughts?但我离题了,有什么想法吗?

You can check the author name or id.您可以查看作者姓名或 ID。 What you got wrong is that message.author.id is an integer and not a string.你错了 message.author.id 是 integer 而不是字符串。

  • With the author's name:附作者姓名:

For instance, if your discord tag is Yolo#5236, you'll have to check message.author == 'Yolo'例如,如果您的 discord 标签是 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
  • With the author's ID有作者身份证

For instance, if your ID is 48864468684, you'll have to check message.author.id == 48864468684例如,如果您的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM