2 回答
TA贡献1816条经验 获得超6个赞
您可以像这样使用 datetime 模块来获取当前时间,然后检查时间是否正确,然后运行该函数。
import datetime
# Gets the weekday and returns a number: 0 for monday : 6 for sunday
print(datetime.datetime.today().weekday())
# Gets the current time
print(datetime.datetime.now().time())
然后,如果日期和时间正确,您就可以运行该函数。
TA贡献1906条经验 获得超10个赞
对于那些有疑问的人,我按照上面的提示发出了这个警报,所以我在最后添加了一个条件 bot.loop.create_task (my_def ())
import discord
from discord.ext import commands, tasks
from discord.ext.commands import has_permissions
from BotGooBee.Humor import GooBee
import asyncio
import json
import random
import datetime
bot = commands.Bot(command_prefix='?')
with open('frases.json', 'r') as json_file:
dados = json.load(json_file)
@bot.event
async def on_ready():
print('bot online')
@bot.command()
async def limpar(ctx, amount=100):
await ctx.channel.purge(limit=amount)
@bot.command()
async def ping(ctx):
await ctx.send(f'Pong! {round(bot.latency * 1000)}ms')
@bot.command()
async def feliz(ctx):
GooBee(1).AtualizarHumor()
await ctx.send('Humor alterado | FELIZ')
@bot.command()
async def normal(ctx):
GooBee(2).AtualizarHumor()
await ctx.send('Humor alterado | NORMAL')
@bot.command()
async def irritado(ctx):
GooBee(3).AtualizarHumor()
await ctx.send('Humor alterado | IRRITADO')
#funcao que faz o alerta da mensagem
async def AlerteHumor():
await bot.wait_until_ready()
while not bot.is_closed():
print('alerta humor')
hora = int(datetime.datetime.now().time().strftime("%H"))
minutos = int(datetime.datetime.now().time().strftime("%M"))
if hora == 16 and minutos <= 59:
channel = bot.get_channel(channel_id)
await channel.send(dados[f'{random.randrange(1,5)}'])
await asyncio.sleep(3600)
bot.loop.create_task(AlerteHumor())
bot.run(token)
添加回答
举报