Runtime error not showing when using asyncio to run discord bot
Question:
I’m developing discord bot with discord.py==2.1.0.
I use cog to write the main function that I wanna use, but I found when the whole bot is wrapped in async function and called by asyncio.run()
, my terminal won’t show any error message when there is any runtime error in my cog script.
Here is the example application. I stored my bot token in environment variable.
- bot.py
import os
import discord
from discord.ext import commands
import asyncio
token = os.environ["BOT_TOKEN"]
class Bot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
description = "bot example."
super().__init__(
command_prefix=commands.when_mentioned_or('!'),
intents=intents,
description=description
)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
bot = Bot()
async def load_extensions():
for f in os.listdir("./cogs"):
if f.endswith(".py"):
await bot.load_extension("cogs." + f[:-3])
async def main():
async with bot:
await load_extensions()
await bot.start(token)
asyncio.run(main())
- ./cogs/my_cog.py
from discord.ext import commands
class Test(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("Ready")
@commands.command()
async def command(self, ctx):
test_error_message() # An runtime error example
print("Command")
async def setup(client):
await client.add_cog(Test(client))
- Command that I run in terminal to start the bot.
python bot.py
- When I type
!command
in the discord channel, there is no error message showing in the terminal, but there is no "Command" printed out so I’m sure the code stopped at the line I called test_error_message()
- I expected that it should show error message normally, but I cannot find useful reference to make it work 🙁
- There is one reason I need to use asyncio, I have a task loop to run in the bot, like the code below.
from discord.ext import tasks
@tasks.loop(seconds=10)
async def avatar_update():
# code here
async def main():
async with bot:
avatar_update.start()
await load_extensions()
await bot.start(token)
- I would happy to know if there are some great practice to handle error in this situation.
Thanks!
Answers:
Client.start()
doesn’t configure logging, so if you want to use that then you have to do it yourself (there’s setup_logging
to add a basic config). run()
configures logging for you.
For more info, read the docs. https://discordpy.readthedocs.io/en/stable/logging.html?highlight=logging
I’m developing discord bot with discord.py==2.1.0.
I use cog to write the main function that I wanna use, but I found when the whole bot is wrapped in async function and called by asyncio.run()
, my terminal won’t show any error message when there is any runtime error in my cog script.
Here is the example application. I stored my bot token in environment variable.
- bot.py
import os
import discord
from discord.ext import commands
import asyncio
token = os.environ["BOT_TOKEN"]
class Bot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
description = "bot example."
super().__init__(
command_prefix=commands.when_mentioned_or('!'),
intents=intents,
description=description
)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
bot = Bot()
async def load_extensions():
for f in os.listdir("./cogs"):
if f.endswith(".py"):
await bot.load_extension("cogs." + f[:-3])
async def main():
async with bot:
await load_extensions()
await bot.start(token)
asyncio.run(main())
- ./cogs/my_cog.py
from discord.ext import commands
class Test(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("Ready")
@commands.command()
async def command(self, ctx):
test_error_message() # An runtime error example
print("Command")
async def setup(client):
await client.add_cog(Test(client))
- Command that I run in terminal to start the bot.
python bot.py
- When I type
!command
in the discord channel, there is no error message showing in the terminal, but there is no "Command" printed out so I’m sure the code stopped at the line I calledtest_error_message()
- I expected that it should show error message normally, but I cannot find useful reference to make it work 🙁
- There is one reason I need to use asyncio, I have a task loop to run in the bot, like the code below.
from discord.ext import tasks
@tasks.loop(seconds=10)
async def avatar_update():
# code here
async def main():
async with bot:
avatar_update.start()
await load_extensions()
await bot.start(token)
- I would happy to know if there are some great practice to handle error in this situation.
Thanks!
Client.start()
doesn’t configure logging, so if you want to use that then you have to do it yourself (there’s setup_logging
to add a basic config). run()
configures logging for you.
For more info, read the docs. https://discordpy.readthedocs.io/en/stable/logging.html?highlight=logging