【问题标题】:Discord.py KillswitchDiscord.py Killswitch
【发布时间】:2021-03-24 15:57:25
【问题描述】:

我在 discord.py 上的 killswitch 似乎没有工作,我也不清楚为什么。有人可以指出我犯的任何错误吗?谢谢。

@client.command(aliases=["shut","shutdown","quit","stop_that","stahp", "kill"])
async def stop(ctx):
   await ctx.send("Attention: I have been murdered.")
   await client.logout()

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

似乎代码没问题,再试一次,但不要使用.logout(),而是使用close()。另外,我建议将其设为所有者专用命令。

【讨论】:

    【解决方案2】:

    如果上述答案不起作用,您可以通过 exit()quit() 退出 python 程序

    【讨论】:

    • 这是一个非常聪明的修复方法,但是它消除了执行其他操作或代码的可能性,例如在需要时重新登录。
    【解决方案3】:

    您可以使用 if 语句,例如 if allowed == True:在开头声明 allowed = True ,当该语句运行时将其设置为 false ,您可以使用另一个语句将其重置为 True 并使其再次工作.还将所有命令放在 if 语句中,否则它将不起作用。可能不是最好或最有效的,但至少对我来说是有效的。这是一个例子:

    import discord
    
    allowed = True
    
    client.command(aliases=["awaken","start","restart"])
    async def start(ctx):
       await ctx.send("Attention: I have been reborn.")
       allowed = True
    
    client.command(aliases=["shut","shutdown","quit","stop_that","stahp", "kill"])
    async def stop(ctx): #outside of if statement so it won't be affected
       await ctx.send("Attention: I have been executed.")
       allowed = False
    
    if allowed == True:
       client.command(aliases=["pain","and","suffering"])
       async def speeek(ctx): # in if statement will be affected
          await ctx.send("Attention: I have been suffering all year.")
    
       client.command(aliases=["ah","ahhhh","oh my gawd"])
       async def SCREEEAM(ctx):
          await ctx.send("AAAAAHHHHHHHH")
    

    有一个基本示例,所有要禁用的命令都在那里。任何你不想受到影响的东西都放在外面。如果所有其他方法都失败了就使用,也不容易受到语法更改的影响,因为它是一个 python 函数(if 语句) 不是 discors.py 的一部分

    【讨论】:

    • 我会推荐 if allowed: 而不是 if allowed == True: 因为那更 Pythonic:python.org/dev/peps/pep-0008
    • 虽然不是 OP 的明确要求,但您的示例不会阻止其他用户关闭和打开 allowed 操作。
    • 我完全忘记了我有这个,感谢仍然回复人们!现在一切都很好,您的建议很棒!
    【解决方案4】:

    确保检查它是否是机器人所有者,所以只有你可以关闭它。

    import discord
    from discord.ext import commands
    
    client = commands.Bot(command_prefix="!", owner_id=youridhere) # youridhere is type int
    
    @client.command(aliases=['stopbot'])
    @commands.is_owner()
    async def logout(ctx):
        await ctx.send(f"I'm going offline {ctx.author.mention}! Bye :wave:!")
        await client.logout()
    
    client.run("YOURTOKENHERE")
    

    【讨论】:

    • client.logout 应该改为 client.logout() 否则,+1
    • 是的,我忘记了括号。刚注意到
    【解决方案5】:

    会容易得多:

    @client.command()
    async def offline(ctx):
        TeamIDS=[id from your bot mates whatever]
        if ctx.author.id in TeamIDS:
            print(f"{ctx.author} hat den Bot ausgemacht.")
            await ctx.send("Bye bye")
            exit()
    

    类似的东西

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 2021-06-29
      • 2020-09-18
      相关资源
      最近更新 更多