Discord.py重新启动命令

时间:2020-07-30 19:29:29

标签: python discord.py restart shutdown

所以,我正在制造一个机器人,我想知道是否有一种方法可以使用以下命令重新启动它: p!restart 我确实喜欢一个命令: p!shutdown 但无法弄清楚如何重新启动,对于那些来这里寻找关机cmd的人:

async def shutdown(ctx):
    id = str(ctx.author.id)
    if id == 'your_id_here':
        await ctx.send('Shutting down the bot!')
        await ctx.bot.logout()
    else:
        await ctx.send("You dont have sufficient permmisions to perform this action!")```

2 个答案:

答案 0 :(得分:0)

Client.logout()

这将简单地注销您,然后您可以使用重新登录 Client.login()

这只是重启机器人所需要的

答案 1 :(得分:0)

ctx.bot.logout() 只会注销您的机器人。

如果你想完全重启你的程序,我是这样实现的:

import sys
import os
from discord.ext import commands

bot = commands.Bot

def restart_bot(): 
  os.execv(sys.executable, ['python'] + sys.argv)

@bot.command(name= 'restart')
async def restart(ctx):
  await ctx.send("Restarting bot...")
  restart_bot()

bot.run(os.getenv('TOKEN'))

为了防止该命令可能被滥用,只需在您的命令中添加一个“if”语句,检查 ctx.author.id 以查看用户是否有权执行该命令。不过看起来你是这样做的。