app.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import discord
  2. """ Declare intents that the bot will use """
  3. intents = discord.Intents.default()
  4. intents.emojis_and_stickers = True
  5. intents.guilds = True
  6. intents.integrations = True
  7. intents.message_content = True
  8. intents.messages = True
  9. intents.members = True
  10. intents.presences = True
  11. intents.reactions = True
  12. intents.voice_states = True
  13. """ Load the bot token """
  14. from dotenv import load_dotenv
  15. from os import getenv
  16. load_dotenv()
  17. TOKEN = getenv("BOT_TOKEN")
  18. GUILD = int(getenv("GUILD_ID"))
  19. """ Initialize the bot """
  20. from discord.ext.commands import Bot, when_mentioned_or
  21. bot = Bot(
  22. command_prefix=when_mentioned_or('..', '>', '.'),
  23. description="A list of commands available",
  24. intents=intents,
  25. # debug_guilds=[GUILD],
  26. max_messages=100_000
  27. )
  28. @bot.event
  29. async def on_ready():
  30. print(f"Logged in as {bot.user} (ID: {bot.user.id})")
  31. print("------")
  32. """ Load cogs """
  33. from os import listdir
  34. def list_all_cogs() -> list[str]:
  35. return [file[:-3] for file in listdir("cogs") if file.endswith(".py")]
  36. for cog in list_all_cogs():
  37. bot.load_extension(f"cogs.{cog}")
  38. # TODO: web server for jukebox and/or soundboard?
  39. # TODO: cog to announce when someone joins vc (maybe delete messages after some time?)
  40. # TODO: purge messages from a user / etc? links, number, bot or human, idk
  41. # TODO: automod? blocklist certain words or urls or whatever
  42. # TODO: warn or kick or ban a user?
  43. # TODO: filter the audit logs for a user? maybe?
  44. # TODO: keep stats or levels? ehhhh
  45. # ================================= ADMIN ======================================
  46. from discord.commands import Option
  47. from discord.ext.commands import Context
  48. from discord import AutocompleteContext, ApplicationContext
  49. async def cog_autocomplete(ctx: AutocompleteContext):
  50. return [cog for cog in list_all_cogs() if cog.lower().startswith( ctx.value.lower() )]
  51. ROLE_ADMIN = 518625964763119616
  52. ROLE_ADMIN_SFW = 727205354353721374
  53. ME = 201046736565829632
  54. def allowed_to_reload(ctx: Context | ApplicationContext):
  55. roles = [role.id for role in ctx.author.roles]
  56. admin = ROLE_ADMIN in roles
  57. sfw_admin = ROLE_ADMIN_SFW in roles
  58. owner = ctx.author.id == ME
  59. return any([admin, sfw_admin, owner])
  60. from cogs.music import Music
  61. def reload_music(ctx):
  62. music: Music = bot.get_cog("Music")
  63. q = music.q
  64. track = music.track
  65. repeat_mode = music.repeat_mode
  66. search_results = music.search_results
  67. bot.reload_extension(f"cogs.music")
  68. music = bot.get_cog("Music")
  69. music.q = q
  70. music.track = track
  71. music.repeat_mode = repeat_mode
  72. music.search_results = search_results
  73. @bot.command(name='reload')
  74. async def reload_prefix(ctx: Context, cog: str = None):
  75. """Reload an extension (admin command)"""
  76. if not allowed_to_reload(ctx):
  77. return await ctx.send(
  78. "You must be an admin or bot owner to use this command"
  79. )
  80. if not cog:
  81. return await ctx.send("Please specify a cog to reload")
  82. elif cog.lower() == "music":
  83. reload_music(ctx)
  84. else:
  85. bot.reload_extension(f"cogs.{cog}")
  86. await ctx.send(f"Reloaded `{cog}` extension")
  87. @bot.slash_command(
  88. name='reload',
  89. guild_ids=[GUILD],
  90. )
  91. async def reload_slash(
  92. ctx: ApplicationContext,
  93. cog: Option(str, "The cog to be reloaded", autocomplete=cog_autocomplete)
  94. ):
  95. """Reload an extension (admin command)"""
  96. if not allowed_to_reload(ctx):
  97. return await ctx.respond(
  98. "You must be an admin or bot owner to use this command",
  99. ephemeral=True
  100. )
  101. if cog == "music":
  102. reload_music(ctx)
  103. else:
  104. bot.reload_extension(f"cogs.{cog}")
  105. await ctx.respond(f"Reloaded `{cog}` extension", ephemeral=True)
  106. # ================================== END =======================================
  107. """ Run the bot """
  108. bot.run(TOKEN)