skip to Main Content

I started to host my Discord bot on a cPanel (o2switch), but I don’t know how to run the bot. I have to define an entry point for my app, but I don’t know what it should be.
I tried to set it as a function that just returns "Launched!", but that doesn’t work.

# imports

def application():
    return "Launched!"

# bot code

Does anyone know what code should I add for my bot runs?


EDIT: Added “runner” thing. The bot still doesn’t launch, but I have this log:

App 16078 output: /opt/passenger-5.3.7-5.el7.cloudlinux/src/helper-scripts/wsgi-loader.py:26: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
App 16078 output:   import sys, os, re, imp, threading, signal, traceback, socket, select, struct, logging, errno
App 16078 output: [ pid=16078, time=2020-01-15 16:18:24,002 ]: PyNaCl is not installed, voice will NOT be supported
App 16078 output: [ pid=16078, time=2020-01-15 16:18:24,033 ]: WSGI application raised an exception!
App 16078 output: Traceback (most recent call last):
App 16078 output:   File "/opt/passenger-5.3.7-5.el7.cloudlinux/src/helper-scripts/wsgi-loader.py", line 199, in main_loop
App 16078 output:     socket_hijacked = self.process_request(env, input_stream, client)
App 16078 output:   File "/opt/passenger-5.3.7-5.el7.cloudlinux/src/helper-scripts/wsgi-loader.py", line 333, in process_request
App 16078 output:     result = self.app(env, start_response)
App 16078 output:   File "/home/bist1484/virtualenv/bot/3.7/lib/python3.7/site-packages/discord/client.py", line 598, in run
App 16078 output:     return future.result()
App 16078 output:   File "/home/bist1484/virtualenv/bot/3.7/lib/python3.7/site-packages/discord/client.py", line 579, in runner
App 16078 output:     await self.start(*args, **kwargs)
App 16078 output:   File "/home/bist1484/virtualenv/bot/3.7/lib/python3.7/site-packages/discord/client.py", line 542, in start
App 16078 output:     await self.login(*args, bot=bot)
App 16078 output: TypeError: login() takes 2 positional arguments but 4 positional arguments (and 1 keyword-only argument) were given

3

Answers


  1. You need to call Client.run. Specifically, it looks like you need to prepare a partial function that you can pass to this other application:

    from functools import partial
    from discord import Client
    
    client = Client()
    
    @client.event
    async def on_message(message):
        print(message.content)
    
    runner = partial(client.run, "your token")  # runner() then starts the bot
    
    Login or Signup to reply.
  2. cPanel is designed for web hosting, not for applications like Discord bots.
    The application entry point is for web application frameworks that support WSGI. It does not apply to Discord bots.

    Login or Signup to reply.
  3. I myself host my bot on cPanel. I’ll help you with hosting your bot. Make sure your bot is in the home directory, with permissions set to 755.

    You’ll need a start script and a stop script. Make a new file in the cgi-bin of your public_html and you’ll be able to start the bot at yourmaindomain.com/cgi-bin/startbot.py considering you name the start script to startbot.py. Place the following code in the start script :

    #!/usr/bin/python3.6
    
    import os, subprocess, signal
    
    print("Content-Type: text/htmlnn")
    
    counter = 0
    p = subprocess.Popen(['ps', '-u', 'username'], stdout=subprocess.PIPE)
    # must match your username --------^^^^^^^^
    
    out, err = p.communicate()
    for line in out.splitlines():
      if 'heliobot.py'.encode('utf-8') in line:
    #     ^^^^^^^^^^^----- this has to match the filename of your bot script
    
        counter += 1
        print("Bot already running.")
    
    if counter == 0:
      subprocess.Popen("/home/username/heliobot.py")
    #                         ^^^^^^^^-- be sure to update it to your username
    
      print("Bot started!")
    

    For the stop script, you can create a stopbot.py file in the same cgi-bin where you’ll be able to stop the bot at yourmaindomain.com/cgi-bin/stopbot.py, place the following code in the script:

    !/usr/bin/python3.6
    
    import os, subprocess, signal
    
    print("Content-Type: text/htmlnn")
    
    counter = 0
    p = subprocess.Popen(['ps', '-u', 'username'], stdout=subprocess.PIPE)
    # must match your username --------^^^^^^^^
    
    out, err = p.communicate()
    for line in out.splitlines():
      if 'heliobot.py'.encode('utf-8') in line:
    #     ^^^^^^^--- this has to match the filename of your loop
    
        counter += 1
        pid = int(line.split(None, 1)[0])
        print("Stopping bot.")
        os.kill(pid, signal.SIGTERM)
    
    if counter == 0:
      print("Already stopped.")
    

    Replace the first line, that’s the shebang, with the Python path of your hosting provider. Ensure that the modules used are installed, else request the host to install it for you. Also, ensure that the permissions of all these files are 755, otherwise you’ll get Internal Server Errors.

    Do remember to replace those parameters I’ve highlighted in the scripts. That’s exactly how I’ve been hosting my bot on cPanel free hosting since I started developing. I never had money to get a VPS so this was the best and seemingly the only option for me. (I don’t prefer Heroku and other application hosts for a variety of reasons). Hope that helps and solves your problem! If you need help with anything else, just comment it down and I’ll try to help you out. 🙂

    Regards,
    Sayan Bhattacharyya.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search