0

My code basically does a conditional statement of a current time zone. if the time in that time zone condition is true then it sends a message pining a role. However, my code does not send a message when i put it in discord's py function

import os
import discord
from discord.ext import commands 
import datetime
import pytz


intents = discord.Intents.default() 
intents.members = True
client = commands.Bot(command_prefix = '?',intents=intents) #sets prefix 
client.remove_command('help')


f = 19
m = 0 
@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.do_not_disturb, activity= discord.Activity(name="Around ;)", type=discord.ActivityType.watching))
    print('ready')

@client.event
async def time_check():
    await client.wait_until_ready()
    while not client.is_closed:
        cst = datetime.datetime.now(tz=pytz.timezone('US/Central')).time()
        if cst.hour == 19 and cst.minute == 35:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 19 and cst.minute == 40:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 9 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 13 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break
        elif cst.hour == 18 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break

client.loop.create_task(time_check())

          
client.run('TOKEN') 
2
  • a) This would be a lot easier to understand without random hard coded strings and numbers, also each if statement block is the same, so maybe they should be functions? b) Have you tried adding some print statements in to see how far execution gets? Do you know you're getting into the while loop? Do you know that you're entering the right if statement? Commented Feb 21, 2021 at 1:51
  • I mean yeah, it's WET and hardcoded but that's not the problem. The issue is that they're decorating the function as an event but he's not dispatching it. One solution would be to dispatch the event somewhere OR just run it as a looping task. Commented Feb 21, 2021 at 2:08

1 Answer 1

1

Because "time_check" is not a valid event.

You probably want to run that as a loop.

Just do this.

from discord.ext import tasks
 # then on your time_check function, change @client.event for:

@tasks.loop(minutes=1)
async def time_check():
    # the rest of your function

#before client.run()
time_check.start()

Hope that works

Sign up to request clarification or add additional context in comments.

1 Comment

well. when i run the code it starts normally but it doesnt preform the task

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.