繁体   English   中英

我想设置一个密码来关闭discord.py上的机器人,我不知道怎么写代码。 有人帮我写代码

[英]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

我想设置一个密码来关闭discord.py上的机器人,我不知道怎么写代码。 有人帮我写代码

@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

这就是我到目前为止所得到的

即使方法不错,您的代码中也存在一些格式错误。

您需要首先包含真实/现有的check function。 这将检查回复是否真的是在机器人的直接消息中做出的,并且它来自执行命令的用户。

然后我们用wait_for等待答案。 您可以根据需要设置timeout或省略它。 然后我们检查消息的content ,如果它与设置的密码匹配,那么机器人就会关闭,否则它不会。 (这需要一些时间!)

您的完整代码将是:

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

您可以像这样简单地定义名为offline的命令:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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