在一天中的某些时间禁用discord.py命令 [英] Disable discord.py command at certain times of day

查看:26
本文介绍了在一天中的某些时间禁用discord.py命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tts机器人,我想使用命令打开和关闭,或者在我睡觉时从晚上11点至早上7点自动关闭.这可能吗?

I have a tts bot that I want to either toggle on and off with a command, or be automatically turned of from 11pm - 7am when i'm sleeping. Is this possible?

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.')

推荐答案

您可以制作一个循环,该循环每24小时进行一次循环,该循环会将 enabled_tts 变量更改为False,在命令中,检查是否该变量设置为True

You can make a loop, that iterates every 24 hours that will change the enabled_tts variable to False, in the command, check if the variable is set to 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.

Here's an example on how to set the variable to False at 11pm, try to figure out how to set it to True at 7am.

参考:

这篇关于在一天中的某些时间禁用discord.py命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆