Reputation: 11
I am working on a discord bot in python that send a scheduled message in a server at very specific times every day as well as other functionalities. After implementing the scheduled message, the bot commands no longer work - so the users can't use any of the commands but the scheduled message works.
I have the bot set up so it sends a message at 12:00,19:00,21:00,22:00,23:00 UTC. The only problem is now the commands such as ";help" do not work.
import discord
import datetime
import asyncio
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix = ';',help_command=None) #prefix
race_times_utc = [12,19,21,22,23]
time = datetime.datetime.now
async def timer():
await client.wait_until_ready()
msg_sent = False
while not client.is_closed():
if any(time().hour == race for race in race_times_utc) and time().minute == 0:
if not msg_sent:
channel = client.get_channel(838626115326636022)
racer_role = get(channel.guild.roles, name = 'Racers')
embed = discord.Embed(
title = 'Race Time',
description = 'Scheduled reminder for everyone that race starts soon.',
colour = discord.Colour.blue()
)
await channel.send(f'{racer_role.mention}',embed=embed)
msg_sent = True
else:
msg_sent = False
await asyncio.sleep(1)
@client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=' ;help'))
client.loop.create_task(timer())
print('Bot is alive')
#Functions such as this no longer working
@client.command()
async def help(ctx):
...
Upvotes: 1
Views: 552
Reputation: 2907
You should use discord.ext.tasks as it seems part of your code is blocking.
import discord
import datetime
import asyncio
from discord.ext import tasks, commands
from discord.utils import get
client = commands.Bot(command_prefix = ';',help_command=None) #prefix
race_times_utc = [12,19,21,22,23]
time = datetime.datetime.now
@tasks.loop(seconds=1.0) # replaces the sleep
async def timer():
msg_sent = False
if any(time().hour == race for race in race_times_utc) and time().minute == 0:
if not msg_sent:
channel = client.get_channel(838626115326636022)
racer_role = get(channel.guild.roles, name = 'Racers')
embed = discord.Embed(
title = 'Race Time',
description = 'Scheduled reminder for everyone that race starts soon.',
colour = discord.Colour.blue()
)
await channel.send(f'{racer_role.mention}',embed=embed)
msg_sent = True
else:
msg_sent = False
@client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=' ;help'))
print('Bot is alive')
timer.start()
Upvotes: 1