Rem*_*ias 4 python discord discord.py
我正在使用日期时间文件来打印:现在是早上 7 点,每天早上 7 点。现在因为这超出了命令或事件引用,所以我不知道如何以不和谐的方式发送一条消息说现在是早上 7 点。不过需要澄清的是,这不是一个警报,它实际上是针对我的学校服务器的,它会在早上 7 点发送我们需要的所有内容的清单。
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)
Run Code Online (Sandbox Code Playgroud)
这就是早上 7 点触发警报的原因,我只想知道触发此警报时如何发送不和谐的消息。
如果您需要任何澄清,请询问。谢谢!
您可以创建一个后台任务来执行此操作并将消息发布到所需的频道。
您还需要使用asyncio.sleep()而不是time.sleep()因为后者会阻塞并且可能会冻结并崩溃您的机器人。
我还添加了一项检查,以便该频道不会在早上 7 点的每一秒都收到垃圾邮件。
discord.pyv2.0
from discord.ext import commands, tasks
import discord
import datetime
time = datetime.datetime.now
class MyClient(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.msg_sent = False
async def on_ready(self):
channel = bot.get_channel(123456789) # replace with channel ID that you want to send to
await self.timer.start(channel)
@tasks.loop(seconds=1)
async def timer(self, channel):
if time().hour == 7 and time().minute == 0:
if not self.msg_sent:
await channel.send('Its 7 am')
self.msg_sent = True
else:
self.msg_sent = False
bot = MyClient(command_prefix='!', intents=discord.Intents().all())
bot.run('token')
Run Code Online (Sandbox Code Playgroud)
discord.pyv1.0
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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12954 次 |
| 最近记录: |