簡體   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