Ask coding questions

← Back to all posts
Live countdown bot for Discord
yyd22002 (0)

I'm trying to develope a bot that can live countdown in the channel.

If a user type '!t', then the bot will send a message with 'Timer is running - 5 seconds' and it keeps mentioning exactly the time remaining in the same message every time the time passes by 1 second such "Timer is running - 3 seconds, Timer is running - 1 seconds"....
And when it reaches 0 seconds then the message will be 'Timer ends - 0'

I just google it and found some code to apply this function, but have no idea if it's correct. Could anyone help one this?

import discord
import random
import os
import time

client = discord.Client()

@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
if message.author == client.user:
return

if message.content.startswith('!t'):

    Text = ""
    learn = message.content.split(" ")
    vrsize = len(learn)
    vrsize = int(vrsize)
    for i in range(1, vrsize): 
        Text = Text + " " + learn[i]

    secint = int(Text)
    sec = secint

    for i in range(sec, 0, -1):
        print(i)
        await client.send_message(message.channel, embed=discord.Embed(description='Timer is running : '+str(i)+'second'))
        time.sleep(1)

    else:
        print("GO")
        await client.send_message(message.channel, embed=discord.Embed(description='Timer ends'))

client.run(os.getenv('TOKEN'))

Comments
hotnewtop
Hyderite (1)

Did you add the .env file?

Hyderite (1)

If you did, type TOKEN=(your discord bot token here) into the .env file

yyd22002 (0)

Hi @Hyderite , yes I did but the console has shown the following error msg when I type !t in the channel.
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 27, in on_message
secint = int(Text)
ValueError: invalid literal for int() with base 10: ''

Hyderite (1)

@yyd22002 it means that you have an error on that piece of code

Hyderite (1)

Maybe proofread that line? Idk lol

yyd22002 (0)

Have no idea so I post this question :(

Hyderite (1)

@yyd22002 have you coded a discord bot before? If you haven't, i recommend you to start with the basics instead of making this countdown bot, like making a bot which responds with a specific text when a user types a command

yyd22002 (0)

@Hyderite I did and it worked very well.
I referred to this link for the basic one : https://www.freecodecamp.org/news/create-a-discord-bot-with-python/

Hyderite (1)

Btw, discord.js is more popular than discord.py and has more support, so maybe use discord.js? You can try to join discord.js (or discord.py if you prefer using it) discord servers

Hyderite (1)

@yyd22002 oh, then i'm sorry that i can't help you, I'm more of a discord.js person, try joining discord.py support discord servers or go to stackoverflow and ask for help there ;) again, im sorry

yyd22002 (0)

@Hyderite Can I get an invitation link?