2 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以像這樣使用 datetime 模塊來獲取當(dāng)前時(shí)間,然后檢查時(shí)間是否正確,然后運(yùn)行該函數(shù)。
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())
然后,如果日期和時(shí)間正確,您就可以運(yùn)行該函數(shù)。

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊
對(duì)于那些有疑問的人,我按照上面的提示發(fā)出了這個(gè)警報(bào),所以我在最后添加了一個(gè)條件 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)
添加回答
舉報(bào)