Issue
so before everyone screams at me saying there already is a response, no there is not. Ive tried every stack overflow post about this and none fix my problem. My code:
my main.py (main)
`
import discord
import os
from discord.ext import commands
import asyncio
intents = discord.Intents.all()
intents.members = True
client = commands.Bot(command_prefix=".", intents=intents, help_command=None)
@client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
client.run('TOKEN')
`
I took this code from a youtube video, that is outdated, I tried some others with their code working, except they cant unload and load cogs! I really want this to work as i want to disable and enable features.
my example.py (cog)
`
import discord
from discord.ext import commands
class Example(commands.Cog):
def __init__(self, client):
self.client = client
# Events
@commands.Cog.listener()
async def on_ready(self):
print('Bot is online.')
#Commands
@commands.command()
async def ping(self, ctx):
await ctx.send('Pong!')
def setup(client):
client.add_cog(Example(client))
`
I have tried adding await to the def setup client, changing it into `
async def main():
await load()
await bot.start("MYTOKEN")
asyncio.run(main())
this doesnt work either. I have also tried putting line 18,19,20 (`
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
`)
into a async def and adding await, but that also just adds more errors. The bot goes online but obviously the command .ping doesnt work as its not loaded. When i run the program i get the coroutine error aswell as when i try to .load example. I know how to make a cog that works, just I want to make one that i can load and reload so that you can disable features. Any help appreaciated
Solution
Extensions are now asynchronous. The migration guide explains what to change in order to make the switch: https://discordpy.readthedocs.io/en/stable/migrating.html#extension-and-cog-loading-unloading-is-now-asynchronous
You're not awaiting async functions, so the solution is to... await them...
"I tried this but it just adds more errors" is not very useful to people here to help figure out the problem. You should say what those errors are...
And stop watching YouTube videos for discord.py. All of them are outdated, and teach bad code practices. Just read the docs.
Answered By - stijndcl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.