Explorar o código

Added a cog, which changes the users nickname to the badgenumber and char name. if multiple chars with badge numbers apper, you have to change it manually

BaumSplitter41 hai 2 meses
pai
achega
b5cdcb399a
Modificáronse 3 ficheiros con 161 adicións e 1 borrados
  1. 156 0
      VPD_BOT/cogs/change_name_badge.py
  2. 2 1
      VPD_BOT/config.cfg
  3. 3 0
      VPD_BOT/main.py

+ 156 - 0
VPD_BOT/cogs/change_name_badge.py

@@ -0,0 +1,156 @@
+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: to use this script on a other server you need to change the SQL querys. It is deactivatable in the config.cfg file.
+
+
+class changedcname(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(minutes=15)
+    async def check_inactive_members(self):
+        config = self._load_config()
+        enable_change_dc_name = config.getboolean("Role Management","nable_change_dc_name")
+        if not enable_change_dc_name:
+            return # Exit the function if the feature is disabled in the config
+
+
+    #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
+
+
+        #needed arrays
+        badgenr = []
+        charinfo = []
+        discord_raw = []
+        user_id = []
+        users = []
+        firstname = []
+        lastname = []
+        
+        #get information from database
+        cursor.execute("""
+        SELECT ny_groups_meta.internal_identifier FROM ny_groups_meta, 
+        users, players WHERE ny_groups_meta.character_identifier=players.citizenid AND 
+        players.userId=users.userId
+        """)
+        for internal_identifier in cursor.fetchall():
+            badgenr.append((internal_identifier))
+        cursor.execute("""
+        SELECT players.charinfo FROM ny_groups_meta, 
+        users, players WHERE ny_groups_meta.character_identifier=players.citizenid AND 
+        players.userId=users.userId
+        """)
+        for charinfo in cursor.fetchall():
+            charinfo.append((charinfo))
+        cursor.execute("""
+        SELECT users.discord FROM ny_groups_meta, 
+        users, players WHERE ny_groups_meta.character_identifier=players.citizenid AND 
+        players.userId=users.userId
+        """)
+        for discord in cursor.fetchall():
+            discord_raw.append((discord))
+        
+
+        #get users to the discordIDs
+        for discord in discord_raw:
+            discord_id = discord_raw.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)
+                users.append(user)
+
+        #check on dublicates
+        for i in range(len(users)):
+            for j in range(i + 1, len(users)):
+                if users[i] == users[j]:
+                    print(f"Duplicate user found: {users[i].name} (ID: {users[i].id})")
+                    users.pop(j)
+                    charinfo.pop(j)
+                    badgenr.remove(badgenr[j])
+                    users.pop(i) 
+                    
+
+
+        #get charname
+        for charinfo in charinfo:
+            charinfo_split = charinfo.split(",")
+            for i in range(len(charinfo_split)):
+                if '"firstname"' in charinfo_split[i]:
+                    firstname.append(charinfo_split[i+1])
+                if '"lastname"' in charinfo_split[i]:
+                    lastname.append(charinfo_split[i+1])
+
+
+        #change username
+        for i in range(len(users)):
+            nick = f"[{badgenr[i]}] {firstname[i]} {lastname[i]}"
+            try:
+                await users[i].edit(nick=nick)
+            except Exception as e:
+                print(f"Failed to change nickname for {users[i].name}: {e}")
+
+      
+        cursor.close()
+        conn.close()
+
+
+
+def setup(bot: discord.Bot):
+    bot.add_cog(changedcname(bot))
+                            
+
+
+            
+        
+            
+
+    
+        
+    
+        
+    

+ 2 - 1
VPD_BOT/config.cfg

@@ -31,7 +31,7 @@ department2_role_id =
 department2_deputy_id =
 department2_unit_role_id =
 
-
+#Role IDs for the rolemanagment commands
 [Role Management]
 department1_command = 1451668309648084992
 department1_ranks = 1459935618371948743, 1459935662102020279, 1459935773108338921, 1459935923608223766, 1459945390571782245, 1459945659548565634, 1459936064612466861, 1459936186922700912, 1459936215766667448, 1459936269168541736, 1459936307974373427 
@@ -45,6 +45,7 @@ department2_units =
 department2_head_unit =
 
 remove_access_role_on_fire = true
+enable_change_dc_name = true
 
 
 [Moderation]

+ 3 - 0
VPD_BOT/main.py

@@ -167,6 +167,9 @@ class Admin(commands.Cog):
 #Print in Log if error occurs
 @bot.event
 async def on_application_command_error(ctx, error):
+    print(f"[!] Error in command {ctx.command}: {error}")
+    if ctx.guild is None:
+        return
     channel = discord.utils.get(ctx.guild.channels, id=int(channel_status_log))
     if channel:
         await channel.send(f"Error occurred: {str(error)}")