-
-
Save 15696/a1b10f044fbd658ce76ab1f862a1bda2 to your computer and use it in GitHub Desktop.
# main.py | |
from discord.ext import commands | |
import os | |
client = commands.Bot(command_prefix = "!") | |
for f in os.listdir("./cogs"): | |
if f.endswith(".py"): | |
client.load_extension("cogs." + f[:-3]) | |
client.run("token") | |
# cogs / test.py | |
from discord.ext import commands | |
class Test(commands.Cog): | |
def __init__(self, client): | |
self.client = client # sets the client variable so we can use it in cogs | |
@commands.Cog.listener() | |
async def on_ready(self): | |
# an example event with cogs | |
@commands.command() | |
async def command(self, ctx): | |
# an example command with cogs | |
def setup(client): | |
client.add_cog(Test(client)) |
The issue has been fixed. Next time, please don't comment on 2 year old posts. It is honestly starting to get annoying.
why are you being so abrasive?
The issue has been fixed. Next time, please don't comment on 2 year old posts. It is honestly starting to get annoying.
why are you being so abrasive?
The code does not even work anymore seeing as add_cog and load_extension are both coroutines.
The issue has been fixed. Next time, please don't comment on 2 year old posts. It is honestly starting to get annoying.
why are you being so abrasive?
The code does not even work anymore seeing as add_cog and load_extension are both coroutines.
Alrighty buddy
The code does not even work anymore seeing as add_cog and load_extension are both coroutines.
Since it doesn't work what should we use instead?
@Tango0o0o Here is a far better version of the code:
# main.py
from discord.ext import commands
import discord
EXTENSIONS = ("extensions.example",)
INTENTS = discord.Intents.default()
INTENTS.message_content = True
bot = commands.Bot(
intents=INTENTS,
command_prefix="!"
)
@bot.event
async def setup_hook() -> None:
for extension in EXTENSIONS:
await bot.load_extension(extension)
bot.run("token")
# extensions/example.py
from discord.ext import commands
class ExampleCog(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
@commands.Cog.listener()
async def on_ready(self) -> None:
pass
@commands.command()
async def command(self, ctx: commands.Context) -> None:
pass
async def setup(bot: commands.Bot) -> None:
await bot.add_cog(ExampleCog(bot))
I deliberately avoided automatically loading extensions, because it could lead to errors and listing the extensions manually was far simpler. Also, note that extensions and cogs are different and should be treated as such, as you can load an extension without any cogs in it at all.
@bot.event
async def setup_hook() -> None:
for extension in extensions:
await bot.load_extension(extension)
That's what I was missing in all the official and fancy guidelines.
Thank you, sir, you are a true savior!
The issue has been fixed. Next time, please don't comment on 2 year old posts. It is honestly starting to get annoying.