Live countdown bot for Discord
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.eventasync 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'))
Did you add the .env file?
If you did, type TOKEN=(your discord bot token here) into the .env file
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: ''
Maybe proofread that line? Idk lol
Have no idea so I post this question :(
I referred to this link for the basic one : https://www.freecodecamp.org/news/create-a-discord-bot-with-python/
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