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

时间:2020-08-13 15:44:20

标签: python 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)

2 个答案:

答案 0 :(得分:0)

您可以像这样使用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())

然后在合适的日期和时间运行该功能。

答案 1 :(得分:-1)

对于那些有疑问的人,按照上面的提示,我设法发出了警报,因此我在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)