简体   繁体   中英

I want to set a password to shut down a bot on discord.py, and I don't know how to write the code. Someone help me witht the code

I want to set a password to shut down a bot on discord.py, and I don't know how to write the code. Someone help me with the code

@client.command()
@commands.has_permissions(administrator = True)
async def offline(ctx):
    await ctx.message.delete()
    msgg = await ctx.send("Please enter password in DM to continue shut down. This is a password protected command")
    msg = await ctx.author.send("Please enter password here. This command will timeout in 10 seconds")
password = []
def check(reaction, user):
    return str(password) == "testo" 

try:

    message = await client.wait_for('password', timeout=10, check=check)

except:
    await msg.edit("Password is incorrect. Please try again or contact bot developer")
    return

else:
    password.append(msg.content)

if:
    password == "testo"

    await msgg.edit("Bot is shutting down")
    await ctx.author.send("Password is correct. Bot will be shutting down")
    await asyncio.sleep(3)
    await msgg.edit(content="Bot has shut down. To start it again, please contact bot dev")
    await bot.logout()
    return

Thats all I got so far

You have some formatting errors in your code, even if the approach was not bad.

You need to include a real/existing check function first. This will check that the reply was really made in the bot's direct message and that it came from the user who executed the command.

We then wait with a wait_for for an answer. You can set timeout as you like or omit it. Then we check the content of the message and if it matches the set password then the bot shuts down, otherwise it does not. (This will take some time!)

Your full code would be:

@client.command()
@commands.has_permissions(administrator=True)
async def offline(ctx):
    await ctx.send(f"{ctx.message.author.mention}, check your DM's!")
    await ctx.message.delete()
    await ctx.author.send("Please enter the password to shut me down!")

    def check(m):
        return ctx.author == m.author and isinstance(m.channel, discord.DMChannel) # Check that it is a DM channel
    try:
        test = await client.wait_for('message', check=check, timeout=100) # We wait 100 seconds for a user response/message in the DMs
        if test.content == "123": # Content can be changed to whatever you want
            await ctx.author.send("Correct password, I will shutdown.")
            await client.logout()
        else:
            await ctx.author.send("Wrong password, run the command again!")
    except asyncio.TimeoutError:
        await ctx.author.send("The time was not enough? Alright.", delete_after=5)

You can simply define your command named offline like this:

password = '12345'

@client.command()
async def offline(ctx, *, password_check=None):
    if password_check and password_check == password:
        await ctx.message.channel.purge(limit=1)
        await ctx.send('Shutting down bot...')
        await ctx.bot.logout()
    elif not password_check:
        await ctx.send('Please enter the password!')
    else:
        await ctx.send('You got the password wrong.')

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