main.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. bot = commands.Bot(
  20. command_prefix=commands.when_mentioned_or("!"),
  21. description="BaumSplitter41 Test Bot",
  22. intents=intents,
  23. debug_guilds=debug_guilds_up if debug_guilds_up else None
  24. )
  25. async def load_extensions():
  26. for filename in os.listdir("cogs"):
  27. if filename.endswith(".py"):
  28. await bot.load_extension(f"cogs.{filename[:-3]}")
  29. class Admin(commands.Cog):
  30. def __init__(self, bot):
  31. self.bot = bot
  32. @bot.event
  33. async def on_ready():
  34. print(f"{bot.user} ist online")
  35. @bot.listen()
  36. async def on_guild_join(guild):
  37. print(f"LOG: guild {guild} joined")
  38. #---------------------------------------------------------------------------------------#
  39. #DONT Touch anything above this line, unless you know what you are doing!#
  40. #---------------------------------------------------------------------------------------#
  41. #---------------------------------#
  42. ## Greet
  43. @bot.slash_command(description="Greet a User")
  44. async def greet(ctx, user: str = Option(discord.User, "The user, you want to greet")):
  45. await ctx.respond(f"Hello {user.mention}")
  46. #---------------------------------#
  47. #---------------------------------#
  48. ## Say
  49. @bot.slash_command(description="Let the bot send a message")
  50. async def say(
  51. ctx,
  52. text: str = Option(description="Input the text you want to send"),
  53. channel_input: discord.TextChannel = Option(description="Select the channel,where you want to send the message.")
  54. ):
  55. channel= discord.utils.get(ctx.guild.channels, id = int(channel_input[2:-1]))
  56. await channel.send(text)
  57. await ctx.respond("Message sent", ephemeral=True)
  58. #---------------------------------#
  59. #---------------------------------#
  60. ## Userinfo
  61. @bot.slash_command(name="userinfo", description="Show informations of a user from this server")
  62. async def userinfo(
  63. ctx,
  64. user: str = Option(discord.User, "Select User"),
  65. ):
  66. if user is None:
  67. user = ctx.author
  68. elif user not in ctx.guild.members:
  69. await ctx.respond("The selected user is not a member on this Server!", ephemeral=True)
  70. return
  71. elif user == bot.user:
  72. await ctx.respond(f"This is me - the {bot.user}", ephemeral=True)
  73. return
  74. embed = discord.Embed(
  75. title=f"Information about *{user.name}*",
  76. description=f"Here you see all details about {user.mention}",
  77. color=discord.Color.blue()
  78. )
  79. time = discord.utils.format_dt(user.created_at, "R")
  80. embed.add_field(name="Account creation date", value=time, inline=False)
  81. if len(user.roles) >= 2:
  82. embed.add_field(name="Roles", value=", ".join([role.mention for role in user.roles if role.name != "@everyone"]), inline=False)
  83. else:
  84. embed.add_field(name="Roles", value="User has no roles", inline=False)
  85. embed.add_field(name="Server join date", value=discord.utils.format_dt(user.joined_at, "R"), inline=False)
  86. embed.add_field(name="User ID", value=user.id)
  87. embed.set_thumbnail(url=user.display_avatar.url)
  88. embed.set_author(name="World Wide Modding", icon_url="https://i.lcpdfrusercontent.com/uploads/monthly_2022_04/756701490_woldwidemodding.thumb.jpg.00bc1f61c05cc6d24519e1dda202d741.jpg")
  89. embed.set_footer(text="World Wide Modding - Bot | Made by BaumSplitter41")
  90. await ctx.respond(embed=embed)
  91. #---------------------------------#
  92. #---------------------------------#
  93. ## Serverinfo
  94. @bot.slash_command(name="serverinfo", description = "Show Informations to this Server")
  95. async def serverinfo(
  96. ctx,
  97. ):
  98. server = ctx.guild
  99. owner = server.owner_id
  100. owner = await bot.fetch_user(owner)
  101. print(owner)
  102. embed = discord.Embed(
  103. title=f"Information about the server __{server.name}__",
  104. description=f"Here you see all details about {server.name}",
  105. color=discord.Color.blue()
  106. )
  107. time = discord.utils.format_dt(server.created_at, "R")
  108. embed.add_field(name="Server creation date", value=time, inline=False)
  109. embed.add_field(name="Owner", value=owner.mention, inline=False)
  110. embed.add_field(name="Member", value=server.member_count, inline=False)
  111. embed.add_field(name="Description", value=server.description, inline=False)
  112. embed.add_field(name="Server ID", value=server.id)
  113. embed.set_thumbnail(url=server.icon)
  114. embed.set_author(name="World Wide Modding", icon_url="https://i.lcpdfrusercontent.com/uploads/monthly_2022_04/756701490_woldwidemodding.thumb.jpg.00bc1f61c05cc6d24519e1dda202d741.jpg")
  115. embed.set_footer(text="World Wide Modding - Bot | Made by BaumSplitter41")
  116. await ctx.respond(embed=embed)
  117. #---------------------------------#
  118. ##Ban System
  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. #logging in #ban-logs on VicePD
  135. channel= discord.utils.get(ctx.guild.channels, id = int(1447580463668400305))
  136. embed = discord.Embed(
  137. title=f"Ban of **{user.name}**",
  138. description=f"User {user.mention} has been banned from the Server",
  139. color=discord.Color.red()
  140. )
  141. time = discord.utils.format_dt(datetime.now(), "f")
  142. embed.add_field(name="Ban Date", value=time, inline=False)
  143. embed.add_field(name="Moderator", value=f"{ctx.author}", inline=False)
  144. embed.add_field(name="Reason", value=reason, inline=False)
  145. embed.add_field(name="User ID", value=user.id)
  146. embed.set_thumbnail(url=user.display_avatar.url)
  147. embed.set_footer(text="World Wide Modding - 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. #TESTs of Modals and Views
  160. class MyModal(discord.ui.Modal):
  161. def __init__(self, *args, **kwargs) -> None:
  162. super().__init__(*args, **kwargs)
  163. self.add_item(discord.ui.InputText(label="Teilnehmende Spieler", placeholder="Gib die Namen der teilnehmenden Spieler abgesehen von dir ein.", required=False))
  164. self.add_item(discord.ui.InputText(label="Benötigte Ausrüstung", placeholder="Gib die benötigte Ausrüstung an (Drogen, Waffen, etc.)."))
  165. self.add_item(discord.ui.InputText(label="Situationsbeschreibung", style=discord.InputTextStyle.long, placeholder="Beschreibe die Situation so detailliert wie möglich (Was? Wo? Wie?)."))
  166. self.add_item(discord.ui.InputText(label="Besonderheiten", placeholder="Langzeitsituationen, besondere Umstände, etc.", required=False))
  167. async def callback(self, interaction: discord.Interaction):
  168. embed_pub = discord.Embed(title="Aktive Situation")
  169. embed_pub.add_field(name="Ersteller", value=interaction.user.mention)
  170. embed_pub.add_field(name="Teilnehmer", value=self.children[0].value)
  171. embed_pub.add_field(name="Long Input", value=self.children[1].value)
  172. await interaction.response.send_message(embeds=[embed_pub])
  173. embed_team = discord.Embed(title="Aktive Situation")
  174. embed_team.add_field(name="Ersteller", value=interaction.user.mention)
  175. embed_team.add_field(name="Teilnehmer", value=self.children[0].value)
  176. embed_team.add_field(name="Benötigte Ausrüstung", value=self.children[1].value)
  177. embed_team.add_field(name="Situationsbeschreibung", value=self.children[2].value)
  178. embed_team.add_field(name="Besonderheiten", value=self.children[3].value)
  179. await interaction.followup.send(embeds=[embed_team], ephemeral=True)
  180. @bot.slash_command()
  181. async def civsitu(ctx: discord.ApplicationContext):
  182. """Shows an example of a modal dialog being invoked from a slash command."""
  183. modal = MyModal(title="Modal via Slash Command")
  184. await ctx.send_modal(modal)
  185. bot.run(token)