python - wait_for() 多用户 discord.py
问题描述
我需要我的机器人等待多个用户输入。例如,如果我在不和谐服务器中
调用命令 ( )!'!invite @user1 @user2 @user3'
它应该等到所有提到的用户都说"@mentionme accept"
现在问题是,我可以做一次提及('!invite @user1'
)但我不能为多个用户做。这是我的代码:
@client.command()
async def invite(ctx,*,message):
mentioned_users = [member for member in message.mentions]#get all mentioned users
def check(message: discord.Message):
return message.channel == ctx.channel and message.author.id in mentioned_users and message.content == f"{ctx.author.mention} accept"
msg = await client.wait_for('message', check=check,timeout=40.0)#wait_for
await ctx.channel.send(f'{member.mention} accepted the invitation!')
它只适用于一次提及!还有其他方法可以wait_for
为多个用户使用该功能吗?
解决方案
您可以列出所有提及的内容并从中删除
@bot.command()
async def invite(ctx, *mentions: discord.Member):
await ctx.send('Waiting for everyone to accept the invitation...')
# Converting the tuple to list so we can remove from it
mentions = list(mentions)
def check(message):
if message.channel == ctx.channel and message.author in mentions and message.content.lower() == f'{ctx.author.mention} accept':
# Removing the member from the mentions list
mentions.remove(message.author)
# If the length of the mentions list is 0 means that everyone accepted the invite
if len(mentions) == 0:
return True
return False
await bot.wait_for('message', check=check)
await ctx.send('Everyone accepted the invitation!')
推荐阅读
- ios - 不支持的 URL Swift 5
- javascript - javascript json 到 python 烧瓶
- events - Datadog 事件触发器不返回数据而不是 0
- r - R ggplot每日时间序列分为数月和数年
- java - 谷歌的刷新访问令牌 API 在 SpringBoot 中不起作用
- r - 如何使用带有 R 的 Azure Notification Hub 向客户手机发送短信?
- c# - 在笔记本/客户端上进行 Windows 10 更新后,我的(软件)服务会不断被删除
- node.js - 如何在没有公共访问权限的情况下访问 AWS s3 文件?
- php - Laravel 资源控制器中的 Update() 函数不起作用
- tensorflow - 什么是 tensorflow.matmul?