首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在一天中的某些时间禁用discord.py命令

在一天中的某些时间禁用discord.py命令
EN

Stack Overflow用户
提问于 2020-11-25 17:28:45
回答 1查看 346关注 0票数 0

我有一个tts机器人,我想要用一个命令来切换和关闭,或者在我睡觉的时候从晚上11点到早上7点自动关闭。这个是可能的吗?

TTS代码:

代码语言:javascript
复制
@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.')
EN

回答 1

Stack Overflow用户

发布于 2020-11-25 18:35:17

您可以创建一个循环,该循环每24小时迭代一次,将enabled_tts变量更改为False。在命令中,检查该变量是否设置为True

代码语言:javascript
复制
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

参考资料:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65001832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档