Discord.py 重启命令

Mys*_*usK 5 python shutdown restart discord.py

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

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!")```
Run Code Online (Sandbox Code Playgroud)

小智 5

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'))

Run Code Online (Sandbox Code Playgroud)

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