【问题标题】:Python discord bot sends old tweets instead of latest and most relevantPython discord bot 发送旧推文而不是最新和最相关的推文
【发布时间】:2022-01-08 14:35:38
【问题描述】:

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

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)

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

【问题讨论】:

    标签: python discord discord.py python-asyncio tweepy


    【解决方案1】:

    你想让它成为一个返回值的函数,并在每次运行命令时调用它。

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      相关资源
      最近更新 更多