如何在 cPanel (o2switch) 上 运行 Python Discord 机器人
How to run a Python Discord bot on a cPanel (o2switch)
我开始在 cPanel (o2switch) 上托管我的 Discord 机器人,但我不知道如何 运行 机器人。我必须为我的应用程序定义一个入口点,但我不知道它应该是什么。
我试图将其设置为仅 returns "Launched!"
的函数,但这不起作用。
# imports
def application():
return "Launched!"
# bot code
有谁知道我应该为我的机器人 运行 添加什么代码?
编辑:添加了 "runner" 东西。机器人仍然没有启动,但我有这个日志:
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
您需要致电Client.run
。具体来说,您似乎需要准备一个可以传递给其他应用程序的部分函数:
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
cPanel 专为网络托管而设计,不适用于 Discord 机器人等应用程序。
应用程序入口点用于 web application frameworks that support WSGI。它不适用于 Discord 机器人。
我自己在 cPanel 上托管我的机器人。我会帮助您托管您的机器人。确保您的机器人位于主目录中,权限设置为 755。
您需要一个启动脚本和一个停止脚本。在 public_html 的 cgi-bin 中创建一个新文件,考虑到将启动脚本命名为 startbot.py,您将能够在 yourmaindomain.com/cgi-bin/startbot.py 启动机器人。将以下代码放在启动脚本中:
#!/usr/bin/python3.6
import os, subprocess, signal
print("Content-Type: text/html\n\n")
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!")
对于停止脚本,您可以在同一个 cgi-bin 中创建一个 stopbot.py 文件,您将能够在您的主域停止机器人。com/cgi-bin/stopbot.py,放置以下内容脚本中的代码:
!/usr/bin/python3.6
import os, subprocess, signal
print("Content-Type: text/html\n\n")
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.")
用您的托管服务提供商的 Python 路径替换第一行,即 shebang。确保使用的模块已安装,否则请主机为您安装。另外,确保所有这些文件的权限都是755,否则你会得到内部服务器错误。
请记住替换我在脚本中突出显示的那些参数。自从我开始开发以来,这正是我在 cPanel 免费托管上托管我的机器人的方式。我从来没有钱买 VPS 所以这是最好的,对我来说似乎是唯一的选择。 (出于各种原因,我不喜欢 Heroku 和其他应用程序主机)。希望对您有所帮助并解决您的问题!如果您需要任何其他帮助,请将其评论下来,我会尽力帮助您。 :)
此致,
Sayan Bhattacharyya.
我开始在 cPanel (o2switch) 上托管我的 Discord 机器人,但我不知道如何 运行 机器人。我必须为我的应用程序定义一个入口点,但我不知道它应该是什么。
我试图将其设置为仅 returns "Launched!"
的函数,但这不起作用。
# imports
def application():
return "Launched!"
# bot code
有谁知道我应该为我的机器人 运行 添加什么代码?
编辑:添加了 "runner" 东西。机器人仍然没有启动,但我有这个日志:
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
您需要致电Client.run
。具体来说,您似乎需要准备一个可以传递给其他应用程序的部分函数:
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
cPanel 专为网络托管而设计,不适用于 Discord 机器人等应用程序。
应用程序入口点用于 web application frameworks that support WSGI。它不适用于 Discord 机器人。
我自己在 cPanel 上托管我的机器人。我会帮助您托管您的机器人。确保您的机器人位于主目录中,权限设置为 755。
您需要一个启动脚本和一个停止脚本。在 public_html 的 cgi-bin 中创建一个新文件,考虑到将启动脚本命名为 startbot.py,您将能够在 yourmaindomain.com/cgi-bin/startbot.py 启动机器人。将以下代码放在启动脚本中:
#!/usr/bin/python3.6
import os, subprocess, signal
print("Content-Type: text/html\n\n")
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!")
对于停止脚本,您可以在同一个 cgi-bin 中创建一个 stopbot.py 文件,您将能够在您的主域停止机器人。com/cgi-bin/stopbot.py,放置以下内容脚本中的代码:
!/usr/bin/python3.6
import os, subprocess, signal
print("Content-Type: text/html\n\n")
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.")
用您的托管服务提供商的 Python 路径替换第一行,即 shebang。确保使用的模块已安装,否则请主机为您安装。另外,确保所有这些文件的权限都是755,否则你会得到内部服务器错误。
请记住替换我在脚本中突出显示的那些参数。自从我开始开发以来,这正是我在 cPanel 免费托管上托管我的机器人的方式。我从来没有钱买 VPS 所以这是最好的,对我来说似乎是唯一的选择。 (出于各种原因,我不喜欢 Heroku 和其他应用程序主机)。希望对您有所帮助并解决您的问题!如果您需要任何其他帮助,请将其评论下来,我会尽力帮助您。 :)
此致,
Sayan Bhattacharyya.