Lui Gutierrez
Lui Gutierrez

Reputation: 39

Discord.py Schedule

This is what I have so far... is does work for the amount delay seconds I want, but how to I add the time module or shedule module to make it work.. Just in case I want the bot to send the message every 24hrs

import discord
import asyncio
from discord.ext import commands
import schedule
import time

TOKEN = 'xxxxx'

client = commands.Bot(command_prefix = '.')

channel_id = '515994xxxxx5036697'

@client.event
async def on_ready():
    print('Bot Online.')

async def alarm_message():
    await client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel(channel_id)
        messages = ('test')
        await client.send_message(channel, messages)
        await asyncio.sleep(5) #runs every 5 seconds

client.loop.create_task(alarm_message())

client.run(TOKEN)

Upvotes: 2

Views: 4418

Answers (1)

Wasi Master
Wasi Master

Reputation: 1207

You can do this using discord.ext.tasks.

import discord
import asyncio
from discord.ext import commands
from discord.ext import tasks
import time

TOKEN = 'xxxxx'

client = commands.Bot(command_prefix = '.')

channel_id = '515994xxxxx5036697'

@client.event
async def on_ready():
    print('Bot Online.')

@tasks.loop(days=1)
async def alarm_message():
    await client.wait_until_ready()
    channel = client.get_channel(channel_id)
    message = 'test'
    await channel.send(message)

alarm_message.start()

client.run(TOKEN)

Upvotes: 2

Related Questions