-4

I am using repl.it to run two bots in the same repl and I'm using imports as a I saw in other stackoverflow questions.

print('This will be the page where all bots will be made.')
import os
import bot1
import keep_alive
import bot2
import keep_alive1
while True:
  print('ALL BOTS STARTED!')

The problem is, it never gets to the while loop (it's for testing) because it gets stuck in the bot1 file. How should I fix this? The reason I'm doing it in the same repl project is so I can merge the bot database.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

3

You should not be using an SQLite or local database with repl.it. This is because when the containers do a restart you might lose some data (unless they have changed this over the years).

It is recommended to use a database like MongoDB which offers a free 500MB database for you to use.

Since you did ask a question, I will answer it. You could try this:

import asyncio

loop = asyncio.get_event_loop()
loop.create_task(bot1.start("bot_token1"))
loop.create_task(bot2.start("bot_token2"))
loop.run_forever()

You would need to use cogs and have both bots set up in the main.py then use cogs for better file management.

Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39
Axisnix
  • 2,822
  • 5
  • 19
  • 41
  • 1
    I wouldn't necessarily use `asyncio` for this, `threading` or `multiprocessing` would be a better option. Though if the bots are small, this should do the trick – Łukasz Kwieciński Mar 14 '21 at 14:37
  • 2
    Well @ŁukaszKwieciński I based the asyncio usage on: hosting with repl.it your bot isn't going to be complex or require much bandwidth. Thanks for the suggestion though – Axisnix Mar 14 '21 at 14:42
  • 2
    @ŁukaszKwieciński I don't even know why I put that there lmao – Axisnix Mar 14 '21 at 15:52
  • But, I am using a file database, txt files since I do not know how to use SQLite. Right now, I'm creating text files with ```with open(str(ctx.author.id)+'.txt','w'): print('Testing', file=out) ``` – Nicholas Wang Mar 14 '21 at 17:05
  • He’s not telling you to use a database, he’s recommending. Though working with text files is a pain, you should switch to at least a json or yaml file – Łukasz Kwieciński Mar 14 '21 at 22:04
  • However, I used asyncio and it still isn't starting both. Also, I'm using keep_alive as my flask application and stuff. Here is the format I followed: https://replit.com/talk/learn/Hosting-discordpy-bots-with-replit/11008 I used these files • keep_alive1.py • bot1.py • keep_alive2.py • bot2.py and • main.py with `asyncio`. – Nicholas Wang Mar 14 '21 at 22:11
  • Right, you need to understand that you need to put bot1.py and bot2.py into the main.py @NicholasWang – Axisnix Mar 14 '21 at 22:14
  • So merge them into **1 file?** – Nicholas Wang Mar 14 '21 at 23:19
  • Yes @NicholasWang – Axisnix Mar 14 '21 at 23:45
  • Okay. This is the first time I'm doing this :) Thanks for y'all's support! – Nicholas Wang Mar 15 '21 at 00:15
  • https://replit.com/@nicholasxwang/CHBUtils#main.py This isn't working though... To be honest, I am very confused now ;-; – Nicholas Wang Mar 15 '21 at 00:24
  • @NicholasWang be patient. You need to understand there are different timezones in the world. This would be a separate question – Axisnix Mar 15 '21 at 08:04
  • @FluxedScript oh sry btw im in PDT rn – Nicholas Wang Mar 15 '21 at 17:08
0

You can try and use:

os.system(f"python {filename} &")

In the same script that your doing imports. You just have to replace the {filename} with your file.

Imports just go to a file and doesn't leave that file.