discord.py 在特定时间和日期发送消息

我正在开发一个机器人来促进我们团队手头的一些事情。我开始创建一些运行良好的任务,我需要创建一个从周一到周五 16 点运行的通知,我看到了一些博客,我尝试申请但没有成功,今天这是我的代码和功能我想调用称为“LEMBRAR()”,我想知道我必须运行它的选项。


import discord

from discord.ext import commands, tasks

from discord.ext.commands import has_permissions

from BotGooBee.Humor import GooBee


hora = '16:00'

diasSemanas = 'seg-sex'


client = commands.Bot(command_prefix='?')


@client.event

async def on_ready():

    print('bot online')


@client.command()

async def limpar(ctx, amount=100):

    await ctx.channel.purge(limit=amount)


@client.command()

async def ping(ctx):

    await ctx.send(f'Pong! {round(client.latency * 1000)}ms')


@client.command()

async def feliz(ctx):

    GooBee(1).AtualizarHumor()

    await ctx.send('Humor alterado | FELIZ')

    


@client.command()

async def normal(ctx):

    GooBee(2).AtualizarHumor()

    await ctx.send('Humor alterado | NORMAL')


@client.command()

async def irritado(ctx):

    GooBee(3).AtualizarHumor()

    await ctx.send('Humor alterado | IRRITADO')



async def lembrar():

    print('hello')

    channel = client.get_channel(id_channel)

    await channel.send('hello')

    

client.run(token)


慕容708150
浏览 179回答 2
2回答

潇湘沐

您可以像这样使用 datetime 模块来获取当前时间,然后检查时间是否正确,然后运行该函数。import datetime# Gets the weekday and returns a number: 0 for monday : 6 for sundayprint(datetime.datetime.today().weekday())# Gets the current timeprint(datetime.datetime.now().time())然后,如果日期和时间正确,您就可以运行该函数。

隔江千里

对于那些有疑问的人,我按照上面的提示发出了这个警报,所以我在最后添加了一个条件 bot.loop.create_task (my_def ())import discordfrom discord.ext import commands, tasksfrom discord.ext.commands import has_permissionsfrom BotGooBee.Humor import GooBeeimport asyncioimport jsonimport randomimport datetimebot = commands.Bot(command_prefix='?')with open('frases.json', 'r') as json_file:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dados = json.load(json_file)@bot.eventasync def on_ready():&nbsp; &nbsp; print('bot online')@bot.command()async def limpar(ctx, amount=100):&nbsp; &nbsp; await ctx.channel.purge(limit=amount)@bot.command()async def ping(ctx):&nbsp; &nbsp; await ctx.send(f'Pong! {round(bot.latency * 1000)}ms')@bot.command()async def feliz(ctx):&nbsp; &nbsp; GooBee(1).AtualizarHumor()&nbsp; &nbsp; await ctx.send('Humor alterado | FELIZ')&nbsp; &nbsp;&nbsp;@bot.command()async def normal(ctx):&nbsp; &nbsp; GooBee(2).AtualizarHumor()&nbsp; &nbsp; await ctx.send('Humor alterado | NORMAL')@bot.command()async def irritado(ctx):&nbsp; &nbsp; GooBee(3).AtualizarHumor()&nbsp; &nbsp; await ctx.send('Humor alterado | IRRITADO')#funcao que faz o alerta da mensagemasync def AlerteHumor():&nbsp; &nbsp; await bot.wait_until_ready()&nbsp; &nbsp; while not bot.is_closed():&nbsp; &nbsp; &nbsp; &nbsp; print('alerta humor')&nbsp; &nbsp; &nbsp; &nbsp; hora = int(datetime.datetime.now().time().strftime("%H"))&nbsp; &nbsp; &nbsp; &nbsp; minutos = int(datetime.datetime.now().time().strftime("%M"))&nbsp; &nbsp; &nbsp; &nbsp; if hora == 16 and minutos <= 59:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel = bot.get_channel(channel_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await channel.send(dados[f'{random.randrange(1,5)}'])&nbsp; &nbsp; &nbsp; &nbsp; await asyncio.sleep(3600)bot.loop.create_task(AlerteHumor())bot.run(token)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python