Skip to content

Instantly share code, notes, and snippets.

@Wamy-Dev
Last active January 23, 2023 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wamy-Dev/cc5505923555ed8e69c8cc1f5d64b481 to your computer and use it in GitHub Desktop.
Save Wamy-Dev/cc5505923555ed8e69c8cc1f5d64b481 to your computer and use it in GitHub Desktop.
DIscordPy-2.0 Bot Template
import requests
import os
import urllib3
import logging
from logging import getLogger
from quart.logging import default_handler
getLogger('quart.serving').removeHandler(default_handler)
getLogger('quart.serving').setLevel(logging.ERROR)
session = requests.Session()
session.verify = False
urllib3.disable_warnings()
import discord
from discord.ext import commands
from quart import Quart, render_template
from decouple import config
import firebase_admin
from firebase_admin import credentials
import asyncio
from threading import Thread
import requests
from datetime import datetime
from humanfriendly import format_timespan
#quart
app = Quart(__name__)
ready = False
inittime = datetime.now()
@app.route("/", methods = ["get"])
async def index():
while ready:
# Anything can go here. THis can be used for status page logic, or really anything else. Wil show the other page if the bot
# is still booting up.
return await render_template("index.html")
else:
return await render_template("notready.html")
#discord
CLIENTTOKEN = config('CLIENTTOKEN')
intents = discord.Intents.default()
client = commands.AutoShardedBot(command_prefix = '>', intents=intents)
client.remove_command('help')
@client.event
async def on_ready():
print(f'Bot is ready. Logged in as {client.user}(ID: {client.user.id})')
print(f'Shards: {client.shard_count}')
await client.change_presence(activity = discord.Activity(type = discord.ActivityType.watching, name = "/help"))
global ready
ready = True
# Reload cog command ONLY for development.
# @client.tree.command(name="reload", description="Reloads a cog.")
# async def reload(interaction: discord.Interaction, cog: str = None):
# try:
# await client.reload_extension(f"cogs.{cog}")
# await interaction.response.send_message(f"Reloaded {cog}")
# await client.tree.sync()
# except Exception as e:
# await interaction.response.send_message(f"{e}")
class async_discord_thread(Thread):
# thanks @FrankWhoee for this code snippet
def __init__(self):
Thread.__init__(self)
self.loop = asyncio.get_event_loop()
self.start()
async def starter(self):
async def load_extensions():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
await client.load_extension(f"cogs.{filename[:-3]}")
await load_extensions()
await client.start(CLIENTTOKEN)
def run(self):
self.name = 'Discord.py'
self.loop.create_task(self.starter())
self.loop.run_forever()
discord_thread = async_discord_thread()
app.run(host="0.0.0.0", port="5001")

Welcome to my DiscordPy Bot template. This is a template that I used for any Discord bots that I write.

Uses DiscordPy 2.0 and features slash commands, cogs, and a WebUI that goes online when the bot is online. You can do anything you want with this template.

To set up, add all commands in /cogs, and add html pages in /templates. Then add a .env file with your CLIENTTOKEN from Discord in the root folder. Then run the bot.py file and it will work perfectly. I use this template for all my Discord bots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment