简体   繁体   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

I want to set a password to shut down a bot on discord.py, and I don't know how to write the code.我想设置一个密码来关闭discord.py上的机器人,我不知道怎么写代码。 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.您需要首先包含真实/现有的check function。 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.然后我们用wait_for等待答案。 You can set timeout as you like or omit it.您可以根据需要设置timeout或省略它。 Then we check the content of the message and if it matches the set password then the bot shuts down, otherwise it does not.然后我们检查消息的content ,如果它与设置的密码匹配,那么机器人就会关闭,否则它不会。 (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:您可以像这样简单地定义名为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.

相关问题 我将如何对我的 Discord.py 机器人进行编码以查看是否有人回复用户(我)? - How would I code my Discord.py bot to see if someone replies to a user(me)? Discord.py 我想在我的代码中加入一些东西,让我成为邀请机器人的人 - Discord.py I want to have something in my code that gives me the person who invited a bot 我不明白以下代码的行为。 有人可以帮我吗 - I don't understand the behavior of this below code. Can someone help me here 我在我的 discord.py 机器人上发出警告 function 我不知道为什么它给我一个属性错误 - I am making a warn function on my discord.py bot and I don't know why it is giving me an attribute error 如果我不想使用 discord.ext.commands,如何向某人添加角色? (discord.py) - How can I add a role to someone if i don't want use discord.ext.commands? (discord.py) 我不知道如何解决这个问题(discord.py) - I don't know how to fix this(discord.py) 我想从 discord 接收用户的信息,但我不知道该怎么做。 ( discord.py ) - I want to receive information from the user from discord, but I don't know what to do. ( discord.py ) 当我提到他们时,我是否可以使用 discord.py 让我的机器人 DM 某人? - Is it possible for me with discord.py to be able to have my bot DM someone when I mention them? discord.py | 我试图让我的 discord 机器人向我发送一个邀请,邀请它连接到数据库中的特定服务器,但我当前的代码不起作用 - discord.py | I tried to make my discord bot send me a invite to specific servers it is connection to that are in a db but my current code doesn't work 我在 discord 中的 discord.py 机器人中需要帮助 - I need help in my discord.py bot in discord
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM