'Discord Bot Python send a message every hour
I have a problem with discord's bot. I have done a script which calculate the weather every hour and I want to send the result to discord with bot:
import Secret
import discord
result = "temp is ...."
TOKEN = Secret.BOT_TOKEN
client = discord.Client()
client.send(result)
client.run(TOKEN)
I have searched on google but I have not found an answer to send the result automatically.
Can you help me?
Solution 1:[1]
If you're using python just put it in a while loop and have it send the message and sleep for an hour.
for sleep you can import time
Something like:
while true:
client.send(result)
sleep(3600)
Important: Your bot will be 100% inactive while you sleep it, so if you use it for more than just weather this might not be the solution you're looking for.
Solution 2:[2]
Using the time
module prevents your bot from doing anything else during the time that it is sleeping.
Use tasks. Just put what you want executed in the task()
function:
from discord.ext import tasks
@tasks.loop(seconds=60) # repeat once every 60 seconds
async def task():
pass
mytask.start()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Nathan Dai |