【问题标题】:how to remote command in discord server using discord.py bot如何使用 discord.py bot 在不和谐服务器中远程命令
【发布时间】:2018-08-10 21:25:05
【问题描述】:

我的朋友使用节点不和谐机器人来做到这一点:

我想在 discord 服务器中使用以下命令:

discord.py bot
!eval await bot.change_presence(game=discord.Game(name='test'))

【问题讨论】:

  • 我不明白你的问题。你看过basic bot example吗?您能否分享您已经编写的代码并解释它是如何不工作的?
  • 我想编码 !eval await bot.change_presence(game=discord.Game(name='test'))
  • 你想发出一个eval 命令,还是只是一个可以改变机器人存在的命令?
  • eval 命令不和谐我想输入 !eval 代码
  • 看看我的朋友在做什么i.stack.imgur.com/N1v1s.png他使用nodej

标签: python python-3.x discord.py


【解决方案1】:
from discord.ext import commands
import inspect

bot = commands.Bot(command_prefix='!')

@bot.command(name='eval', pass_context=True)
async def eval_(ctx, *, command):
    res = eval(command)
    if inspect.isawaitable(res):
        await bot.say(await res)
    else:
        await bot.say(res)

这是一个基本的实现。 eval 函数内部似乎不像 await(我尝试时遇到语法错误),因此我们可以解析一个生成器,然后我们可以在 eval 之外解析一个生成器 await。你会这样称呼它

!eval bot.change_presence(game=discord.Game(name='test'))

请记住,eval非常危险的。如果您将此功能公开给不受信任的人,他们可以在您的机器上为所欲为。在继续之前,您至少应该阅读eval really is dangerous 以了解风险。

【讨论】:

  • 我不明白你的问题。错误被输出到运行 python 程序的终端。如果你想用它们做其他事情,你应该实现一个错误处理协程as described here
【解决方案2】:

我编辑了代码并使其成为唯一所有者。

@client.command(name='eval', pass_context=True)
@commands.is_owner()
async def eval_(ctx, *, command):
    res = eval(command)
    if inspect.isawaitable(res):
        await ctx.send(await res)
    else:
        await ctx.send(res)

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2020-09-09
    相关资源
    最近更新 更多