[英]Discord Bot Python send a message every hour
I have a problem with discord's bot.我对 discord 的机器人有疑问。 I have done a script which calculate the weather every hour and I want to send the result to discord with bot:
我做了一个每小时计算天气的脚本,我想用机器人将结果发送到 discord:
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?你能帮助我吗?
If you're using python just put it in a while loop and have it send the message and sleep for an hour. 如果您使用的是python,只需将其放入while循环中,然后让它发送消息并休眠一个小时。
for sleep you can import time
睡觉可以
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. 重要提示:您的机器人在睡眠时会100%处于非活动状态,因此,如果您不仅仅将其用于天气,这可能不是您想要的解决方案。
Using the time
module prevents your bot from doing anything else during the time that it is sleeping.使用
time
模块可以防止你的机器人在它睡觉的时候做任何其他事情。
Use tasks.使用任务。 Just put what you want executed in the
task()
function:只需将您要执行的内容放入
task()
function 中即可:
from discord.ext import tasks
@tasks.loop(seconds=60) # repeat once every 60 seconds
async def task():
pass
mytask.start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.