Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I have been creating a discord bot with discord.py and python. I want the bot to automatically post daily covid numbers retrieved from twitter. I start the bot by sending ‘$QLD’ in the server, if posts todays numbers, then sleeps for 24 hours and loops back posting the numbers again. My only issue is that is continues to post the same numbers that it retrieved from the original day. How can i make it so the bot will research for the twitter numbers daily? Here is the code i have so far.

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)

Thank you in advance. Your help is appreciated a lot.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You want to make it a function that returns the value and call it every time the command is run.

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading