So i was working in the cogs of a local hosted discord bot, when i tried to run it it displayed the following error:
async with client:
TypeError: 'Bot' object does not support the asynchronous context manager protocol
The main file:
import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
import asyncio
client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
@client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Escribe "/" para ver los comandos'))
print(f'Bot conectado como {client.user}')
print('ID: {}'.format(client.user.id))
async def load():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
await client.load_extension(f"cogs.{filename[:-3]}")
print(f"{filename[:-3]} está cargado!")
async def main():
async with client:
await load()
await client.run(TOKEN)
asyncio.run(main())
I tried to create a copy and do exactly what the documentation and tutorials did but it remained the same, i tried to update the libraries and didn't work either so i'm pretty much stuck here. Thanks in advance for any insight of my problem. A paste of the full log here
Since I haven't been working with cogs, I tried it myself to see what I could do and found out that you don't necessarily have to load the cogs before you run the bot. This might be a sloppy solution, but it will do the job.
import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
import asyncio
client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
async def load_cogs(client):
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
await client.load_extension(f"cogs.{filename[:-3]}")
print(f"{filename[:-3]} está cargado!")
@client.event
async def on_ready():
await load_cogs(client) # loading the cogs after the bot has initiated
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Escribe "/" para ver los comandos'))
print(f'Bot conectado como {client.user}')
print('ID: {}'.format(client.user.id))
client.run(TOKEN)