如何在 discord.py 中创建密码检查命令

How to create a password checking command in discord.py

我最近尝试构建我的 discord 机器人,我想创建一个 discord 命令来检查你的密码强度等等!有什么办法吗?我使用 python3(discord.py 重写)。谢谢!

这是我的做法。

import re #re module provides support for regular expressions in Python.

@bot.command()
async def password(ctx, password):
    password = password
    flag = 0
    while True:  
        if (len(password)<8):
            flag = -1
            break
        elif not re.search("[a-z]", password):
            flag = -1
            break
        elif not re.search("[A-Z]", password):
            flag = -1
            break
        elif not re.search("[0-9]", password):
            flag = -1
            break
        elif not re.search("[_@$]", password):
            flag = -1
            break
        elif re.search("\s", password):
            flag = -1
            break
        else:
            flag = 0
            await ctx.send("Valid password")
            break
    
    if flag ==-1:
        await ctx.send("Invalid password")

我从一个堆栈溢出问题中得到了帮助。它非常适合我。

好吧,假设你想做什么检查密码强度或检查密码是否已经泄露,你可以使用这个代码

这是为了密码泄漏检查 首先,转到 10 Million Passwords 并下载它并将其复制到 discordbot.py 文件的文件夹中

# The loading message
please_wait_emb = discord.Embed(title="Please Wait", description="``` Processing Your Request ```", color=0xff0000)
please_wait_emb.set_author(name="YourBot")
please_wait_emb.set_thumbnail(url="https://c.tenor.com/I6kN-6X7nhAAAAAj/loading-buffering.gif")

# Opening the passwords file
filepwdlist1 = open("10-million-password-list-top-1000000.txt", "r")
lines = filepwdlist1.readlines() # creates a list with each line as an element

# The command
@client.command()
async def pwdcheck(ctx, *, password):
    loading_message = await ctx.send(embed=please_wait_emb)

    try:
        if password + "\n" in lines: # check if the password is in the list
            embed=discord.Embed(title="Password Checker!", color=0xff0000)
            embed.set_author(name="YourBot", icon_url="https://cdn.discordapp.com/attachments/881007500588089404/881046764206039070/unknown.png")
            embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/877796755234783273/879311068097290320/PngItem_1526969.png")
            embed.add_field(name=f"Your Passoword", value=f"{password}", inline=False)
            embed.add_field(name=f"Safety", value=f"Not Safe. This password is in the list of most common 10 million passwords!", inline=False)
            embed.set_footer(text=f"Requested by {ctx.author.name}")
            await loading_message.delete()
            await ctx.send(embed=embed)
        else:
            embed=discord.Embed(title="Password Checker!", color=0xff0000)
            embed.set_author(name="YourBot", icon_url="https://cdn.discordapp.com/attachments/881007500588089404/881046764206039070/unknown.png")
            embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/877796755234783273/879311068097290320/PngItem_1526969.png")
            embed.add_field(name=f"Your Passoword", value=f"{password}", inline=False)
            embed.add_field(name=f"Safety", value=f"Safe. This password is not in the list of most common 10 million passwords!", inline=False)
            embed.set_footer(text=f"Requested by {ctx.author.name}")
            await loading_message.delete()
            await ctx.send(embed=embed)

    except Exception as e:
        embed2=discord.Embed(title=":red_square: Error!", description="The command was unable to run successfully! ", color=0xff0000)
        embed2.set_author(name="YourBot", icon_url="https://cdn.discordapp.com/attachments/881007500588089404/881046764206039070/unknown.png")
        embed2.set_thumbnail(url="https://cdn.discordapp.com/attachments/877796755234783273/879298565380386846/sign-red-error-icon-1.png")
        embed2.add_field(name="Error:", value=f"{e}", inline=False)
        embed2.set_footer(text=f"Requested by {ctx.author.name}")
        await loading_message.delete()
        await ctx.send(embed=embed2)

如果要检查密码强度 我想这应该可行

首先 pip install password-strength : More info on PyPi

from password_strength import PasswordStats

stats = PasswordStats('qwerty123')
print(stats.strength())  #-> Its strength is 0.316

而且,您可以改进它并将其添加到您的机器人中,例如:

# The loading message
please_wait_emb = discord.Embed(title="Please Wait", description="``` Processing Your Request ```", color=0xff0000)
please_wait_emb.set_author(name="YourBot")
please_wait_emb.set_thumbnail(url="https://c.tenor.com/I6kN-6X7nhAAAAAj/loading-buffering.gif")

@client.command()
async def passwordstrentghcheck(ctx, *, passowrdhere):
  loading_message = await ctx.send(embed=please_wait_emb)
  try:
    stats = PasswordStats(f'{passowrdhere}')
    embed=discord.Embed(title="Password Strength Checker", color=0xff0000)
    embed.add_field(name="Strenth:", value=f"{stats.strength()}", inline=False)
    embed.set_footer(text=f"Requested by {ctx.author.name}")
    await loading_message.delete()
    await ctx.send(embed=embed)
  
  except Exception as e:
    embed3=discord.Embed(title=":red_square: Error!", description="The command was unable to run successfully! ", color=0xff0000)
    embed3.set_author(name="YourBot", icon_url="https://cdn.discordapp.com/attachments/877796755234783273/879295069834850324/Avatar.png")
    embed3.set_thumbnail(url="https://cdn.discordapp.com/attachments/877796755234783273/879298565380386846/sign-red-error-icon-1.png")
    embed3.add_field(name="Error:", value=f"{e}", inline=False)
    embed3.set_footer(text=f"Requested by {ctx.author.name}")
    await loading_message.delete()
    await ctx.send(embed=embed3)