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

2024-04-25 09:38:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个机器人,以促进我们团队现有的一些工作。有了这些,我开始创建一些运行良好的任务,我需要创建一个从周一到周五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)

Tags: fromimportclientsendasyncdefchannelawait
2条回答

对于那些有疑问的人,我按照上面的提示发出了此警报,因此我在bot.loop.create_任务(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)

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

然后,如果是正确的日期和时间,您可以运行该函数

相关问题 更多 >