userinfo.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import discord
  2. from discord.ext import commands
  3. from discord.commands import Option
  4. from discord.commands import slash_command
  5. import mysql.connector
  6. import configparser
  7. import os
  8. from dotenv import load_dotenv
  9. class userinfo(commands.Cog):
  10. def __init__(self, bot: discord.Bot):
  11. self.bot = bot
  12. def _load_config(self):
  13. config = configparser.ConfigParser()
  14. configFilePath = r'config.cfg'
  15. config.read(configFilePath)
  16. return config
  17. def _connect_db(self):
  18. load_dotenv()
  19. dbhost = os.getenv("HOST")
  20. if dbhost is None:
  21. raise ValueError("HOST not found in .env file")
  22. dbname = os.getenv("NAME")
  23. if dbname is None:
  24. raise ValueError("NAME not found in .env file")
  25. dbpsswd = os.getenv("PASSWORD")
  26. if dbpsswd is None:
  27. raise ValueError("PASSWORD not found in .env file")
  28. dbdb = os.getenv("DATABASE")
  29. if dbdb is None:
  30. raise ValueError("DATABASE not found in .env file")
  31. connection = mysql.connector.connect(
  32. host=dbhost,
  33. user=dbname,
  34. password=dbpsswd,
  35. database=dbdb
  36. )
  37. return connection
  38. #Command initialization
  39. @slash_command(name="userinfo", description="Show informations of a user from this server")
  40. async def userinfo(
  41. self,
  42. ctx,
  43. user: str = Option(discord.User, "Select User"),
  44. ):
  45. config = self._load_config()
  46. team_role_id = config.get('Team Roles', 'team_role_id')
  47. team_role = ctx.guild.get_role(int(team_role_id))
  48. conn = self._connect_db()
  49. cursor = conn.cursor()
  50. conn.database = self._connect_db().dbdb
  51. if team_role not in ctx.author.roles:
  52. await ctx.respond("You don't have the permission to use this command!", ephemeral=True)
  53. return
  54. if user is None:
  55. user = ctx.author
  56. elif user not in ctx.guild.members:
  57. await ctx.respond("The selected user is not a member on this Server!", ephemeral=True)
  58. return
  59. elif user == self.bot.user:
  60. await ctx.respond(f"This is me - the {self.bot.user}", ephemeral=True)
  61. return
  62. embed = discord.Embed(
  63. title=f"Information about *{user.name}*",
  64. description=f"Here you see all details about {user.mention}",
  65. color=discord.Color.blue()
  66. )
  67. #get database entrys of the notes
  68. events = []
  69. cursor.execute("SELECT moderatorname, information, date FROM Notes WHERE userid = %s", (user.id,))
  70. for moderatorname, reason, date in cursor.fetchall():
  71. events.append((moderatorname, reason, date))
  72. time = discord.utils.format_dt(user.created_at, "R")
  73. embed.add_field(name="Account creation date", value=time, inline=False)
  74. if len(user.roles) >= 2:
  75. embed.add_field(name="Roles", value=", ".join([role.mention for role in user.roles if role.name != "@everyone"]), inline=False)
  76. else:
  77. embed.add_field(name="Roles", value="User has no roles", inline=False)
  78. embed.add_field(name="Server join date", value=discord.utils.format_dt(user.joined_at, "R"), inline=False)
  79. embed.add_field(name="User ID", value=user.id)
  80. embed.add_field(name="Bot", value="Yes" if user.bot else "No")
  81. if events:
  82. for moderatorname, reason, date in events:
  83. embed.add_field(name=f"Note by {moderatorname} on {date}", value=reason, inline=False)
  84. embed.set_thumbnail(url=user.display_avatar.url)
  85. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  86. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  87. await ctx.respond(embed=embed)
  88. def setup(bot: discord.Bot):
  89. bot.add_cog(userinfo(bot))