【发布时间】:2019-11-03 12:40:17
【问题描述】:
我想制作一个通过 discord api 运行 bash 命令的机器人,并使用一个检查器检查名为 admin.json 的文件,该文件将通过 discord 的 API 在我的 vps 服务器上运行 bash 命令运行,因此我不必每次都登录通过 ssh。我想专门针对不和谐这样做。
我尝试像无处不在一样进行研究.. https://discordpy.readthedocs.io/en/latest/api.html#discord.MessageType 这是我所需要的壁橱:返回 message.content 标识符。我希望能够通过 discord 访问我的 vps 服务器,这样我就不必每次都通过 ssh 登录。
import discord
import sys
import os
import json
import subprocess
import asyncio
from discord.ext import commands
t = "<token here>"
with open("admin.json", "r") as admin:
admin = json.load(admin)
client = commands.Bot(command_prefix='-')
@client.event
async def on_ready():
print('Bash bot is online')
@client.command()
async def on_message(ctx, arg1):
if str(ctx.author.id) in(admin):
command = ctx.message.content #this for some reason fails
cmd = subprocess.getoutput(command)
await ctx.send(command)
print(str(ctx.message.content)) #even this one fails :(
else:
pass
await ctx.send("You don't have permission to use this command!" + "\n" + "<" + "@" + str(ctx.author.id) + ">")
client.run(t, bot=True)```
Should be able to run with any args so if there is a space between a bash command it shouldn't break the bot like:
```wget https://google.com``` <- Should not break the discord bot
My most common error message:
```File "bot.py", line 23, in run
print(message.content)
AttributeError: 'str' object has no attribute 'content'
【问题讨论】:
-
我认为这与 str(ctx.message.content) 有关,但我想以某种方式将其集成到 subprocess.getoutput() 函数中。
-
我在哪里可以在您的服务器上注册为用户“admi”?
-
您分享的代码中不存在显示错误的行
print(message.content) -
@KlausD 你需要先给我你的不和谐。
-
@IainShelvington 我的意思是
AttributeError: 'str' object has no attribute 'content'是我最常见的问题,忽略第 23 行,因为我更改程序的次数太多,而且我有太多的错误消息需要处理。其中最常见的是从 ctx.content 转换为字符串,以便可以使用该变量来运行命令。