如何在与python不一致的自动用户超时后删除机器人和用户的消息?

问题描述 投票:0回答:1

如果用户过快地向机器人发送垃圾命令,我会触发一个触发器,让用户超时 30 分钟。我还想让机器人删除其命令响应,即帮助命令的输出,以及用户垃圾邮件!帮助消息。这个想法是当有人发送垃圾邮件时自动保持聊天畅通无阻。我可以让机器人告诉我该方法在日志中开始和结束,但它实际上从未删除消息。

有人可以提供一些指导吗?在网上找不到太多东西,我已经尝试了我能找到的大部分文档以及遇到类似问题的人。

    async def timeout_user(self, user):
        guild = user.guild

        # Get the timeout role
        timeout_role = discord.utils.get(guild.roles, name="Timeout")

        # Add the timeout role to the user
        await user.add_roles(timeout_role)

        # Remove the spam commands from the user
        await self.remove_spam_commands(user)

        # Send a message to the user
        timeout_message = "removed because inappropriate outside testing "
        await user.send(timeout_message)
        # Send a message in the channel
        timeout_channel = discord.utils.get(guild.channels, name="testing")
        if timeout_channel:
            await timeout_channel.send(f"{user.mention}, removed because inappropriate outside testing")

        # Remove the timeout role after the specified duration
        await asyncio.sleep(1800)  # 30 minutes
        await user.remove_roles(timeout_role)


    async def remove_spam_commands(self, user):
        print("Removing spam commands...")  # Debug print statement

        async for message in user.history(limit=15):
            if message.content.startswith('!'):
                await message.delete()
                print(f"Deleted message from user {user.id}: {message.content}")

        print("Finished removing spam commands.")  # Debug print statement

    @commands.Cog.listener()
    async def on_message(self, message):
        print("on_message event triggered!")
        print(f"Received message from {message.author}: {message.content}")

        if message.author.bot:
            return

        # Check if the message is from the user you want to timeout
        user_id = message.author.id
        if user_id == QUARTERMASTER_ID:
            await message.channel.send("This is a test message from the bot.")
            await message.delete()
            print(f"Deleted message from Quartermaster: {message.content}")

        else:
            print(f"Message is not from the Quartermaster.")

        # Update command usage for the user
        user_id = message.author.id
        await self.check_command_limit(message.author)
        self.command_limits[user_id]['count'] += 1

日志:

Removing spam commands...
Finished removing spam commands.
(Removed output of message due to sensitivity of text)
on_message event triggered!
(Removed output of message due to sensitivity of text)
on_message event triggered!

python-3.x discord.py bots
1个回答
0
投票

如果您使用

context.reply()
message.reply()
等命令消息进行响应,一种想法是查看机器人的消息历史记录并删除响应用户的消息:

async def remove_spam_commands(self, user):
    print("Removing spam commands...")  # Debug print statement
    user_message_ids = []
    async for message in user.history(limit=15):
        if message.content.startswith('!'):
            await message.delete()

            # save the id of user messages that were deleted
            user_message_ids.append(message.id)

            print(f"Deleted message from user {user.id}: {message.content}")
    async for message in user.guild.me.history(limit=100):
        # You need to set a higher limit here, as the bot send more messages
        if message.reference and message.reference.message_id in user_message_ids:
            await message.delete()

    print("Finished removing spam commands.")  # Debug print statement
© www.soinside.com 2019 - 2024. All rights reserved.