#How can I change the code so that it runs well on hosting?

1 messages · Page 1 of 1 (latest)

copper torrent
#

Hello! The following problem has arisen. When I launch the bot via PyCharm, everything works great, but when I upload it to the hosting and try to launch it, it gives an error. This is the second bot on the hosting, the first one works great and without errors

Here is the main.py code

from disnake.ext import commands
from utils.config import TOKEN

bot = commands.Bot(
    command_prefix="voice/", 
    intents=disnake.Intents.all(), 
    help_command=None,
    reload=True
)

@bot.event
async def on_ready():
    print(f'Bot run')

bot.load_extensions("cogs")
bot.run("token")```


config.py
```TOKEN = "token"
CATEGORY = 123 
VoiceID = 123```


Error:
echo pecan
#

How are you running it on the server?

#

Specifically, what command?

#

Oh pm2

copper torrent
#

pm2 start "/root/private/main.py" --interpreter python3 --name private

echo pecan
#

Are you sure your pycharm directory tree

#

Is the same as what’s in the server?

copper torrent
#

I'm trying right now with a different directory

echo pecan
#

Can you make an empty file inside utils and cogs called “init.py”

#

One sec

#

__

#

__ init __ . Py

#

Discord keeps doing underline of course

copper torrent
#

should I create an empty file in which folder? Privates or utilities?

echo pecan
#

Utils and cogs

#

_ _ init _ _ . Py

#

I’m on mobile and this is annoying

copper torrent
echo pecan
#

The error says “/roots/cogs”

copper torrent
#

I’m also trying to solve this same problem with a friend at the same time, he generally says that it’s all too interesting and incomprehensible, and right now he’s also been trying to figure out this shit for a very long time

echo pecan
#

It’s something to do with the location of the files and where you want to run it from

#

Do you have terminal available? I guess you do

#

If you just type “pwd” command in there and show it

#

I can join call in 5mins if that helps

copper torrent
#

Look, when I run the same code through my computer, the bot starts without any errors, and when I upload the same code without changes to the hosting, connect everything and try to run it, it starts giving errors. By the way, this is the second bot on this VPS server, the first one works great and without any complaints

#

Of course, I would be very grateful if something worked out, but I don’t speak or communicate well in English)

echo pecan
#

I’ll try on my side

echo pecan
copper torrent
echo pecan
#

then do the same and go into Privates

copper torrent
echo pecan
#

run ls u should see all ur files

copper torrent
echo pecan
#

do you have python installed, and all libaries installed on this vps already?

copper torrent
#

yes

echo pecan
#

try and run this in plain python as a test python3 main.py

copper torrent
#

everything starts up fine on the computer

echo pecan
#

no in ur vps

#

start it in ur vps, i know u want to use pm2 i just want to see something

copper torrent
echo pecan
#

its hard inside a picture

copper torrent
#

root@da25df8ee7cf:~/Bots/Privates# python3 main.py
Bot run!

echo pecan
#

no this one

copper torrent
#

/root/.pm2/logs/privat1-out.log last 15 lines:
/root/.pm2/logs/privat1-error.log last 15 lines:
1|privat1 | File "/root/Bots/Privates/main.py", line 16, in <module>
1|privat1 | bot.load_extensions("cogs")
1|privat1 | File "/usr/local/lib/python3.10/dist-packages/disnake/ext/commands/common_bot_base.py", line 637, in load_extensions
1|privat1 | for extension in disnake.utils.search_directory(path):
1|privat1 | File "/usr/local/lib/python3.10/dist-packages/disnake/utils.py", line 1274, in search_directory
1|privat1 | raise ValueError(f"Provided path '{abspath}' does not exist")
1|privat1 | ValueError: Provided path '/root/cogs' does not exist
1|privat1 | Traceback (most recent call last):
1|privat1 | File "/root/Bots/Privates/main.py", line 16, in <module>
1|privat1 | bot.load_extensions("cogs")
1|privat1 | File "/usr/local/lib/python3.10/dist-packages/disnake/ext/commands/common_bot_base.py", line 637, in load_extensions
1|privat1 | for extension in disnake.utils.search_directory(path):
1|privat1 | File "/usr/local/lib/python3.10/dist-packages/disnake/utils.py", line 1274, in search_directory
1|privat1 | raise ValueError(f"Provided path '{abspath}' does not exist")
1|privat1 | ValueError: Provided path '/root/cogs' does not exist

echo pecan
#

ye

#

bot.load_extensions("cogs") this is the problem I think.
bot cant find the cogs directory

copper torrent
#

How can I show it for him to see?

echo pecan
#
import disnake
from disnake.ext import commands
from utils.config import TOKEN
import os

bot = commands.Bot(
    command_prefix="voice/", 
    intents=disnake.Intents.all(), 
    help_command=None,
    reload=True
)

@bot.event
async def on_ready():
    print(f'Bot run')

cogs_directory = os.path.join(os.path.dirname(__file__), 'cogs')
bot.load_extensions(cogs_directory)
bot.run("token")

try this maybe

copper torrent
#
[TAILING] Tailing last 15 lines for [privat1] process (change the value with --lines option)
/root/.pm2/logs/privat1-out.log last 15 lines:
/root/.pm2/logs/privat1-error.log last 15 lines:
1|privat1  |   File "/usr/local/lib/python3.10/dist-packages/disnake/ext/commands/common_bot_base.py", line 519, in load_extension
1|privat1  |     spec = importlib.util.find_spec(name)
1|privat1  |   File "/usr/lib/python3.10/importlib/util.py", line 94, in find_spec
1|privat1  |     parent = __import__(parent_name, fromlist=['__path__'])
1|privat1  | ModuleNotFoundError: No module named 'Bots'
1|privat1  | Traceback (most recent call last):
1|privat1  |   File "/root/Bots/Privates/main.py", line 18, in <module>
1|privat1  |     bot.load_extensions(cogs_directory)
1|privat1  |   File "/usr/local/lib/python3.10/dist-packages/disnake/ext/commands/common_bot_base.py", line 638, in load_extensions
1|privat1  |     self.load_extension(extension)
1|privat1  |   File "/usr/local/lib/python3.10/dist-packages/disnake/ext/commands/common_bot_base.py", line 519, in load_extension
1|privat1  |     spec = importlib.util.find_spec(name)
1|privat1  |   File "/usr/lib/python3.10/importlib/util.py", line 94, in find_spec
1|privat1  |     parent = __import__(parent_name, fromlist=['__path__'])
1|privat1  | ModuleNotFoundError: No module named 'Bots'


echo pecan
#

try run with this

pm2 start "/root/Bots/Privates/main.py" --interpreter python3 --name priate1 --cwd /root/Bots/Privates just to make sure ur script is starting in the right directory

copper torrent
#

one sec

echo pecan
copper torrent
echo pecan
#

so its good?

copper torrent
#

thank you very much, I just puzzled over this all day and tried to solve it

echo pecan
#

I think you were running the script from /root so it will start in there

#

and inside root it does not have ur files

copper torrent
#

Now it works and everything seems to be fine

echo pecan
#

so u can just do cd /root/Bots/Privates

#

then run it WITHOUT the cwd

#

no problem

copper torrent
#

Do you happen to know how to make sure that when you start hosting or restart it, you don’t have to constantly enter the pm2 start command, but it is entered automatically, just like if you start bots through systemd?

echo pecan
#

You mean if you restart the bot, pm2 will start it automatically?

#

or if you restart the VPS?

copper torrent
echo pecan
#

I think you can set a command to run when pm2 starts up

copper torrent
#

Thanks, I'll read it later and try to figure it out

echo pecan
#

i dont use pm2 sorry, im a docker person :D

#

but since pm2 is running as systemd service

#

all u need to do is tell systemd to start pm2 when ur VPS reboots

#

which in turn will start ur bot as well