0

I am making d-day count and notice bot in discord now. i need my bot to send d-day count message every 12 hours. how can i make that happen??

import discord
from discord.ext import commands
from bs4 import BeautifulSoup as bs
import asyncio
import requests

bot = commands.Bot(command_prefix='?')

html = requests.get('https://superkts.com/cal/d_day/20200504')
soup = bs(html.text, 'html.parser')
data1 = soup.find('article', {'class': 'result'}).text

html = requests.get('https://superkts.com/cal/d_day/20201203')
soup = bs(html.text, 'html.parser')
data2 = soup.find('article', {'class': 'result'}).text

html = requests.get('https://superkts.com/cal/d_day/20220105')
soup = bs(html.text, 'html.parser')
data3 = soup.find('article', {'class': 'result'}).text

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command()
async def notice(ctx):
    await ctx.send("문준영 입대" + data1)
    await ctx.send("신원호 수능" + data2)
    await ctx.send("김수한 전역" + data3)

bot.run('TOKEN')
moonjy1120
  • 37
  • 2
  • 11

1 Answers1

1

If you just want to repeat it every 12 hours, you can put the message in a loop with a sleep at the end:

@bot.command()
async def notice(ctx):
    while True:
        await ctx.send("문준영 입대" + data1)
        await ctx.send("신원호 수능" + data2)
        await ctx.send("김수한 전역" + data3)
        await asyncio.sleep(12 * 60 * 60)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Thanks for fast replay^^ i think your code makes schedule everytime i type "?notice" right? i just want that message to appear when i type command and every 12am. how can i make this?? – moonjy1120 Apr 09 '20 at 21:01
  • Sorry, do you want it at 12 AM every day, or every 12 hours? Do you only want one notice going on eveywhere, or only one per channel, or only one per guild? – Patrick Haugh Apr 09 '20 at 21:10
  • Sorry, my bad explanation makes you confuse.I want it at 12AM every day only one notice going on everyehere. – moonjy1120 Apr 09 '20 at 21:26
  • You might want to look into using `tasks` instead of `commands`. Have a `before_loop` that waits until 12 am, then set the loop timer to 24 hours. – Patrick Haugh Apr 09 '20 at 21:35
  • I tried using tasks like you said...(code below answer by me) but this time it sends message every seconds do i did something wrong? i was trying to check every seconds that it is 12AM and if it is True, send message to channel. – moonjy1120 Apr 15 '20 at 03:19
  • `@tasks.loop(seconds=1)` why not do `@tasks.loop(hours=12)` – Patrick Haugh Apr 15 '20 at 03:19
  • if i do like that i have to run the code exactly exactly at 12AM. But i want to run the code anytime. – moonjy1120 Apr 15 '20 at 03:22
  • @moonjy1120 Here's an answer I wrote to a similar question. https://stackoverflow.com/questions/51530012/how-can-i-run-an-async-function-using-the-schedule-library/61180222#61180222 – Patrick Haugh Apr 15 '20 at 03:25
  • Oh...thx finally solved it. Do you know why the code i wrote below didn't work as i expected? – moonjy1120 Apr 15 '20 at 03:41
  • @moonjy1120 `now_time` isn't getting updated, so it'll always be whatever the initial value is – Patrick Haugh Apr 15 '20 at 03:45