main.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. ## Deleted Message
  46. """
  47. @bot.event
  48. async def on_message_delete(
  49. ctx = discord.Message,
  50. ):
  51. if ctx.author != bot.user:
  52. await ctx.send(f"Eine Nachricht von {ctx.author} wurde gelöscht: {ctx.content}", ephemeral=False)
  53. """
  54. #---------------------------------#
  55. #---------------------------------#
  56. @bot.event
  57. async def on_message_delete(msg):
  58. if msg.author != bot.user:
  59. await msg.channel.send(f"A Message from {msg.author} has been deleted: {msg.content}")
  60. #---------------------------------#
  61. #---------------------------------#
  62. ## Greet
  63. @bot.slash_command(description="Greet a User")
  64. async def greet(ctx, user: str = Option(discord.User, "The user, you want to greet")):
  65. await ctx.respond(f"Hello {user.mention}")
  66. #---------------------------------#
  67. #---------------------------------#
  68. ## Say
  69. @bot.slash_command(description="Let the bot send a message")
  70. async def say(
  71. ctx,
  72. text: str = Option(description="Input the text you want to send"),
  73. channel_input: discord.TextChannel = Option(description="Select the channel,where you want to send the message.")
  74. ):
  75. channel= discord.utils.get(ctx.guild.channels, id = int(channel_input[2:-1]))
  76. await channel.send(text)
  77. await ctx.respond("Message sent", ephemeral=True)
  78. #---------------------------------#
  79. #---------------------------------#
  80. ## Userinfo
  81. @bot.slash_command(name="userinfo", description="Show informations of a user from this server")
  82. async def userinfo(
  83. ctx,
  84. user: str = Option(discord.User, "Select User"),
  85. ):
  86. if user is None:
  87. user = ctx.author
  88. elif user not in ctx.guild.members:
  89. await ctx.respond("The selected user is not a member on this Server!", ephemeral=True)
  90. return
  91. elif user == bot.user:
  92. await ctx.respond(f"This is me - the {bot.user}", ephemeral=True)
  93. return
  94. embed = discord.Embed(
  95. title=f"Information about *{user.name}*",
  96. description=f"Here you see all details about {user.mention}",
  97. color=discord.Color.blue()
  98. )
  99. time = discord.utils.format_dt(user.created_at, "R")
  100. embed.add_field(name="Account creation date", value=time, inline=False)
  101. if len(user.roles) >= 2:
  102. embed.add_field(name="Roles", value=", ".join([role.mention for role in user.roles if role.name != "@everyone"]), inline=False)
  103. else:
  104. embed.add_field(name="Roles", value="User has no roles", inline=False)
  105. embed.add_field(name="Server join date", value=discord.utils.format_dt(user.joined_at, "R"), inline=False)
  106. embed.add_field(name="User ID", value=user.id)
  107. embed.set_thumbnail(url=user.display_avatar.url)
  108. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  109. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  110. await ctx.respond(embed=embed)
  111. #---------------------------------#
  112. #_________________________________#
  113. #BAN SYSTEM
  114. #---------------------------------#
  115. ##Ban
  116. @bot.slash_command(name="ban", description="Ban a user from this Server")
  117. async def ban(
  118. ctx,
  119. user: Option(discord.User, description = "Select User", required=True), # type: ignore
  120. reason: Option(str, description = "Reason for the ban", default="No reason provided") # type: ignore
  121. ):
  122. if not ctx.author.guild_permissions.ban_members:
  123. await ctx.respond("Error: You don't have the permission to ban Members!", ephemeral=True)
  124. return
  125. if user == bot.user:
  126. await ctx.respond("Error: I can't ban myself!", ephemeral=True)
  127. return
  128. if user == ctx.author:
  129. await ctx.respond("Error: You can't ban yourself!", ephemeral=True)
  130. return
  131. channel= discord.utils.get(ctx.guild.channels, id = int(banlog_id))
  132. embed = discord.Embed(
  133. title=f"Ban of **{user.name}**",
  134. description=f"User {user.mention} has been banned from the Server",
  135. color=discord.Color.red()
  136. )
  137. time = discord.utils.format_dt(datetime.now(), "f")
  138. embed.add_field(name="Ban Date", value=time, inline=False)
  139. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  140. embed.add_field(name="Reason", value=reason, inline=False)
  141. embed.add_field(name="User ID", value=user.id)
  142. embed.set_thumbnail(url=user.display_avatar.url)
  143. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  144. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  145. try:
  146. await ctx.guild.ban(user, reason=reason)
  147. await ctx.respond(f"User {user.mention} has been banned from this Server!", ephemeral=True)
  148. await channel.send(embed=embed)
  149. except discord.Forbidden:
  150. await ctx.respond("Error: I don't have permission to ban this user.", ephemeral=True)
  151. except discord.HTTPException as e:
  152. await ctx.respond(f"Error: Could not ban User {user.mention}. Reason: {e}", ephemeral=True)
  153. except Exception as e:
  154. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  155. #---------------------------------#
  156. #Unban
  157. @bot.slash_command(name="unban", description="Unban a user from this Server")
  158. async def ban(
  159. ctx,
  160. user: Option(discord.User, description = "Insert User ID", required=True), # type: ignore
  161. reason: Option(str, description = "Reason for the unbanning", default="No reason provided") # type: ignore
  162. ):
  163. if not ctx.author.guild_permissions.ban_members:
  164. await ctx.respond("Error: You don't have the permission to unban Members!", ephemeral=True)
  165. return
  166. if user == bot.user:
  167. await ctx.respond("Error: I can't unban myself!", ephemeral=True)
  168. return
  169. if user == ctx.author:
  170. await ctx.respond("Error: You can't unban yourself!", ephemeral=True)
  171. return
  172. if user in ctx.guild.members:
  173. await ctx.respond("Error: This user is not banned!", ephemeral=True)
  174. return
  175. channel= discord.utils.get(ctx.guild.channels, id = int(banlog_id))
  176. embed = discord.Embed(
  177. title=f"Unban of **{user.name}**",
  178. description=f"User {user.mention} was unbanned from this server.",
  179. color=discord.Color.green()
  180. )
  181. time = discord.utils.format_dt(datetime.now(), "f")
  182. embed.add_field(name="Unban Date", value=time, inline=False)
  183. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  184. embed.add_field(name="Reason", value=reason, inline=False)
  185. embed.add_field(name="User ID", value=user.id)
  186. embed.set_thumbnail(url=user.display_avatar.url)
  187. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  188. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  189. try:
  190. await ctx.guild.unban(user, reason=reason)
  191. await ctx.respond(f"User {user.mention} is now unbanned!", ephemeral=True)
  192. await channel.send(embed=embed)
  193. except discord.Forbidden:
  194. await ctx.respond("Error: I don't have permission to unban this user.", ephemeral=True)
  195. except discord.HTTPException as e:
  196. await ctx.respond(f"Error: Could not unban User {user.mention}. Reason: {e}", ephemeral=True)
  197. except Exception as e:
  198. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  199. #---------------------------------#
  200. #Run function
  201. bot.run(token)
  202. #---------------------------------#