【问题标题】:Discord.py Restart CommandDiscord.py 重启命令
【发布时间】:2020-07-30 19:29:29
【问题描述】:

所以,我正在制作一个机器人,我想知道是否有办法使用以下命令重新启动它: 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!")```

【问题讨论】:

    标签: python discord.py restart shutdown


    【解决方案1】:

    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 以查看用户是否有权执行该命令。不过看起来你是这样做的。

    【讨论】:

      【解决方案2】:

      Client.logout()

      这只会将您注销,然后您可以使用以下命令再次登录 Client.login()

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

      【讨论】:

        猜你喜欢
        • 2021-06-15
        • 1970-01-01
        • 2020-10-19
        • 2021-07-15
        • 2020-12-31
        • 2019-10-23
        • 2020-05-03
        • 2020-11-06
        • 2021-03-30
        相关资源
        最近更新 更多