【发布时间】:2021-06-22 13:32:30
【问题描述】:
我一直在使用以下两种方法从不和谐嵌入中获取用户输入。第一个用于获取基于文本的消息输入,第二个用于获取反应输入(用于对嵌入进行分页)。我想知道的是我是否可以将这两者合并以创建一个等待反应或消息输入的方法?
#message input
try:
answer = await self.bot.wait_for(
"message",
timeout=60,
check=lambda message: message.author.id == ctx.author.id
and isinstance(message.channel, discord.channel.DMChannel)
)
#reaction input
try:
reaction, user = await self.bot.wait_for(
"reaction_add",
timeout=60,
check=lambda reaction, user: user.id == ctx.author.id
and reaction.emoji in buttons
#and isinstance(reaction.channel, discord.channel.DMChannel),
)
更新: 所以我试图实现方法duckboycool 链接到(非常感谢!)。 我现在遇到的问题是,当我对分页嵌入做出反应时,它工作得很好,反应会被记录下来并相应地更新嵌入。但是,如果我输入一条消息,则会出现以下错误:
“反应,表情符号=等待任务 TypeError: cannot unpack non-iterable Message object"
这是我的代码:
finished =0
while finished == 0:
done_tasks = None
check1=lambda reaction, user: user.id == ctx.author.id and reaction.emoji in buttons
check2=lambda message: message.author.id == ctx.author.id and isinstance(message.channel, discord.channel.DMChannel)
pending_tasks = [self.bot.wait_for('reaction_add',check=check1),self.bot.wait_for('message',check=check2)]
done_tasks, pending_tasks = await asyncio.wait(pending_tasks, return_when=asyncio.FIRST_COMPLETED)
#print(done_tasks)
for task in pending_tasks:
task.cancel()
for task in done_tasks:
reaction, emoji = await task
message = await task
if reaction:
print(reaction)
previous_page = current
if reaction.emoji == u"\u23EA":
current = 0
elif reaction.emoji == u"\u25C0":
if current > 0:
current -= 1
elif reaction.emoji == u"\u25B6":
if current < len(pages)-1:
current += 1
elif reaction.emoji == u"\u23E9":
current = len(pages)-1
if current != previous_page:
await msg.edit(embed = pages[current])
else:
print(message.content)
【问题讨论】:
-
this 回答你的问题了吗?
-
@duckboycool 谢谢你的链接!我试图实施该解决方案......认为我做错了什么。你介意看看吗?我已经更新了原帖。
标签: python discord.py