我有一个tts机器人,我想要用一个命令来切换和关闭,或者在我睡觉的时候从晚上11点到早上7点自动关闭。这个是可能的吗?
TTS代码:
@client.command()
async def tts(ctx, *, msg):
print('{0} : {1}'.format(ctx.author, msg))
print('TTS started {}'.format(msg))
os.system('flite -t "{}"'.format(msg))
await ctx.send('TTS done.')发布于 2020-11-25 18:35:17
您可以创建一个循环,该循环每24小时迭代一次,将enabled_tts变量更改为False。在命令中,检查该变量是否设置为True
import asyncio
from datetime import datetime, timedelta
from discord.ext import tasks
enabled_tts = True
def delta(hour, minute):
"""Returns in how many seconds is
going to be the specified hour"""
now = datetime.now()
future = datetime(now.year, now.month, now.day, hour, minute)
if now.hour >= hour and now.minute > minute:
future += timedelta(days=1)
return (future - now).seconds
@tasks.loop(hours=24)
async def disable_command():
"""Disables TTS every 24 hours"""
global enabled_tts
enabled_tts = False
print('TTS disabled')
@disable_command.before_loop
async def before_disable_command():
"""This basically delays the `disable_command` loop to start at
the defined hour"""
hour, minute = 23, 00
seconds = delta(hour, minute)
await asyncio.sleep(seconds)
# Also make sure to start the loop on the `on_ready` event
@client.event
async def on_ready():
await client.wait_until_ready()
print(f'Logged in as {client.user}')
disable_command.start()这里有一个关于如何在晚上11点将变量设置为False的示例,尝试弄清楚如何在上午7点将其设置为True
参考资料:
https://stackoverflow.com/questions/65001832
复制相似问题