main.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. intents = discord.Intents.default()
  9. intents.message_content = True
  10. client = discord.Client(intents=intents)
  11. load_dotenv()
  12. token = os.getenv("TOKEN")
  13. if token is None:
  14. raise ValueError("TOKEN not found in .env file")
  15. debug_guilds_up = []
  16. server_token = os.getenv("SERVER").split(",")
  17. for i in range(len(server_token)):
  18. debug_guilds_up.append(int(server_token[i]))
  19. banlog_id = os.getenv("BANLOG")
  20. if banlog_id is None:
  21. raise ValueError("BANLOG ID not found in .env file")
  22. bot = commands.Bot(
  23. command_prefix=commands.when_mentioned_or("!"),
  24. description="VicePD Bot",
  25. intents=intents,
  26. debug_guilds=debug_guilds_up if debug_guilds_up else None
  27. )
  28. async def load_extensions():
  29. for filename in os.listdir("cogs"):
  30. if filename.endswith(".py"):
  31. await bot.load_extension(f"cogs.{filename[:-3]}")
  32. class Admin(commands.Cog):
  33. def __init__(self, bot):
  34. self.bot = bot
  35. @bot.event
  36. async def on_ready():
  37. print(f"{bot.user} ist online")
  38. @bot.listen()
  39. async def on_guild_join(guild):
  40. print(f"LOG: guild {guild} joined")
  41. #---------------------------------------------------------------------------------------#
  42. #DONT Touch anything above this line, unless you know what you are doing!#
  43. #---------------------------------------------------------------------------------------#
  44. #---------------------------------#
  45. ## Greet
  46. @bot.slash_command(description="Greet a User")
  47. async def greet(ctx, user: str = Option(discord.User, "The user, you want to greet")):
  48. await ctx.respond(f"Hello {user.mention}")
  49. #---------------------------------#
  50. #---------------------------------#
  51. ## Say
  52. @bot.slash_command(description="Let the bot send a message")
  53. async def say(
  54. ctx,
  55. text: str = Option(description="Input the text you want to send"),
  56. channel_input: discord.TextChannel = Option(description="Select the channel,where you want to send the message.")
  57. ):
  58. channel= discord.utils.get(ctx.guild.channels, id = int(channel_input[2:-1]))
  59. await channel.send(text)
  60. await ctx.respond("Message sent", ephemeral=True)
  61. #---------------------------------#
  62. #---------------------------------#
  63. ## Userinfo
  64. @bot.slash_command(name="userinfo", description="Show informations of a user from this server")
  65. async def userinfo(
  66. ctx,
  67. user: str = Option(discord.User, "Select User"),
  68. ):
  69. if user is None:
  70. user = ctx.author
  71. elif user not in ctx.guild.members:
  72. await ctx.respond("The selected user is not a member on this Server!", ephemeral=True)
  73. return
  74. elif user == bot.user:
  75. await ctx.respond(f"This is me - the {bot.user}", ephemeral=True)
  76. return
  77. embed = discord.Embed(
  78. title=f"Information about *{user.name}*",
  79. description=f"Here you see all details about {user.mention}",
  80. color=discord.Color.blue()
  81. )
  82. time = discord.utils.format_dt(user.created_at, "R")
  83. embed.add_field(name="Account creation date", value=time, inline=False)
  84. if len(user.roles) >= 2:
  85. embed.add_field(name="Roles", value=", ".join([role.mention for role in user.roles if role.name != "@everyone"]), inline=False)
  86. else:
  87. embed.add_field(name="Roles", value="User has no roles", inline=False)
  88. embed.add_field(name="Server join date", value=discord.utils.format_dt(user.joined_at, "R"), inline=False)
  89. embed.add_field(name="User ID", value=user.id)
  90. embed.set_thumbnail(url=user.display_avatar.url)
  91. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  92. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  93. await ctx.respond(embed=embed)
  94. #---------------------------------#
  95. #_________________________________#
  96. #BAN SYSTEM
  97. #---------------------------------#
  98. ##Ban
  99. @bot.slash_command(name="ban", description="Ban a user from this Server")
  100. async def ban(
  101. ctx,
  102. user: Option(discord.User, description = "Select User", required=True), # type: ignore
  103. reason: Option(str, description = "Reason for the ban", default="No reason provided") # type: ignore
  104. ):
  105. if not ctx.author.guild_permissions.ban_members:
  106. await ctx.respond("Error: You don't have the permission to ban Members!", ephemeral=True)
  107. return
  108. if user == bot.user:
  109. await ctx.respond("Error: I can't ban myself!", ephemeral=True)
  110. return
  111. if user == ctx.author:
  112. await ctx.respond("Error: You can't ban yourself!", ephemeral=True)
  113. return
  114. channel= discord.utils.get(ctx.guild.channels, id = int(banlog_id))
  115. embed = discord.Embed(
  116. title=f"Ban of **{user.name}**",
  117. description=f"User {user.mention} has been banned from the Server",
  118. color=discord.Color.red()
  119. )
  120. time = discord.utils.format_dt(datetime.now(), "f")
  121. embed.add_field(name="Ban Date", value=time, inline=False)
  122. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  123. embed.add_field(name="Reason", value=reason, inline=False)
  124. embed.add_field(name="User ID", value=user.id)
  125. embed.set_thumbnail(url=user.display_avatar.url)
  126. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  127. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  128. try:
  129. await ctx.guild.ban(user, reason=reason)
  130. await ctx.respond(f"User {user.mention} has been banned from this Server!", ephemeral=True)
  131. await channel.send(embed=embed)
  132. except discord.Forbidden:
  133. await ctx.respond("Error: I don't have permission to ban this user.", ephemeral=True)
  134. except discord.HTTPException as e:
  135. await ctx.respond(f"Error: Could not ban User {user.mention}. Reason: {e}", ephemeral=True)
  136. except Exception as e:
  137. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  138. #---------------------------------#
  139. #Unban
  140. @bot.slash_command(name="unban", description="Unban a user from this Server")
  141. async def ban(
  142. ctx,
  143. user: Option(discord.User, description = "Insert User ID", required=True), # type: ignore
  144. reason: Option(str, description = "Reason for the unbanning", default="No reason provided") # type: ignore
  145. ):
  146. if not ctx.author.guild_permissions.ban_members:
  147. await ctx.respond("Error: You don't have the permission to unban Members!", ephemeral=True)
  148. return
  149. if user == bot.user:
  150. await ctx.respond("Error: I can't unban myself!", ephemeral=True)
  151. return
  152. if user == ctx.author:
  153. await ctx.respond("Error: You can't unban yourself!", ephemeral=True)
  154. return
  155. if user in ctx.guild.members:
  156. await ctx.respond("Error: This user is not banned!", ephemeral=True)
  157. return
  158. channel= discord.utils.get(ctx.guild.channels, id = int(banlog_id))
  159. embed = discord.Embed(
  160. title=f"Unban of **{user.name}**",
  161. description=f"User {user.mention} was unbanned from this server.",
  162. color=discord.Color.green()
  163. )
  164. time = discord.utils.format_dt(datetime.now(), "f")
  165. embed.add_field(name="Unban Date", value=time, inline=False)
  166. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  167. embed.add_field(name="Reason", value=reason, inline=False)
  168. embed.add_field(name="User ID", value=user.id)
  169. embed.set_thumbnail(url=user.display_avatar.url)
  170. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  171. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  172. try:
  173. await ctx.guild.unban(user, reason=reason)
  174. await ctx.respond(f"User {user.mention} is now unbanned!", ephemeral=True)
  175. await channel.send(embed=embed)
  176. except discord.Forbidden:
  177. await ctx.respond("Error: I don't have permission to unban this user.", ephemeral=True)
  178. except discord.HTTPException as e:
  179. await ctx.respond(f"Error: Could not unban User {user.mention}. Reason: {e}", ephemeral=True)
  180. except Exception as e:
  181. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  182. #---------------------------------#
  183. #_________________________________#
  184. #---------------------------------#
  185. #Kick
  186. @bot.slash_command(name="kick", description="Kick a user from this Server")
  187. async def ban(
  188. ctx,
  189. user: Option(discord.User, description = "Select User", required=True), # type: ignore
  190. reason: Option(str, description = "Reason for the ban", default="No reason provided") # type: ignore
  191. ):
  192. if not ctx.author.guild_permissions.kick_members:
  193. await ctx.respond("Error: You don't have the permission to kick Members!", ephemeral=True)
  194. return
  195. if user == bot.user:
  196. await ctx.respond("Error: I can't kick myself!", ephemeral=True)
  197. return
  198. if user == ctx.author:
  199. await ctx.respond("Error: You can't kick yourself!", ephemeral=True)
  200. return
  201. try:
  202. await ctx.guild.kick(user, reason=reason)
  203. await ctx.respond(f"User {user.mention} has been kick from this Server!", ephemeral=True)
  204. except discord.Forbidden:
  205. await ctx.respond("Error: I don't have permission to kick this user.", ephemeral=True)
  206. except discord.HTTPException as e:
  207. await ctx.respond(f"Error: Could not kick User {user.mention}. Reason: {e}", ephemeral=True)
  208. except Exception as e:
  209. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  210. #---------------------------------#
  211. #---------------------------------#
  212. #Run function
  213. bot.run(token)
  214. #---------------------------------#