【问题标题】:Run bash through discord api通过 discord api 运行 bash
【发布时间】: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 转换为字符串,以便可以使用该变量来运行命令。

标签: python bots discord


【解决方案1】:

您可以创建一个check 来处理检查ID。我不确定您对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

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2014-10-30
    • 2011-01-23
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多