【问题标题】:How would I create a command to shutdown my Discord.py Bot?我将如何创建一个命令来关闭我的 Discord.py Bot?
【发布时间】:2019-06-30 20:56:56
【问题描述】:

我已经重写了我现有的 Discord Bot,以便命令可以通过 @client.command 工作。

这里是 clear 命令的示例,您可以了解该语言的工作原理。

@client.command()
async def echo(*args):
  output = ""
  for word in args:
    output += word
    output += " "
  await client.say(output)

我想创建 2 个命令。 一个将-shutdown 机器人,使其脱机并且无响应。 另一个是-restart机器人,意思是如果我更新代码,我运行重启命令,机器人会离线,重启,然后回来。

我该怎么做呢?

因为我希望这些命令只对我有用,所以我在下面留下了 Discord 用户 ID,因此您可以将其包含在代码中。 432234718860148749.

提前致谢, H

【问题讨论】:

  • 您使用什么操作系统来运行您的机器人? “重新启动”的最简单方法是启动另一个进程,等待几秒钟(当您的机器人关闭时),然后再次启动您的机器人。
  • @PatrickHaugh 我正在使用 Chrombook,运行 repl.it 的代码。我将如何创建该代码?
  • 嗯,我从未使用 repl.it 进行托管。这将使这变得更加困难。你如何正常启动/停止你的机器人?是否有一些 web api 可以用来重启机器人?
  • @PatrickHaugh 要启动它,我运行 repl.it 代码。要停止,我更改令牌并编辑代码。因为它是一个私人机器人,所以它有效,但它需要相当长的时间。

标签: python bots discord shutdown repl.it


【解决方案1】:

https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.logout有你的答案

@client.command()
@commands.is_owner()
async def shutdown(ctx):
    await ctx.bot.logout()

但是,我还不知道如何通过命令重新启动机器人

【讨论】:

    【解决方案2】:

    从最新版本——Discord.py 1.3.3 开始——试试这个:

    # Close the bot
    @client.command(aliases=["quit"])
    @commands.has_permissions(administrator=True)
    async def close(ctx):
        await client.close()
        print("Bot Closed")  # This is optional, but it is there to tell you.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-07
      • 2021-09-04
      • 2021-04-09
      • 2020-08-30
      • 2021-10-31
      • 2021-01-07
      • 2021-07-02
      • 2019-08-22
      相关资源
      最近更新 更多