Hey everyone! I’m working on a Discord bot and I want it to switch between two status messages every 10 seconds. I’ve tried using threading, but I’m getting an error. Here’s what I’ve got so far:
@bot.event
async def on_ready():
print('Bot is online!')
await update_status()
@bot.event
async def update_status():
while True:
await bot.change_presence(activity=discord.Game(name=f'Active on {len(bot.guilds)} servers'))
await asyncio.sleep(10)
await bot.change_presence(activity=discord.Game(name='Type .help for commands'))
await asyncio.sleep(10)
When I run this, I get an error saying the coroutine was never awaited. Can someone help me figure out how to make this work? I want the bot to keep running other functions while changing its status. Thanks!
yo, i had this issue too. try using discord.ext.tasks, it’s way easier. something like this:
from discord.ext import tasks
@tasks.loop(seconds=10)
async def change_status():
await bot.change_presence(activity=discord.Game(name='status 1'))
await asyncio.sleep(5)
await bot.change_presence(activity=discord.Game(name='status 2'))
@bot.event
async def on_ready():
change_status.start()
this should work without any errors. good luck!
I’ve dealt with this exact problem before. The key is to use tasks instead of events for continuous background processes. Here’s what worked for me:
import discord
from discord.ext import tasks
@tasks.loop(seconds=10)
async def change_status():
statuses = [
f'Active on {len(bot.guilds)} servers',
'Type .help for commands'
]
await bot.change_presence(activity=discord.Game(name=statuses[change_status.current_loop % 2]))
@bot.event
async def on_ready():
print('Bot is online!')
change_status.start()
This method uses discord.ext.tasks to create a loop that runs every 10 seconds. It’s more efficient and doesn’t block other bot functions. The modulo operator (%) helps alternate between the two status messages. Just make sure to import the necessary modules and you should be good to go!
I encountered a similar issue when developing my Discord bot. The problem lies in how you’re calling the update_status function. Instead of using @bot.event, you should create a separate async function and call it in your on_ready event. Here’s a corrected version that should work:
@bot.event
async def on_ready():
print('Bot is online!')
bot.loop.create_task(update_status())
async def update_status():
while True:
await bot.change_presence(activity=discord.Game(name=f'Active on {len(bot.guilds)} servers'))
await asyncio.sleep(10)
await bot.change_presence(activity=discord.Game(name='Type .help for commands'))
await asyncio.sleep(10)
This approach uses bot.loop.create_task() to run the update_status function concurrently with other bot operations. It should resolve the ‘coroutine never awaited’ error and allow your bot to switch status messages every 10 seconds as intended.