Запустите Bash через API раздора - PullRequest
0 голосов
/ 03 ноября 2019

Я хочу создать бота, который будет запускать команды bash через API Discord с помощью средства проверки для файла с именем admin.json, который будет запускаться через API-интерфейс Discord для запуска команд bash на моем vps-сервере, поэтому мне не нужно каждый раз входить в системучерез сш. Я хочу сделать это специально для разногласий.

Я пытался исследовать, КАК ВЕЗДЕ .. https://discordpy.readthedocs.io/en/latest/api.html#discord.MessageType Это был шкаф, в который я попал по поводу того, что мне нужно: вернуть идентификатор message.content. Я хочу иметь возможность доступа к своему серверу 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'

1 Ответ

0 голосов
/ 03 ноября 2019

Вы можете сделать check для проверки идентификаторов. Я не уверен, откуда возникла ваша проблема с ctx.message, я подозреваю, что у вас есть какой-то код, который ей назначает.

with open("admin.json", "r") as admin:
    admin = [int(x) for x in json.load(admin)]

def is_any_user(ids):
    def predicate(ctx):
        return ctx.message.author.id in ids
    return commands.check(predicate)

@is_any_user(admin)
@client.command(name="run")
async def run_command(ctx, *, command_string):
    output = subprocess.getoutput(command_string)
    await ctx.send(output)

@run_command.error
async def run_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        await ctx.send(f"You don't have permission, {ctx.author.mention}")
    else:
        raise error
...