modinfo.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import discord
  2. from discord.ext import commands
  3. from discord.commands import Option
  4. from discord.commands import slash_command
  5. import os
  6. from dotenv import load_dotenv
  7. import mysql.connector
  8. import configparser
  9. #Bot initialization
  10. class modinfo(commands.Cog):
  11. def __init__(self, bot: discord.Bot):
  12. self.bot = bot
  13. #---------------------------------#
  14. #ENV initialization
  15. load_dotenv()
  16. dbhost = os.getenv("HOST")
  17. if dbhost is None:
  18. raise ValueError("HOST not found in .env file")
  19. dbname = os.getenv("NAME")
  20. if dbname is None:
  21. raise ValueError("NAME not found in .env file")
  22. dbpsswd = os.getenv("PASSWORD")
  23. if dbpsswd is None:
  24. raise ValueError("PASSWORD not found in .env file")
  25. dbdb = os.getenv("DATABASE")
  26. if dbdb is None:
  27. raise ValueError("DATABASE not found in .env file")
  28. #Database initialization
  29. conn = mysql.connector.connect(
  30. host=dbhost,
  31. user=dbname,
  32. password=dbpsswd
  33. )
  34. self.cursor = conn.cursor()
  35. conn.database = dbdb
  36. #Command initialization
  37. @slash_command(name="modinfo", description="Shows the moderative history of a user from this Server")
  38. async def modinfo(
  39. self,
  40. ctx,
  41. user: Option(discord.User, required=True) # type: ignore
  42. ):
  43. await ctx.defer(ephemeral=False)
  44. if not ctx.author.guild_permissions.kick_members:
  45. await ctx.followup.send("No permission.", ephemeral=True)
  46. return
  47. embed = discord.Embed(
  48. title=f"__Moderation History for {user.name}__",
  49. color=discord.Color.orange()
  50. )
  51. self.cursor.execute(
  52. "SELECT moderatorname, reason, date FROM Warns WHERE userid = %s",
  53. (user.id,)
  54. )
  55. warns = self.cursor.fetchall()
  56. if warns:
  57. for moderatorname, reason, date in warns:
  58. embed.add_field(
  59. name=f"Warned by {moderatorname} on {date.strftime('%Y-%m-%d %H:%M:%S')}",
  60. value=f"Reason: {reason}",
  61. inline=False
  62. )
  63. self.cursor.execute(
  64. "SELECT moderatorname, reason, date FROM Kick WHERE userid = %s",
  65. (user.id,)
  66. )
  67. kicks = self.cursor.fetchall()
  68. if kicks:
  69. for moderatorname, reason, date in kicks:
  70. embed.add_field(
  71. name=f"Kicked by {moderatorname} on {date.strftime('%Y-%m-%d %H:%M:%S')}",
  72. value=f"Reason: {reason}",
  73. inline=False
  74. )
  75. self.cursor.execute(
  76. "SELECT moderatorname, reason, date FROM Bans WHERE userid = %s",
  77. (user.id,)
  78. )
  79. bans = self.cursor.fetchall()
  80. if bans:
  81. for moderatorname, reason, date in bans:
  82. embed.add_field(
  83. name=f"Banned by {moderatorname} on {date.strftime('%Y-%m-%d %H:%M:%S')}",
  84. value=f"Reason: {reason}",
  85. inline=False
  86. )
  87. self.cursor.execute(
  88. "SELECT moderatorname, reason, date FROM Unbans WHERE userid = %s",
  89. (user.id,)
  90. )
  91. unbans = self.cursor.fetchall()
  92. if unbans:
  93. for moderatorname, reason, date in unbans:
  94. embed.add_field(
  95. name=f"Unbanned by {moderatorname} on {date.strftime('%Y-%m-%d %H:%M:%S')}",
  96. value=f"Reason: {reason}",
  97. inline=False
  98. )
  99. if not warns and not kicks and not bans and not unbans:
  100. await ctx.followup.send(f"User `{user.name}` has no moderation history.", ephemeral=True)
  101. return
  102. embed.set_thumbnail(url=user.display_avatar.url)
  103. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  104. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  105. await ctx.followup.send(embed=embed, ephemeral=False)
  106. def setup(bot: discord.Bot):
  107. bot.add_cog(modinfo(bot))