使用 Discord.py 制作一个在预定日期发送消息的机器人 [英] Making a bot that sends messages at a scheduled date with Discord.py

查看:26
本文介绍了使用 Discord.py 制作一个在预定日期发送消息的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个向特定文本频道发送预定消息的机器人.例如在我生日那天说生日快乐",或者每天早上说早上好".机器人似乎不起作用,因为我的文本频道中没有任何内容.

I'm trying to make a bot that sends a scheduled message to a specific text channel. for example at the date of my birthday saying "happy birthday", or also every morning saying "Good morning". The bot seems to don't work since nothing appears in my text channel.

import discord,random,asyncio,os
from datetime import datetime
from discord.ext import commands

token = '#mytokenhere'
bot=commands.Bot(command_prefix='!')

send_time='01:41' #time is in 24hr format
message_channel_id='0000000000000' #channel ID to send images to

@bot.event
async def on_ready():
    print(bot.user.name)
    print(bot.user.id)

async def time_check():
    await bot.wait_until_ready()
    message_channel=bot.get_channel(message_channel_id)
    while not bot.is_closed:
        now=datetime.strftime(datetime.now(),'%H:%M')
        if now.hour() == 1 and now.minute() == 52:
            message= 'a'
            await message_channel.send(message)
            time=90
        else:
            time=1
        await asyncio.sleep(time)

bot.loop.create_task(time_check())


bot.run('token')

推荐答案

这里是如何使用 tasks 扩展

Here's how you would do something like this using the tasks extension

from discord.ext import commands, tasks

bot = commands.Bot("!")

target_channel_id = 1234

@tasks.loop(hours=24)
async def called_once_a_day():
    message_channel = bot.get_channel(target_channel_id)
    print(f"Got channel {message_channel}")
    await message_channel.send("Your message")

@called_once_a_day.before_loop
async def before():
    await bot.wait_until_ready()
    print("Finished waiting")

called_once_a_day.start()
bot.run("token")

这篇关于使用 Discord.py 制作一个在预定日期发送消息的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆