Discord bot, current date as status

Question:

i have looked a bit and tried multiple things and im stumped. Im going to be hosting a discord bot 24/7 and i want the Status to display the current date and time, as example. 11/30/22, 10:51 PM, in eastern time. Thanks!

tried methods such as "
activity=discord.Game(datetime.datetime.utcnow().strftime("%H:%M")),"

Asked By: Loverrr

||

Answers:

You can use tasks.loop, creating a task that updates every minute and changes the bot’s Status to the current time.

tasks.loop is a decorator which executes the decorated function repeatedly at a defined interval. Then you just need to spawn the loop in an asynchronous context, of which I personally use setup_hook in this example.

from discord.ext import tasks
import datetime

@tasks.loop(minutes=1):
async def set_status(): # name is unimportant, but it must not take arguments
  name = datetime.datetime.utcnow().strftime("%H:%M")
  activity = discord.Game(name=name)
  await client.change_presence(activity=activity)

@client.event
async def setup_hook(): # name is important, and it must not take arguments
  set_status.start()

Replacing client with the name of your client as appropriate (usually client or bot).

Answered By: Mous
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.