I am recoding my discord.py bot using cogs as it wasnt very nice before.
I tried this code:
import discord
import os
import keep_alive
from discord.ext import commands
from discord.ext.commands import BucketType, cog, BadArgument, command, cooldownintents = discord.Intents().all()
bot = commands.Bot(command_prefix=',', intents=intents)
p = ','class General(commands.Cog):def __init__(self, bot):self.bot = bot@commands.command()async def test(self, ctx):await ctx.channel.send("I'm **alive**.")@commands.command()async def ping(self, ctx):await ctx.send(f'⏱|** {round(bot.latency * 1000)} ms** Latency!')await bot.add_cog(General(bot))if __name__ == '__main__':keep_alive.keep_alive()
bot.run(os.environ['token'])
But I get an error that says “Command ‘ping’ is not found”. What is wrong with the code?
If you use cogs you usually want to a have a main script. In that main script you load your cogs which are located as separate files in another folder. After you loaded your cogs you will run the bot via the main script.
Example for main script you can try to copy pasta it:
Enter your Token.
Enter your Server ID (-> right click on your server -> "Copy Server ID")
Enter your APP ID (-> right click on your Bot -> "Copy User ID")
import discord
from discord.ext import commands
import os SERVER_ID = # ENTER YOUR SERVER ID HERE (AS INT)
APP_ID = # ENTER YOUR APP ID HERE (AS INT)
TOKEN = # ENTER YOUR TOKEN HERE (AS STRING)class MyBot(commands.Bot):def __init__(self):super().__init__(command_prefix='.', intents=discord.Intents.all(), application_id=APP_ID)async def setup_hook(self):for filename in os.listdir('./cogs'): # accessing the cogs folderif filename.endswith('.py'): # checking if cogs inside the folderawait self.load_extension(f'cogs.{filename[:-3]}') # loading the cogsbot.tree.clear_commands(guild=discord.Object(id=SERVER_ID))await bot.tree.sync(guild=discord.Object(id=SERVER_ID)) # syncing the tree in case you use app commandsasync def on_ready(self):synced = await self.tree.sync()print(" Slash CMDs Synced " + str(len(synced)) + " Commands")print(synced)await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='.helpme'))bot = MyBot()
bot.run(TOKEN, reconnect=True)
Then you need to set up your cog folder, for this create a cog folder like this:
Now you need to put a cog script into the cog folder:
Example for your cog script:
import discord
import os
from discord.ext import commands
from discord.ext.commands import BucketType, cog, BadArgument, command, cooldownclass TestCog(commands.Cog):def __init__(self, bot: commands.Bot):self.bot = bot@commands.Cog.listener()async def on_ready(self):print('TestCog loaded.')@commands.command(pass_context=True)async def test(self, ctx):await ctx.channel.send("I'm **alive**.")@commands.command(pass_context=True)async def ping(self, ctx):await ctx.send(f'⏱|** {round(self.bot.latency * 1000)} ms** Latency!')async def setup(bot: commands.Bot):await bot.add_cog(TestCog(bot))
Now save the files and run the main script.
I tested it and it worked for me: