main.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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="BaumSplitter41 Test 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. @bot.slash_command(name="ban", description="Ban a user from this Server")
  115. async def ban(
  116. ctx,
  117. user: Option(discord.User, description = "Select User", required=True), # type: ignore
  118. reason: Option(str, description = "Reason for the ban", default="No reason provided") # type: ignore
  119. ):
  120. if not ctx.author.guild_permissions.ban_members:
  121. await ctx.respond("Error: You don't have the permission to ban Members!", ephemeral=True)
  122. return
  123. if user == bot.user:
  124. await ctx.respond("Error: I can't ban myself!", ephemeral=True)
  125. return
  126. if user == ctx.author:
  127. await ctx.respond("Error: You can't ban yourself!", ephemeral=True)
  128. return
  129. channel= discord.utils.get(ctx.guild.channels, id = int(banlog_id))
  130. embed = discord.Embed(
  131. title=f"Ban of **{user.name}**",
  132. description=f"User {user.mention} has been banned from the Server",
  133. color=discord.Color.red()
  134. )
  135. time = discord.utils.format_dt(datetime.now(), "f")
  136. embed.add_field(name="Ban Date", value=time, inline=False)
  137. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  138. embed.add_field(name="Reason", value=reason, inline=False)
  139. embed.add_field(name="User ID", value=user.id)
  140. embed.set_thumbnail(url=user.display_avatar.url)
  141. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  142. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  143. try:
  144. await ctx.guild.ban(user, reason=reason)
  145. await ctx.respond(f"User {user.mention} has been banned from this Server!", ephemeral=True)
  146. await channel.send(embed=embed)
  147. except discord.Forbidden:
  148. await ctx.respond("Error: I don't have permission to ban this user.", ephemeral=True)
  149. except discord.HTTPException as e:
  150. await ctx.respond(f"Error: Could not ban User {user.mention}. Reason: {e}", ephemeral=True)
  151. except Exception as e:
  152. await ctx.respond(f"Unexpected error: {e}", ephemeral=True)
  153. #---------------------------------#
  154. #---------------------------------#
  155. #Run function
  156. bot.run(token)
  157. #---------------------------------#