如何从不和谐中收集意见

  • 本文关键字:不和谐 python discord
  • 更新时间 :
  • 英文 :

chars = "qwertyuiopasdfghjklzxcvbnm!@#$%^&*()1234567890QWERTYUIOPASDFGHJKLZXCVBNM?/"
while 1:

如何在discord.py中收集输入?比如我想让用户输入密码的长度和密码的个数并保存在passCount和passLength中

passLength = int(input("What length do you want your password to be: "))
passCount = int(input("How many passwords do you want to generate: "))
for x in range(0,passCount):
password = ""
for x in range(0,passLength):
password_char = random.choice(chars) 
password = password + password_char
print("Here is your password,",password)
@client.command()
async def gen_password(ctx, length):
[your code here...]
await ctx.send(f'Your generated password is {password')

可以使用wait_for

等待WebSocket事件发送。

这可以用来等待用户回复消息,或者对消息作出反应,或者以自包含的方式编辑消息。

timeout参数被传递给asyncio.wait_for()。缺省情况下,不超时。注意,这确实传播asyncio。TimeoutError在超时的情况下为您提供,并提供方便使用。

如果事件返回多个参数,则返回包含这些参数的元组。请查看文档中的事件及其参数列表。

该函数返回第一个满足要求的事件。

wait_for(event, *, check=None, timeout=None)

等待,直到check返回True。你可以设置一个超时时间

等待用户回复:

@client.event
async def on_message(message):
if message.content.startswith('$greet'):
channel = message.channel
await channel.send('Say hello!')
def check(m): # m = [Message Object]
return m.content == 'hello' and m.channel == channel
msg = await client.wait_for('message', check=check) # wait for reply
await channel.send('Hello {.author}!'.format(msg))

示例2

等待消息作者的点赞反应:

@client.event
async def on_message(message):
if message.content.startswith('$thumb'):
channel = message.channel
await channel.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍'
try:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('👎')
else:
await channel.send('👍')

注意这段代码没有经过测试


def isInt(string):
try:
int(string)
return True
except ValueError:
return False
chars = [...]
@bot.command()
async def password(ctx):
def check(m): # m = [Message Object]
if isInt(m.content) and m.channel == channel and m.author.id == ctx.author:
await ctx.send("please send a number")
return True
else:
return False
try:
await ctx.send("How many passwords do you want to generate")
message = await client.wait_for('message', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('time out returning')
return
for x in range(0,int(message)):
password = ""
for x in range(0,10):
password_char = random.choice(chars) 
password += password_char
print("Here is your password,",password)