0

I am currently trying to create a Discord bot (with Discord.py) that is able to search through Reddit every few hours to see if there is anything new that has been posted in a specific subreddit. Currently, I have a way to scrape Reddit for this information but it is activated through a Discord command. Ideally, I would like for the script to run automatically after a certain time interval instead of being activated through a command.

I am using asyncpraw and discord.py for my code, so it is all in Python. Additionally, I am using Flask to create a webserver for the bot, and then using Repl.it to host the server for free. I also use cron-job.org to submit a request every five minutes to the webserver in order to keep it running.

I have my files set up as main.py, which contains all information for the Discord bot, commands, and code that activates when the command is entered into the chat, and then a keep_alive.py file that contains the starting of the web server and the thread (I'm not super knowledgeable about Flask so I'm not super clear on what everything does). At the end of main.py, before I enter the discord bot's token, I call a function from keep_alive.py.

So far, I have tried to use cron-job.org to set up a cronjob, but it hasn't worked thus far as I am not super clear on how to go about it. I have also tried using schedule.py or sched.py, but neither has worked.

To my understanding, I need to use a cronjob for the functionality that I want, but I would love some guidance on actually implementing it, especially as far as the organization of files goes and where to call functions for them to activate. Additionally, I would like to find out if there is a way to use cronjobs within Repl.it, because I don't think you can use them from within their console.

Please let me know if you need any more information or code or anything else! I appreciate any and all help! Thanks!

ABhatia
  • 3
  • 2
  • are you trying to simply have a background task? If yes -- take a look at [`discord.ext.tasks`](https://discordpy.readthedocs.io/en/stable/ext/tasks/index.html) – Łukasz Kwieciński Jul 28 '21 at 20:22
  • @ŁukaszKwieciński would I be able to mix a Reddit scraping script in with that? It's not necessarily a background task since I would want the messages to send to a discord chat. – ABhatia Jul 28 '21 at 20:33
  • yes, you could, but it should be completely asynchronous, and you can still send messages to discord. – Łukasz Kwieciński Jul 28 '21 at 20:52
  • @ŁukaszKwieciński to use cogs would the bot have to continually run? and what would happen if it times out eventually? would it just restart once I rerun it? – ABhatia Jul 28 '21 at 21:14

1 Answers1

1

I don't know if this helps you but the way I was able to automate python scripts on my web server was by using a windows 2016 server.

Then creating a .bat file that would run the script like this. Just make sure your file location is set correctly.

python C:\inetpub\wwwroot\top_10_week.py %*

Then use windows task scheduler to schedule the script to run after a certain time, I set mine to 3 hours but I'm sure you can do it every 5 minutes.

https://www.windowscentral.com/how-create-automated-task-using-task-scheduler-windows-10

aesap
  • 11
  • 1