Prechádzať zdrojové kódy

Added a funktion that sends a dm to users, who have been inaktive on the server (gameserver) for two weeks.

baumsplitter41 2 mesiacov pred
rodič
commit
df151fd492

+ 105 - 0
VPD_BOT/cogs/inaktive_remider_dm.py

@@ -0,0 +1,105 @@
+import os
+from dotenv import load_dotenv
+import discord
+from discord.ext import commands, tasks
+from discord.commands import Option
+from discord.commands import slash_command
+import configparser
+import time
+import mysql.connector
+
+
+## Note: this script will be pretty hardcoded and specific for our use case. It is deactivatable in the config.cfg file.
+
+
+class remiderinactive(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
+
+        
+    @commands.Cog.listener()
+    async def on_ready(self):
+        self.check_inactive_members.start()
+    
+    @tasks.loop(hours=48)
+    async def check_inactive_members(self):
+        config = self._load_config()
+        enable_inavtive_reminder_dm = config.getboolean("Welcome","enable_inavtive_reminder_dm")
+        if not enable_inavtive_reminder_dm:
+            return # Exit the function if the feature is disabled in the config
+
+        log_channel_id = config.getint("Welcome","log_channel_id")
+        log_channel = self.bot.get_channel(log_channel_id)
+    
+    #Load .env file for the gameserver database
+        dbhost = os.getenv("HOST2")
+        if dbhost is None:
+            raise ValueError("HOST2 not found in .env file")
+        dbname = os.getenv("NAME2")
+        if dbname is None:
+            raise ValueError("NAME2 not found in .env file")
+        dbpsswd = os.getenv("PASSWORD2")
+        if dbpsswd is None:
+            raise ValueError("PASSWORD2 not found in .env file")
+        dbdb = os.getenv("DATABASE2")
+        if dbdb is None:
+            raise ValueError("DATABASE2 not found in .env file")
+        
+
+        #Database initialization
+        conn = mysql.connector.connect(
+            host=dbhost,
+            user=dbname,
+            password=dbpsswd,
+            charset='utf8mb4',
+            collation='utf8mb4_unicode_ci'
+        )
+        cursor = conn.cursor()
+        conn.database = dbdb
+
+        inaktive_players = []
+        
+        cursor.execute("""
+        SELECT license, discord FROM users WHERE license in (SELECT license FROM players WHERE last_logged_out < CURDATE() - INTERVAL 14 DAY)
+        """)
+        for license, discord in cursor.fetchall():
+            inaktive_players.append((discord))
+
+
+        for discord in inaktive_players:
+            discord_raw = discord.split(":")
+            for i in range(len(discord_raw)):
+                if discord_raw[i].isdigit():
+                    user_id = int(discord_raw[i])
+                    user = self.bot.get_user(user_id)
+                    if user is not None:
+                        try:
+                            await user.send("Hey! We noticed that you haven't been active in the server for a while. If you want to stay in the server, please log in to the game at least once every 14 days. If you have any questions, feel free to reach out to the moderators.")
+                            await log_channel.send(f"Sent inactivity reminder DM to {user.name} ({user.id})")
+                        except Exception as e:
+                            print(f"Could not send DM to {user.name}: {e}")
+                            await log_channel.send(f"Could not send DM to {user.name} ({user.id}): {e}")
+                    else:
+                        print(f"Could not find user with ID {user_id}")
+                        await log_channel.send(f"Could not find user with ID {user_id}")
+        
+        cursor.close()
+        conn.close()
+                            
+
+
+            
+        
+            
+
+    
+        
+    
+        
+    

+ 2 - 2
VPD_BOT/cogs/welcome_msg.py

@@ -19,11 +19,11 @@ class welcome_msg(commands.Cog):
     @commands.Cog.listener()
     async def on_member_join(self, member: discord.Member):
         config = self._load_config()
-        enable_welcome_message = config.getboolean("Welcome Message","enable_welcome_message")
+        enable_welcome_message = config.getboolean("Welcome","enable_welcome_message")
         if not enable_welcome_message:
             return
         
-        welcome_channel_id = config.getint("Welcome Message","welcome_channel_id")
+        welcome_channel_id = config.getint("Welcome","welcome_channel_id")
         welcome_channel = self.bot.get_channel(welcome_channel_id)
         if welcome_channel is None:
             print(f"Welcome channel with ID {welcome_channel_id} not found.")

+ 11 - 2
VPD_BOT/config.cfg

@@ -3,6 +3,7 @@
 label_rules = Verifizieren
 rules_role = 1442528544394575892
 
+
 # Channel IDs for the logs
 [Logs]
 status_log = 1448305623287464050
@@ -10,10 +11,12 @@ mod_log = 1447580463668400305
 action_log = 1466410535271530735
 enable_action_log = true
 
+
 #Role IDs for the Teamroles
 [Team Roles]
 team_role_id = 1447295632774660158
 
+
 # Role IDs for the Departments (/einweisung)
 [Einweisung]
 acces_role_id = 1449145241813647380
@@ -28,6 +31,7 @@ department2_role_id =
 department2_deputy_id =
 department2_unit_role_id =
 
+
 [Role Management]
 department1_command = 1451668309648084992
 department1_ranks = 1459935618371948743, 1459935662102020279, 1459935773108338921, 1459935923608223766, 1459945390571782245, 1459945659548565634, 1459936064612466861, 1459936186922700912, 1459936215766667448, 1459936269168541736, 1459936307974373427 
@@ -42,6 +46,7 @@ department2_head_unit =
 
 remove_access_role_on_fire = true
 
+
 [Moderation]
 Mod_role_IDs = 1447295632774660158
 Blacklist_Words = true
@@ -49,6 +54,10 @@ 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
 Spam_Sensitivity_Index = 5  #Number of Messages within 10 seconds considered as spam
 
-[Welcome Message]
+
+# Configuration of the welcome message system
+[Welcome]
 welcome_channel_id = 1442132329006104630
-enable_welcome_message = true
+enable_welcome_message = true
+
+enable_inavtive_reminder_dm = true

+ 1 - 1
VPD_BOT/main.py

@@ -445,7 +445,7 @@ async def warn(
     embed_dm.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
 
 
-    try:
+    try: 
         await user.send(embed=embed_dm)
     except discord.Forbidden:
         await ctx.respond("Error: I can't send a DM to this user. The user was warned without a information.", ephemeral=True)