如何在 discord.py 中创建密码检查命令 [关闭]

Posted

技术标签:

【中文标题】如何在 discord.py 中创建密码检查命令 [关闭]【英文标题】:How to create a password checking command in discord.py [closed] 【发布时间】:2021-10-31 19:26:36 【问题描述】:

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

【问题讨论】:

我猜有,是的。 【参考方案1】:

我是这样做的。

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

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

【讨论】:

谢谢!但现在我会接受另一个答案!【参考方案2】:

好的,所以假设你想检查密码强度或检查密码之前是否泄露过,你可以使用这个代码

这是用于密码泄漏检查 首先,去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)

【讨论】:

非常感谢!

以上是关于如何在 discord.py 中创建密码检查命令 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

我如何在 Discord.py 中创建一个您 @ 用户的命令并给该用户一个拥抱

我试图在 discord.py 中创建一个楼梯命令

如何在 discord.py 中创建 discord.Permissions 对象?

如何制作不狙击跨服务器的狙击命令(discord.py)

在特定类别中创建频道(类别 id 应该是可变的) discord.py

discord.py 如何检查用户是不是在服务器上?