inaktive_remider_dm.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 DISTINCT users.license, users.discord FROM users, players WHERE players.last_updated < CURDATE() - INTERVAL 14 DAY;
  56. """)
  57. for license, discord in cursor.fetchall():
  58. inaktive_players.append(discord)
  59. #Core script
  60. discord_ids = []
  61. for player in inaktive_players:
  62. if player is not None:
  63. discord_raw = player.split(":")
  64. for discord_part in discord_raw:
  65. if discord_part.isdigit():
  66. discord_ids.append(discord_part)
  67. for discord_id in discord_ids:
  68. user_id = int(discord_id)
  69. user = self.bot.get_user(user_id)
  70. if user is not None:
  71. try:
  72. 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! 🚔")
  73. await log_channel.send(f"Sent inactivity reminder DM to {user.name} ({user.id})")
  74. except Exception as e:
  75. print(f"Could not send DM to {user.name}: {e}")
  76. await log_channel.send(f"Could not send DM to {user.name} ({user.id}): {e}")
  77. else:
  78. print(f"Could not find user with ID {user_id}")
  79. await log_channel.send(f"Could not find user with ID {user_id}")
  80. cursor.close()
  81. conn.close()
  82. def setup(bot: discord.Bot):
  83. bot.add_cog(remiderinactive(bot))