Python How to run script every 30 sec and then send discord msg

Viewed 393

I'm working on a bot that checks specific ether addr and checks token transfers. Everything is done api and stuff but I can't get checking part working. It checks for addrs and it outputs if it changes but how to run it every 30 sec and that output get to discord.

my code:

    import requests, time, json, sys, discord
    result = requests.get('myapi')

    result.json()
    results = "soon:tm:"
    
    def price_of_gas(inp):
        def recursive_function(inp):
            if type(inp) is list:
                for i in inp:
                    ans = recursive_function(i)
                    if ans != None:
                        return ans
            elif type(inp) is dict:
                if 'name' in inp:
                    return inp['name']
                for i in inp:
                    ans = recursive_function(inp[i])
                    if ans != None:
                        return ans
            else:
                return None
        ans = recursive_function(inp)
        return ans if ans else "Could NOT find the new token tx"
        message.channel.send(price_of_gas(result.json()['operations'][0]['tokenInfo']['name']))
     
    
    class MyClient(discord.Client):
        async def on_ready(self):
            print('Logged on as', self.user)
      
        async def on_message(self, message):
            # don't respond to ourselves
            if message.author == self.user:
                return
            if message.content == '.get':
                #send message    
            #checking other commands like '.help'
            

    while True:
        # Code executed here
        print ('done')
        time.sleep(1)
    client = MyClient()
    client.run("mytoken")

Look it like this when script check and if output token, then program to run .get command down there or something like that. i was working on it for 7 hours and i cant get it working.

3 Answers

I'm not sure exactly what you're trying to do, but

while True:
    # Code executed here
    print ('done')
    time.sleep(1)

is going to block any code after this. The loop never ends. Therefore,

client = MyClient()
client.run(btoken)
print ('done no errors 2')

will never run, and your bot will never come online

I already think that you have your answer but I thought maybe it will help you.

There is also a tasks function in discord.py. This is an example

from discord.ext import tasks, commands
    
bot = commands.Bot(command_prefix="!")
    
class TasksCog(commands.Cog):
    def __init__(self, bot) -> None:
        self.bot = bot
        self.change_doing.start()
    
    @tasks.loop(seconds=30)
    async def things_to_do(self):
            .... 
    
    @commands.command()
    async def some_command(self, ctx):
           ...

bot.add_cog(TasksCog(bot))
bot.run("token")

I think you should place this at the top of your code because (like @effprime said) your bot will never be online if its under the infinite while loop:

import requests , time , json , sys , discord
result=requests.get('myapi')

result.json()
results = "soon:tm:"
client = MyClient()
client.run("mytoken")

To answer your main question, which was "How to run script every 30 sec and then send discrod msg", I'd write a similar while loop:

while True:
    # Code executed here
    print ('Done')
    time.sleep(30) #you pause the code for 30 seconds everytime all the code as been executed.
Related