My cog file isn't sending any errors but does not work. A friend of mine suggested importing the cog to my main file. I previously checked on Stackoverflow on how to import it but the method along the line of import (file name here, without extension)
doesn't exactly work. I am using the Pycord library by the way.
import discord
from discord.ext import commands
import os
client = commands.Bot(command_prefix = "!")
client.remove_command('help')
@client.event
async def on_ready():
await client.change_presence(status = discord.Status.do_not_disturb, activity = discord.Game("Being Handy."))
print("Bot is ready!")
## - Cog Loader --------------------------------------------------
cogs = ["cogs.Secondary"]
Start_Decor="----- Connecting Cogs -----\n---------------------------"
End_Decor = "\n---------------------------\n----- Cogs Connected -----"
Fail = "N/A"
Success = False
for cog in cogs:
try:
client.load_extension(cog)
Success = True
except Exception as e:
if Fail == "N/A":
Fail = f'🚫 Failed to connect the following:\n\n{cog}: {str(e)}'
else:
Fail += f'\n{cog}: {str(e)}'
if Success == True and Fail == "N/A":
print(f'{Start_Decor}\n✅ Connecting Cogs\n{End_Decor}')
elif Success == True and not Fail == "N/A":
print(f'{Start_Decor}\n✅ Connecting Cogs\n\n{Fail}\n{End_Decor}')
else:
print(f"{Start_Decor}\n{Fail}\n{End_Decor}")
@client.slash_command()
async def main_test(ctx):
await ctx.respond("Working", ephemeral = False)
## - Code Below --------------------------------------------------
client.run(os.environ.get("BOT_TOKEN"))
^^^ My Main file.
import discord
from discord.ext import commands
from datetime import datetime
class Secondary(commands.Cog):
def __init__(self, client):
self.client = client
@commands.slash_command()
async def cog_test(self, ctx):
await ctx.respond("Working.", ephemeral = False)
def setup(client):
client.add_cog(Secondary(client))
^^^ My cog file.
If you want to import your cog into your discord bot, you have to use the bot.load_extension()
method which takes the file in parameter:
At the end of your main file:
my_files = ["YOUR FILES HERE WITHOUT EXTENSION"]
#For example, if my files are cog1.py and cog2.py and is located in the cogs folder:
my_files = ["cog1", "cog2"]
for file in my_files:
bot.load_extension(f"cogs.{file}")
Don't forget to put the setup function in your cog file
See https://docs.pycord.dev/en/master/api.html?highlight=load_extension#discord.Bot.load_extension