Advertisement
Frostyy22

main.py

Aug 7th, 2023
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. from discord.ext import commands
  2. from dotenv import load_dotenv
  3. import os
  4. import asyncio
  5. import tkinter as tk
  6. from threading import Thread
  7.  
  8. load_dotenv()
  9.  
  10. DISCORD_DARK = 0x36393F
  11. DISCORD_GREY = 0x2C2F33
  12. DISCORD_GREEN = 0x57F287
  13. DISCORD_RED = 0xED4245
  14. DISCORD_YELLOW = 0xFEE75C
  15.  
  16. initial_extensions = [
  17.     'cogs.ping',
  18.     'cogs.whois',
  19.     'cogs.info',
  20.     'cogs.help',
  21.     'cogs.msg',
  22.     'cogs.rip',
  23.     'cogs.prefix',
  24.     'cogs.meme',
  25.     'cogs.startcod',
  26.     'cogs.ww2',
  27.     'cogs.helpsys',
  28.     'cogs.fanmade_cards',
  29. ]
  30.  
  31. class Bot(commands.Bot):
  32.     def __init__(self, *args, **kwargs):
  33.         super().__init__(*args, **kwargs)
  34.         self.is_running = False
  35.         self.is_ready = False  # New attribute to track readiness
  36.  
  37.     async def on_ready(self):
  38.         self.is_ready = True  # Set the readiness flag when the bot is ready
  39.         status_label.config(text="Status: On", fg="green")  # Update the status label
  40.  
  41.     async def start_bot(self):
  42.         TOKEN = os.getenv("DISCORD_TOKEN")
  43.         await self.start(TOKEN)
  44.  
  45.     async def stop_bot(self):
  46.         await self.close()
  47.  
  48. async def run_bot(bot):
  49.     for extension in initial_extensions:
  50.         await bot.load_extension(extension)
  51.  
  52.     ### Command Not Found
  53.     @bot.event
  54.     async def on_command_error(ctx, error):
  55.         if isinstance(error, commands.CommandNotFound):
  56.             await ctx.send("Command not found. Please enter a valid command.")
  57.  
  58.     await bot.start_bot()
  59.  
  60. def start_bot_thread(bot):
  61.     loop = asyncio.new_event_loop()
  62.     asyncio.set_event_loop(loop)
  63.     try:
  64.         loop.run_until_complete(run_bot(bot))
  65.     finally:
  66.         loop.close()
  67.  
  68. def toggle_bot():
  69.     global bot
  70.     if bot.is_running:
  71.         bot.loop.call_soon_threadsafe(bot.loop.create_task, bot.stop_bot())
  72.         bot.loop.call_soon_threadsafe(bot.loop.stop)
  73.         bot.loop.call_soon_threadsafe(bot.loop.close)
  74.         bot.is_running = False
  75.         status_label.config(text="Status: Off", fg=f"#{DISCORD_RED:06X}")
  76.         start_stop_button.config(text="Start DEclipse", bg=f"#{DISCORD_GREEN:06X}")
  77.     else:
  78.         bot_thread = Thread(target=start_bot_thread, args=(bot,))
  79.         bot_thread.start()
  80.         bot.is_running = True
  81.         status_label.config(text="Status: Connecting", fg=f"#{DISCORD_YELLOW:06X}")
  82.         start_stop_button.config(text="Stop DEclipse", bg=f"#{DISCORD_RED:06X}")
  83.  
  84. bot = Bot(command_prefix='!', self_bot=True, help_command=None)
  85.  
  86. root = tk.Tk()
  87. root.title("DEclipse")
  88. root.geometry("500x300")
  89. root.configure(bg=f"#{DISCORD_DARK:06X}")
  90.  
  91. start_stop_button = tk.Button(root, text="Start DEclipse", command=toggle_bot, bg=f"#{DISCORD_GREEN:06X}")
  92. start_stop_button.pack(pady=20)
  93.  
  94. status_label = tk.Label(root, text="Status: Off", fg=f"#{DISCORD_RED:06X}", bg=f"#{DISCORD_DARK:06X}")
  95. status_label.pack()
  96.  
  97. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement