main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import os
  2. from dotenv import load_dotenv
  3. import discord
  4. from discord.ext import commands
  5. from discord.commands import Option
  6. from discord.commands import slash_command
  7. from datetime import datetime
  8. import configparser
  9. intents = discord.Intents.default()
  10. intents.message_content = True
  11. intents.members = True
  12. intents.guilds = True
  13. intents.reactions = True
  14. client = discord.Client(intents=intents)
  15. #------#
  16. #Load .env file
  17. load_dotenv()
  18. token = os.getenv("TOKEN")
  19. if token is None:
  20. raise ValueError("TOKEN not found in .env file")
  21. debug_guilds_up = []
  22. server_token = os.getenv("SERVER").split(",")
  23. for i in range(len(server_token)):
  24. debug_guilds_up.append(int(server_token[i]))
  25. #------#
  26. #ConfigParser
  27. config = configparser.RawConfigParser()
  28. configFilePath = r'config.cfg'
  29. config.read_file(open(configFilePath))
  30. label_rules = config.get('Reactionroles Rules', 'label_rules')
  31. role_rules = config.get('Reactionroles Rules', 'rules_role')
  32. print(role_rules)
  33. channel_log = config.get('Logs', 'channel_log')
  34. channel_banlog = config.get('Logs', 'ban_log')
  35. #------#
  36. #Initialize Bot
  37. bot = commands.Bot(
  38. command_prefix=commands.when_mentioned_or("!"),
  39. description="VicePD Bot",
  40. intents=intents,
  41. debug_guilds=debug_guilds_up if debug_guilds_up else None
  42. )
  43. async def load_extensions():
  44. for filename in os.listdir("cogs"):
  45. if filename.endswith(".py"):
  46. bot.load_extension(f"cogs.{filename[:-3]}")
  47. class Admin(commands.Cog):
  48. def __init__(self, bot):
  49. self.bot = bot
  50. #---------------------------------#
  51. #Bot Online Console
  52. @bot.event
  53. async def on_ready():
  54. print(f"{bot.user} ist online")
  55. if bot.guilds:
  56. channel = discord.utils.get(bot.guilds[0].channels, id=int(channel_log))
  57. if channel:
  58. await channel.send(f"{bot.user} ist online")
  59. await load_extensions()
  60. bot.add_view(PersistentRoleView()) #loading reactionrole memory
  61. #---------------------------------------------------------------------------------------#
  62. #DONT Touch anything above this line, unless you know what you are doing!#
  63. #---------------------------------------------------------------------------------------#
  64. #---------------------------------#
  65. ## Greet
  66. @bot.slash_command(description="Greet a User")
  67. async def greet(ctx, user: str = Option(discord.User, "The user, you want to greet")):
  68. await ctx.respond(f"Hello {user.mention}")
  69. #---------------------------------#
  70. #---------------------------------#
  71. ## Say
  72. @bot.slash_command(description="Let the bot send a message")
  73. async def say(
  74. ctx,
  75. text: str = Option(description="Input the text you want to send"),
  76. channel_input: discord.TextChannel = Option(description="Select the channel,where you want to send the message.")
  77. ):
  78. channel= discord.utils.get(ctx.guild.channels, id = int(channel_input[2:-1]))
  79. await channel.send(text)
  80. await ctx.respond("Message sent", ephemeral=True)
  81. #---------------------------------#
  82. #---------------------------------#
  83. ## Userinfo
  84. @bot.slash_command(name="userinfo", description="Show informations of a user from this server")
  85. async def userinfo(
  86. ctx,
  87. user: str = Option(discord.User, "Select User"),
  88. ):
  89. if user is None:
  90. user = ctx.author
  91. elif user not in ctx.guild.members:
  92. await ctx.respond("The selected user is not a member on this Server!", ephemeral=True)
  93. return
  94. elif user == bot.user:
  95. await ctx.respond(f"This is me - the {bot.user}", ephemeral=True)
  96. return
  97. embed = discord.Embed(
  98. title=f"Information about *{user.name}*",
  99. description=f"Here you see all details about {user.mention}",
  100. color=discord.Color.blue()
  101. )
  102. time = discord.utils.format_dt(user.created_at, "R")
  103. embed.add_field(name="Account creation date", value=time, inline=False)
  104. if len(user.roles) >= 2:
  105. embed.add_field(name="Roles", value=", ".join([role.mention for role in user.roles if role.name != "@everyone"]), inline=False)
  106. else:
  107. embed.add_field(name="Roles", value="User has no roles", inline=False)
  108. embed.add_field(name="Server join date", value=discord.utils.format_dt(user.joined_at, "R"), inline=False)
  109. embed.add_field(name="User ID", value=user.id)
  110. embed.set_thumbnail(url=user.display_avatar.url)
  111. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  112. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  113. await ctx.respond(embed=embed)
  114. #---------------------------------#
  115. #_________________________________#
  116. #BAN SYSTEM
  117. #---------------------------------#
  118. ##Ban
  119. @bot.slash_command(name="ban", description="Ban a user from this Server")
  120. async def ban(
  121. ctx,
  122. user: Option(discord.User, description = "Select User", required=True), # type: ignore
  123. reason: Option(str, description = "Reason for the ban", default="No reason provided") # type: ignore
  124. ):
  125. if not ctx.author.guild_permissions.ban_members:
  126. await ctx.respond("Error: You don't have the permission to ban Members!", ephemeral=True)
  127. return
  128. if user == bot.user:
  129. await ctx.respond("Error: I can't ban myself!", ephemeral=True)
  130. return
  131. if user == ctx.author:
  132. await ctx.respond("Error: You can't ban yourself!", ephemeral=True)
  133. return
  134. channel= discord.utils.get(ctx.guild.channels, id = int(channel_banlog))
  135. embed = discord.Embed(
  136. title=f"Ban of **{user.name}**",
  137. description=f"User {user.mention} has been banned from the Server",
  138. color=discord.Color.red()
  139. )
  140. time = discord.utils.format_dt(datetime.now(), "f")
  141. embed.add_field(name="Ban Date", value=time, inline=False)
  142. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  143. embed.add_field(name="Reason", value=reason, inline=False)
  144. embed.add_field(name="User ID", value=user.id)
  145. embed.set_thumbnail(url=user.display_avatar.url)
  146. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  147. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  148. try:
  149. await ctx.guild.ban(user, reason=reason)
  150. await ctx.respond(f"User {user.mention} has been banned from this Server!", ephemeral=True)
  151. await channel.send(embed=embed)
  152. except discord.Forbidden:
  153. await ctx.respond("Error: I don't have permission to ban this user.", ephemeral=True)
  154. except discord.HTTPException as e:
  155. await ctx.respond(f"Error: Could not ban User {user.mention}. Reason: {e}", ephemeral=True)
  156. except Exception as e:
  157. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  158. #---------------------------------#
  159. #Unban
  160. @bot.slash_command(name="unban", description="Unban a user from this Server")
  161. async def ban(
  162. ctx,
  163. user: Option(discord.User, description = "Insert User ID", required=True), # type: ignore
  164. reason: Option(str, description = "Reason for the unbanning", default="No reason provided") # type: ignore
  165. ):
  166. if not ctx.author.guild_permissions.ban_members:
  167. await ctx.respond("Error: You don't have the permission to unban Members!", ephemeral=True)
  168. return
  169. if user == bot.user:
  170. await ctx.respond("Error: I can't unban myself!", ephemeral=True)
  171. return
  172. if user == ctx.author:
  173. await ctx.respond("Error: You can't unban yourself!", ephemeral=True)
  174. return
  175. if user in ctx.guild.members:
  176. await ctx.respond("Error: This user is not banned!", ephemeral=True)
  177. return
  178. channel= discord.utils.get(ctx.guild.channels, id = int(channel_banlog))
  179. embed = discord.Embed(
  180. title=f"Unban of **{user.name}**",
  181. description=f"User {user.mention} was unbanned from this server.",
  182. color=discord.Color.green()
  183. )
  184. time = discord.utils.format_dt(datetime.now(), "f")
  185. embed.add_field(name="Unban Date", value=time, inline=False)
  186. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  187. embed.add_field(name="Reason", value=reason, inline=False)
  188. embed.add_field(name="User ID", value=user.id)
  189. embed.set_thumbnail(url=user.display_avatar.url)
  190. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  191. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  192. try:
  193. await ctx.guild.unban(user, reason=reason)
  194. await ctx.respond(f"User {user.mention} is now unbanned!", ephemeral=True)
  195. await channel.send(embed=embed)
  196. except discord.Forbidden:
  197. await ctx.respond("Error: I don't have permission to unban this user.", ephemeral=True)
  198. except discord.HTTPException as e:
  199. await ctx.respond(f"Error: Could not unban User {user.mention}. Reason: {e}", ephemeral=True)
  200. except Exception as e:
  201. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  202. #---------------------------------#
  203. #_________________________________#
  204. #---------------------------------#
  205. #Kick
  206. @bot.slash_command(name="kick", description="Kick a user from this Server")
  207. async def ban(
  208. ctx,
  209. user: Option(discord.User, description = "Select User", required=True), # type: ignore
  210. reason: Option(str, description = "Reason for the ban", default="No reason provided") # type: ignore
  211. ):
  212. if not ctx.author.guild_permissions.kick_members:
  213. await ctx.respond("Error: You don't have the permission to kick Members!", ephemeral=True)
  214. return
  215. if user == bot.user:
  216. await ctx.respond("Error: I can't kick myself!", ephemeral=True)
  217. return
  218. if user == ctx.author:
  219. await ctx.respond("Error: You can't kick yourself!", ephemeral=True)
  220. return
  221. try:
  222. await ctx.guild.kick(user, reason=reason)
  223. await ctx.respond(f"User {user.mention} has been kick from this Server!", ephemeral=True)
  224. except discord.Forbidden:
  225. await ctx.respond("Error: I don't have permission to kick this user.", ephemeral=True)
  226. except discord.HTTPException as e:
  227. await ctx.respond(f"Error: Could not kick User {user.mention}. Reason: {e}", ephemeral=True)
  228. except Exception as e:
  229. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  230. #---------------------------------#
  231. #reaction role system
  232. #reaction role verfiy
  233. class PersistentRoleView(discord.ui.View):
  234. def __init__(self):
  235. super().__init__(timeout=None)
  236. @discord.ui.button(
  237. label=label_rules,
  238. style=discord.ButtonStyle.success,
  239. emoji="✅",
  240. custom_id="persistent_view:role_verify"
  241. )
  242. async def verify_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
  243. role = interaction.guild.get_role(int(role_rules))
  244. if role is None:
  245. await interaction.response.send_message("Fehler: Die konfigurierte Rolle wurde nicht gefunden.", ephemeral=True)
  246. return
  247. if role in interaction.user.roles:
  248. await interaction.user.remove_roles(role)
  249. await interaction.response.send_message(f"Rolle **{role.name}** wurde entfernt.", ephemeral=True)
  250. else:
  251. await interaction.user.add_roles(role)
  252. await interaction.response.send_message(f"Du hast die Rolle **{role.name}** erhalten!", ephemeral=True)
  253. @bot.slash_command(name="verify_message", description="Sendet die persistente Reaction-Role Nachricht")
  254. async def setup_rr(
  255. ctx: discord.ApplicationContext,
  256. channel: discord.TextChannel,
  257. title: str,
  258. description: str
  259. ):
  260. if not ctx.author.guild_permissions.administrator:
  261. await ctx.respond("Keine Berechtigung.", ephemeral=True)
  262. return
  263. embed = discord.Embed(
  264. title=title,
  265. description=description,
  266. color=discord.Color.red()
  267. )
  268. try:
  269. await channel.send(embed=embed, view=PersistentRoleView())
  270. await ctx.respond(f"Nachricht erfolgreich in {channel.mention} gesendet!", ephemeral=True)
  271. except discord.Forbidden:
  272. await ctx.respond("Ich darf in diesem Kanal nicht schreiben.", ephemeral=True)
  273. #---------------------------------#
  274. #Run function
  275. bot.run(token)
  276. #---------------------------------#