【问题标题】:Python How to run script every 30 sec and then send discord msgPython如何每30秒运行一次脚本然后发送不和谐消息
【发布时间】:2021-04-21 09:49:17
【问题描述】:

我正在开发一个机器人来检查特定的以太地址并检查令牌传输。一切都完成了 api 和东西,但我无法检查部分工作。它检查 addrs 并输出是否更改,但如何每​​ 30 秒运行一次,并且输出不和谐。

我的代码:

    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")

当脚本检查时看起来像这样,如果输出令牌,然后程序在那里运行 .get 命令或类似的东西。我为此工作了 7 个小时,但我无法让它工作。

【问题讨论】:

    标签: python json api discord discord.py


    【解决方案1】:

    我认为你应该把它放在代码的顶部,因为(就像@effprime 所说)如果你的机器人在无限的 while 循环下,它永远不会在线:

    import requests , time , json , sys , discord
    result=requests.get('myapi')
    
    result.json()
    results = "soon:tm:"
    client = MyClient()
    client.run("mytoken")
    

    要回答您的主要问题,即“如何每 30 秒运行一次脚本,然后发送 discrod msg”,我会编写一个类似的 while 循环:

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

    【讨论】:

    • 您不应该在异步代码中使用time.sleep,它会阻塞整个执行线程,换句话说 - 当脚本处于休眠状态时您将无法使用机器人,您应该改用asyncio.sleep
    【解决方案2】:

    我不确定你到底想做什么,但是

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

    将在此之后阻止任何代码。循环永远不会结束。因此,

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

    永远不会运行,你的机器人永远不会上线

    【讨论】:

      【解决方案3】:

      我已经认为你有你的答案,但我想也许它会对你有所帮助。

      discord.py 中还有一个任务函数。 这是一个例子

      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")
      

      【讨论】:

        猜你喜欢
        • 2018-04-25
        • 2021-04-27
        • 2016-07-24
        • 2012-03-26
        • 2013-02-23
        • 2022-01-23
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多