好的,所以我希望它在数据库中发生数字更改或更新时更新状态文本。 因为我想显示有多少人在线,我得到了一个 PHP 函数,我用 python 中的请求调用它,但是如果自上次以来它发生了变化,它将状态从 1 online 更新为 2 online for例子?
@bot.event
async def on_ready():
while True:
response = json.loads(requests.get(weburl + f"/api/index.php?totalonline").text)
string = "Currently " + str(response["message"]) + " online"
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=string))
答案 0 :(得分:4)
不要为此使用 while
循环。而是使用 tasks
。定义一个每秒钟重复一次的任务,并在 on_ready
事件中启动它。
示例:
import discord
from discord.ext import commands, tasks
bot = commands.Bot(command_prefix=".")
@bot.event
async def on_ready():
if not loop.is_running():
loop.start()
@tasks.loop(seconds=1)
async def loop():
print("I'm running!")
# your repetitive task
bot.run("TOKEN")