<legend id='hyfcA'><style id='hyfcA'><dir id='hyfcA'><q id='hyfcA'></q></dir></style></legend>
    1. <small id='hyfcA'></small><noframes id='hyfcA'>

    2. <i id='hyfcA'><tr id='hyfcA'><dt id='hyfcA'><q id='hyfcA'><span id='hyfcA'><b id='hyfcA'><form id='hyfcA'><ins id='hyfcA'></ins><ul id='hyfcA'></ul><sub id='hyfcA'></sub></form><legend id='hyfcA'></legend><bdo id='hyfcA'><pre id='hyfcA'><center id='hyfcA'></center></pre></bdo></b><th id='hyfcA'></th></span></q></dt></tr></i><div id='hyfcA'><tfoot id='hyfcA'></tfoot><dl id='hyfcA'><fieldset id='hyfcA'></fieldset></dl></div>

        <bdo id='hyfcA'></bdo><ul id='hyfcA'></ul>
    3. <tfoot id='hyfcA'></tfoot>

      1. background_task.py 不显示消息 - Python

        时间:2023-09-10
          <tbody id='CpF4f'></tbody>
            <bdo id='CpF4f'></bdo><ul id='CpF4f'></ul>

                <i id='CpF4f'><tr id='CpF4f'><dt id='CpF4f'><q id='CpF4f'><span id='CpF4f'><b id='CpF4f'><form id='CpF4f'><ins id='CpF4f'></ins><ul id='CpF4f'></ul><sub id='CpF4f'></sub></form><legend id='CpF4f'></legend><bdo id='CpF4f'><pre id='CpF4f'><center id='CpF4f'></center></pre></bdo></b><th id='CpF4f'></th></span></q></dt></tr></i><div id='CpF4f'><tfoot id='CpF4f'></tfoot><dl id='CpF4f'><fieldset id='CpF4f'></fieldset></dl></div>
                <legend id='CpF4f'><style id='CpF4f'><dir id='CpF4f'><q id='CpF4f'></q></dir></style></legend>

                <small id='CpF4f'></small><noframes id='CpF4f'>

              1. <tfoot id='CpF4f'></tfoot>

                  本文介绍了background_task.py 不显示消息 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我注意到,当我从 discord.py Github 页面运行代码片段时,它没有显示预期的消息.

                  I noticed that when I ran a code snippet from the discord.py Github page it didn't show the intended message.

                  我稍作修改的代码:

                  import discord
                  import asyncio
                  
                  import nest_asyncio
                  nest_asyncio.apply()
                  
                  class MyClient(discord.Client):
                      def __init__(self, *args, **kwargs):
                          super().__init__(*args, **kwargs)
                  
                          # create the background task and run it in the background
                          self.bg_task = self.loop.create_task(self.my_background_task())
                  
                      async def on_ready(self):
                          print('Logged in as')
                          print(self.user.name)
                          print(self.user.id)
                          print('------')
                  
                      async def my_background_task(self):
                          counter = 0
                          channel = self.get_channel(1234567890) # channel ID goes here
                          while not self.is_closed():
                              counter += 1
                              await channel.send(counter)
                              await asyncio.sleep(10) # task runs every 10 seconds
                  
                  
                  client = MyClient()
                  client.run('token')
                  

                  当我检查 Discord 时没有显示任何内容,但它确实在 IDLE 中显示输出:

                  When I check Discord nothing shows, however it does show output in the IDLE:

                  Logged in as
                  bot_name
                  1234567890
                  ------
                  

                  但在 Discord 服务器中,什么也没有发生.有没有办法解决这个问题?

                  But in the Discord server, nothing happens. Is there anyway to fix this?

                  推荐答案

                  代码失败,因为 self.get_channel(1234567890) 在 bot 正确连接之前使用,导致它总是返回 .这是因为先完成client = MyClient(),即后台任务已创建但bot尚未连接,这是通过client.run完成的.

                  The code fails because self.get_channel(1234567890) is used before the bot has properly connected, resulted in it always returning None. This is because client = MyClient() is done first, meaning the background task is created but the bot has not yet connected, which is done through client.run.

                  要解决此问题,请将循环的创建移至 on_ready 事件内部.

                  To fix this, move the creation of the loop to inside the on_ready event.

                  import discord
                  import asyncio
                  
                  import nest_asyncio
                  nest_asyncio.apply()
                  
                  class MyClient(discord.Client):
                      def __init__(self, *args, **kwargs):
                          super().__init__(*args, **kwargs)
                  
                      async def on_ready(self):
                          print('Logged in as')
                          print(self.user.name)
                          print(self.user.id)
                          print('------')
                  
                          # create the background task and run it in the background
                          self.bg_task = self.loop.create_task(self.my_background_task())
                  
                      async def my_background_task(self):
                          counter = 0
                          channel = self.get_channel(1234567890) # channel ID goes here
                          while not self.is_closed():
                              counter += 1
                              await channel.send(counter)
                              await asyncio.sleep(10) # task runs every 10 seconds
                  
                  
                  client = MyClient()
                  client.run('token')
                  

                  这篇关于background_task.py 不显示消息 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何检查用户是否在 on_message 中具有特定角色? 下一篇:我怎么做才能让你不能用 echo 命令@everyone

                  相关文章

                  最新文章

                    <i id='gjyrK'><tr id='gjyrK'><dt id='gjyrK'><q id='gjyrK'><span id='gjyrK'><b id='gjyrK'><form id='gjyrK'><ins id='gjyrK'></ins><ul id='gjyrK'></ul><sub id='gjyrK'></sub></form><legend id='gjyrK'></legend><bdo id='gjyrK'><pre id='gjyrK'><center id='gjyrK'></center></pre></bdo></b><th id='gjyrK'></th></span></q></dt></tr></i><div id='gjyrK'><tfoot id='gjyrK'></tfoot><dl id='gjyrK'><fieldset id='gjyrK'></fieldset></dl></div>
                  1. <legend id='gjyrK'><style id='gjyrK'><dir id='gjyrK'><q id='gjyrK'></q></dir></style></legend>
                      <bdo id='gjyrK'></bdo><ul id='gjyrK'></ul>

                    <small id='gjyrK'></small><noframes id='gjyrK'>

                    <tfoot id='gjyrK'></tfoot>