简体   繁体   中英

How to make an on/off switch for discord.py bot

So I have a discord bot with like 2000 lines of code.

I want to make an on-off switch for the bot where after on_ready if the switch is turned off nothing will work! Here's what I have so far:

async def on_ready():
   print("YE")

k = 1

@client.command()
@commands.has_permissions(administrator=True)
async def off(ctx):
    global k
    k = 0
    await ctx.send("**RUNNING EMERGENCY ONLY!**")

@client.command()
@commands.has_permissions(administrator=True)
async def on(ctx):
    global k
    k = 1
    await ctx.send("**I'm Awake!**")

@client.command()
async def kstat(ctx):
    if k == 0:
        await ctx.send("**EMERGENCY FUNCTIONS ONLY!**")
    else:
        await ctx.send("**I'm Running Just Fine :)**")

if k == 1:
   #million lines of code
else:
   print("LOCKDOWN")

Now the function works, so when I do prefix + off or prefix + on, k value changes, but the functions don't die!

What could be the reason and how can I fix?

PS I tried while loop and it reads all the commands infite times thinking the command exists infinite times giving an error! So I don't think while loop can be called!

I hope you guys can help me! Thanks :)

So if I understand correctly, it looks like once the bot is online it checks if k == 1, and if so it runs the function. Since you're only doing the check once, if the value changes in the interim nothing will happen.

I suggest you try and call the functions/commands (or whatever happens in the million lines of code) and check the value of k or vice versa, every time you want a function to be called instead of constantly running the command without anything to stop it.

Please correct me if I've misunderstood anything, send a link to your code if possible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM