You can create a background task that does this and posts a message to the required channel.
You also need to use asyncio.sleep()
instead of time.sleep()
as the latter is blocking and may freeze and crash your bot.
I've also included a check so that the channel isn't spammed every second that it is 7 am.
discord.py
v2.0
from discord.ext import commands, tasks
import discord
import datetime
time = datetime.datetime.now
class MyClient(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.msg_sent = False
async def on_ready(self):
channel = bot.get_channel(123456789) # replace with channel ID that you want to send to
await self.timer.start(channel)
@tasks.loop(seconds=1)
async def timer(self, channel):
if time().hour == 7 and time().minute == 0:
if not self.msg_sent:
await channel.send('Its 7 am')
self.msg_sent = True
else:
self.msg_sent = False
bot = MyClient(command_prefix='!', intents=discord.Intents().all())
bot.run('token')
discord.py
v1.0
from discord.ext import commands
import datetime
import asyncio
time = datetime.datetime.now
bot = commands.Bot(command_prefix='!')
async def timer():
await bot.wait_until_ready()
channel = bot.get_channel(123456789) # replace with channel ID that you want to send to
msg_sent = False
while True:
if time().hour == 7 and time().minute == 0:
if not msg_sent:
await channel.send('Its 7 am')
msg_sent = True
else:
msg_sent = False
await asyncio.sleep(1)
bot.loop.create_task(timer())
bot.run('TOKEN')