Is there a way I can get this code to update a discord channel reliably every minute?

148 views Asked by At

I found this code on GitHub and am trying to get it to update a voice channel on Discord every minute with the current time. Unfortunately, this code only seems to work every 10 minutes, and then updates the minute after, no matter how many times I restart the code. Not an expert in Python so not entirely sure of all the syntax, but would be super grateful if anyone could walk me through it and tell me what could be going wrong. I'm also hosting it on repl.it if that's relevant.

import discord
import asyncio
import datetime

client = discord.Client()

distoken = "xxxxxx"

# These must all be Voice Channels
timechannel = xxxxxx

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    while True:
        now = datetime.datetime.now()
        await client.get_channel(timechannel).edit(name=f"{now.hour}:{now.minute} (BST)") # The channel gets changed here
        await asyncio.sleep(60)


client.run(distoken)
1

There are 1 answers

0
stijndcl On

To do something every X seconds/minutes/hours, you should use the built-in tasks, which are designed for this very purpose. Tasks work like this:

from discord.ext import tasks

@tasks.loop(seconds=5)
async def my_loop():
    # do_something_here

my_loop.start()