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

      <small id='1I6FA'></small><noframes id='1I6FA'>

      • <bdo id='1I6FA'></bdo><ul id='1I6FA'></ul>
        <tfoot id='1I6FA'></tfoot><legend id='1I6FA'><style id='1I6FA'><dir id='1I6FA'><q id='1I6FA'></q></dir></style></legend>

        如何在没有命令或事件 discord.py 的情况下发送消

        时间:2023-10-11
      1. <i id='SPh1N'><tr id='SPh1N'><dt id='SPh1N'><q id='SPh1N'><span id='SPh1N'><b id='SPh1N'><form id='SPh1N'><ins id='SPh1N'></ins><ul id='SPh1N'></ul><sub id='SPh1N'></sub></form><legend id='SPh1N'></legend><bdo id='SPh1N'><pre id='SPh1N'><center id='SPh1N'></center></pre></bdo></b><th id='SPh1N'></th></span></q></dt></tr></i><div id='SPh1N'><tfoot id='SPh1N'></tfoot><dl id='SPh1N'><fieldset id='SPh1N'></fieldset></dl></div>
        • <legend id='SPh1N'><style id='SPh1N'><dir id='SPh1N'><q id='SPh1N'></q></dir></style></legend>
            <tbody id='SPh1N'></tbody>
        • <small id='SPh1N'></small><noframes id='SPh1N'>

        • <tfoot id='SPh1N'></tfoot>

                • <bdo id='SPh1N'></bdo><ul id='SPh1N'></ul>
                  本文介绍了如何在没有命令或事件 discord.py 的情况下发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 datetime 文件来打印:现在是早上 7 点,每天早上 7 点.现在因为这超出了命令或事件引用,我不知道如何发送一条不和谐的消息说现在是早上 7 点.只是为了澄清一下,这不是警报,它实际上是针对我的学校服务器的,它会在早上 7 点发送一份我们需要的所有东西的清单.

                  I am using the datetime file, to print: It's 7 am, every morning at 7. Now because this is outside a command or event reference, I don't know how I would send a message in discord saying It's 7 am. Just for clarification though, this isn't an alarm, it's actually for my school server and It sends out a checklist for everything we need at 7 am.

                  import datetime
                  from time import sleep
                  import discord
                  
                  time = datetime.datetime.now
                  
                  
                  while True:
                      print(time())
                      if time().hour == 7 and time().minute == 0:
                          print("Its 7 am")
                      sleep(1)
                  

                  这是早上 7 点触发警报的原因.我只想知道如何在触发时不和谐地发送消息.

                  This is what triggers the alarm at 7 am I just want to know how to send a message in discord when this is triggered.

                  如果您需要任何说明,请询问.谢谢!

                  If you need any clarification just ask. Thanks!

                  推荐答案

                  您可以创建一个后台任务来执行此操作并将消息发布到所需的频道.

                  You can create a background task that does this and posts a message to the required channel.

                  您还需要使用 asyncio.sleep() 而不是 time.sleep(),因为后者会阻塞并且可能会冻结和崩溃您的机器人.

                  You also need to use asyncio.sleep() instead of time.sleep() as the latter is blocking and may freeze and crash your bot.

                  我还添加了一项检查,以便频道不会在早上 7 点每秒都发送垃圾邮件.

                  I've also included a check so that the channel isn't spammed every second that it is 7 am.

                  from discord.ext import commands
                  import datetime
                  import asyncio
                  
                  time = datetime.datetime.now
                  
                  bot = commands.Bot(command_prefix='!')
                  
                  async def timer():
                      await bot.wait_until_ready()
                      channel = bot.get_channel(123456789) # replace with channel ID that you want to send to
                      msg_sent = False
                  
                      while True:
                          if time().hour == 7 and time().minute == 0:
                              if not msg_sent:
                                  await channel.send('Its 7 am')
                                  msg_sent = True
                          else:
                              msg_sent = False
                  
                      await asyncio.sleep(1)
                  
                  bot.loop.create_task(timer())
                  bot.run('TOKEN')
                  

                  这篇关于如何在没有命令或事件 discord.py 的情况下发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:discord.py 中的提醒命令问题 下一篇:不和谐机器人等待消息

                  相关文章

                  最新文章

                        <bdo id='AW3Nw'></bdo><ul id='AW3Nw'></ul>

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

                    3. <small id='AW3Nw'></small><noframes id='AW3Nw'>