inaktive_remider_dm.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os
  2. from dotenv import load_dotenv
  3. import discord
  4. from discord.ext import commands, tasks
  5. from discord.commands import Option
  6. from discord.commands import slash_command
  7. import configparser
  8. import time
  9. import mysql.connector
  10. ## Note: this script will be pretty hardcoded and specific for our use case. It is deactivatable in the config.cfg file.
  11. class remiderinactive(commands.Cog):
  12. def __init__(self, bot: discord.Bot):
  13. self.bot = bot
  14. def _load_config(self):
  15. config = configparser.ConfigParser()
  16. configFilePath = r'config.cfg'
  17. config.read(configFilePath)
  18. return config
  19. @commands.Cog.listener()
  20. async def on_ready(self):
  21. self.check_inactive_members.start()
  22. @tasks.loop(hours=48)
  23. async def check_inactive_members(self):
  24. config = self._load_config()
  25. enable_inavtive_reminder_dm = config.getboolean("Welcome","enable_inavtive_reminder_dm")
  26. if not enable_inavtive_reminder_dm:
  27. return # Exit the function if the feature is disabled in the config
  28. log_channel_id = config.getint("Logs","action_log")
  29. log_channel = self.bot.get_channel(log_channel_id)
  30. #Load .env file for the gameserver database
  31. dbhost = os.getenv("HOST2")
  32. if dbhost is None:
  33. raise ValueError("HOST2 not found in .env file")
  34. dbname = os.getenv("NAME2")
  35. if dbname is None:
  36. raise ValueError("NAME2 not found in .env file")
  37. dbpsswd = os.getenv("PASSWORD2")
  38. if dbpsswd is None:
  39. raise ValueError("PASSWORD2 not found in .env file")
  40. dbdb = os.getenv("DATABASE2")
  41. if dbdb is None:
  42. raise ValueError("DATABASE2 not found in .env file")
  43. #Database initialization
  44. conn = mysql.connector.connect(
  45. host=dbhost,
  46. user=dbname,
  47. password=dbpsswd,
  48. charset='utf8mb4',
  49. collation='utf8mb4_unicode_ci'
  50. )
  51. cursor = conn.cursor()
  52. conn.database = dbdb
  53. inaktive_players = []
  54. cursor.execute("""
  55. SELECT license, discord FROM users WHERE license in (SELECT license FROM players WHERE last_logged_out < CURDATE() - INTERVAL 14 DAY)
  56. """)
  57. for license, discord in cursor.fetchall():
  58. inaktive_players.append((discord))
  59. for discord in inaktive_players:
  60. discord_raw = discord.split(":")
  61. for i in range(len(discord_raw)):
  62. if discord_raw[i].isdigit():
  63. user_id = int(discord_raw[i])
  64. user = self.bot.get_user(user_id)
  65. if user is not None:
  66. try:
  67. await user.send("Hey, du warst eine ganze Weile außer Dienst – höchste Zeit, wieder einzusteigen und den Bürgern von Los Santos auf den Straßen zu helfen! 🚔")
  68. await log_channel.send(f"Sent inactivity reminder DM to {user.name} ({user.id})")
  69. except Exception as e:
  70. print(f"Could not send DM to {user.name}: {e}")
  71. await log_channel.send(f"Could not send DM to {user.name} ({user.id}): {e}")
  72. else:
  73. print(f"Could not find user with ID {user_id}")
  74. await log_channel.send(f"Could not find user with ID {user_id}")
  75. cursor.close()
  76. conn.close()
  77. def setup(bot: discord.Bot):
  78. bot.add_cog(remiderinactive(bot))