Python discord bot 发送旧推文而不是最新和最相关的推文

Python discord bot sends old tweets instead of latest and most relevant

我一直在用 discord.py 和 python 创建一个不和谐的机器人。我希望机器人自动 post 从 Twitter 检索每日 covid 号码。我通过在服务器中发送“$QLD”来启动机器人,如果 posts 今天的数字,然后休眠 24 小时并再次循环返回 posting 数字。我唯一的问题是它继续 post 与它从原始日期检索到的相同数字。我怎样才能让机器人每天研究推特号码?这是我到目前为止的代码。

import tweepy
import discord
from asyncio import sleep as s

auth = tweepy.OAuthHandler("twitterCodes", "twitterCodes")
auth.set_access_token("twitterCodes", "twitterCodes")

api = tweepy.API(auth)

# a function to get the latest tweet and return it as a string

anna_covid_tweet = api.user_timeline(screen_name="annastaciaMP", count=3, tweet_mode="extended", include_rts=False)
for info in anna_covid_tweet:
    if "coronavirus" in info.full_text:
        covidnum = info.full_text
    else:
        print("No tweet found")

#discord bot stuff

client = discord.Client()
bottoken = 'discord bot token'

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

@client.event
async def on_message(message):
  while message.content.startswith('$QLD'):
    await message.channel.send('These are the QLD covid numbers for today!')
    await message.channel.send(covidnum)
    await s(86400) - 86400 is 24hours in seconds fyi


client.run(bottoken)

提前谢谢你。非常感谢您的帮助。

您想使它成为一个 returns 值的函数,并在每次命令为 运行 时调用它。

import tweepy
import discord
from asyncio import sleep as s

auth = tweepy.OAuthHandler("twitterCodes", "twitterCodes")
auth.set_access_token("twitterCodes", "twitterCodes")

api = tweepy.API(auth)

# create a function that returns the text
def get_covidnum():
    anna_covid_tweet = api.user_timeline(screen_name="annastaciaMP", count=3, 
    tweet_mode="extended", include_rts=False)
    for info in anna_covid_tweet:
        if "coronavirus" in info.full_text:
            return info.full_text
        else:
            print("No tweet found")
            return "No tweet found"

#discord bot stuff

client = discord.Client()
bottoken = 'discord bot token'

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

@client.event
async def on_message(message):
  while message.content.startswith('$QLD'):
    await message.channel.send('These are the QLD covid numbers for today!')
    await message.channel.send(get_covidnum()) # call the function
    await s(86400) - 86400 is 24hours in seconds fyi


client.run(bottoken)