Эх сурвалжийг харах

added a function with deletes all messages with are older than 48h, the channel ids and the time is editable in the cfg

BaumSplitter41 3 долоо хоног өмнө
parent
commit
67fd9191d8

+ 48 - 0
VPD_BOT/cogs/auto_delete_msg.py

@@ -0,0 +1,48 @@
+import discord
+from discord.ext import commands
+from discord.commands import Option
+from discord.commands import slash_command
+import configparser
+import time
+import asyncio
+
+
+class autodelmsg(commands.Cog):
+    def __init__(self, bot: discord.Bot):
+        self.bot = bot
+    
+    def _load_config(self):
+        config = configparser.ConfigParser()
+        configFilePath = r'config.cfg'
+        config.read(configFilePath)
+        return config
+    def _get_info_channel(self):
+        config = self._load_config()
+        channel_ids = [int(channel_id.strip()) for channel_id in config.get("Moderation")("autodelete_channel_id").split(",")]
+        channels = [self.bot.get_channel(channel_id) for channel_id in channel_ids]
+        if any(channel is None for channel in channels):
+            print(f"One or more roles with IDs {channel_ids} not found.")
+            return
+        return channels
+    def _get_message_age_limit(self):
+        config = self._load_config()
+        return config.getint("Moderation", "autodelete_message_age")
+    
+
+
+    @commands.Cog.listener()
+    async def on_ready(self):
+        channels = self._get_info_channel()
+        if channels is None:
+            return
+        message_age_limit = self._get_message_age_limit()
+        while True:
+            for channel in channels:
+                async for message in channel.history(limit=None):
+                    if (time.time() - message.created_at.timestamp()) > (message_age_limit * 3600):
+                        try:
+                            await message.delete()
+                            #print(f"Deleted message from {message.author} in {channel.name} due to age.")
+                        except Exception as e:
+                            print(f"Failed to delete message: {e}")
+            await asyncio.sleep(3600)

+ 3 - 0
VPD_BOT/config.cfg

@@ -67,6 +67,9 @@ Antispam = true
 Blacklist_Words_List = buy now, free money, click here, visit this site, subscribe, follow me, check this out, discord.gg//, join my server, free nitro, free discord nitro
 Blacklist_Words_List = buy now, free money, click here, visit this site, subscribe, follow me, check this out, discord.gg//, join my server, free nitro, free discord nitro
 Spam_Sensitivity_Index = 5  #Number of Messages within 10 seconds considered as spam
 Spam_Sensitivity_Index = 5  #Number of Messages within 10 seconds considered as spam
 
 
+autodelete_channel_id = 1442286402535686274, 1442286556047212668, 1479480527605207184, 1442519953856008313, 1447333589040763061, 1442286712658333876, 1442286762536734831, 1442286814059696270, 1447332777786605618
+autodelete_message_age = 48  #Age of messages in hours to be autodeleted | For disabling remove channel IDs.
+
 
 
 # Configuration of the welcome message system
 # Configuration of the welcome message system
 [Welcome]
 [Welcome]