英文:
discordpy - How to grab member from message in wait_for method
问题
当有人点击按钮时,我想提示他们发送一个包含用户提及的消息。然后,我想从他们的回应中获取用户并将其添加到“第一个类别”。此外,我想为此输入设置超时,这就是为什么我使用了 wait_for()
。
我的问题是我无法将 m
指定为用户(User),并且总是收到 asyncio.TimeoutError
。
class LevelButtons(discord.ui.View):
@discord.ui.button(label="设置第一个类别", style=discord.ButtonStyle.green)
async def conf_level(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.message.channel.send("发送一个 @用户,该用户将被添加到第一个类别")
def check(m: discord.Member):
return m == discord.Member and m.channel == self.message.channel
try:
m = await self.bot.wait_for('message', timeout=10, check=check)
except asyncio.TimeoutError:
await self.message.channel.send('未收到输入,操作已中止')
else:
self.first_level.append(m)
await self.message.channel.send(f"{m.content} 已添加到第一个类别")
英文:
When somebody clicks on a button, I want to prompt them to send a message containing a user mention. I then want to grab the user from their response and add it to first category
. Also, I want to set timeout for this input, which is why I used wait_for()
.
My problem is I can't specify m
as a User and always get asyncio.TimeoutError
class LevelButtons(discord.ui.View):
@discord.ui.button(label="Setting up first category", style=discord.ButtonStyle.green)
async def conf_level(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.message.channel.send("Send a @user who will be in the first category")
def check(m: discord.Member):
return m == discord.Member and m.channel == self.message.channel
try:
m = await self.bot.wait_for('message', timeout=10, check=check)
except asyncio.TimeoutError:
await self.message.channel.send('Received no input so operation was aborted')
else:
self.first_level.append(m)
await self.message.channel.send(f"{m.content} added in the first category")
答案1
得分: 0
你可以使用 discord.Member
的属性 mentions
,它会返回一个提及的用户列表。
class LevelButtons(discord.ui.View):
@discord.ui.button(label="Setting up first category", style=discord.ButtonStyle.green)
async def conf_level(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.message.channel.send("Send a @user who will be in the first category")
def check(m: discord.Member):
return m == discord.Member and m.channel == self.message.channel
try:
m = await self.bot.wait_for('message', timeout=10, check=check)
except asyncio.TimeoutError:
await self.message.channel.send('Received no input so operation was aborted')
else:
self.first_level.append(m.mentions[0]) # Takes first mention in message
await self.message.channel.send(f"{m.mentions[0]} added in the first category")
另外,我假设你想检查消息的作者是否是按钮的用户,只需将 m == discord.Member
更改为:
def check(m):
return m.author.id == interaction.user.id and m.channel == self.message.channel
请记住,bot.wait_for('message')
返回的是 discord.Message
。
文档链接:
discord.Message, discord.Member, wait_for
额外说明:
不需要回应 Interaction,self.message
可以用 interaction.message
或更具上下文的 interaction.channel
代替。
英文:
You can use discord.Member
's attribute mentions
which returns a list of mentions.
class LevelButtons(discord.ui.View):
@discord.ui.button(label="Setting up first category", style=discord.ButtonStyle.green)
async def conf_level(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.message.channel.send("Send a @user who will be in the first category")
def check(m: discord.Member):
return m == discord.Member and m.channel == self.message.channel
try:
m = await self.bot.wait_for('message', timeout=10, check=check)
except asyncio.TimeoutError:
await self.message.channel.send('Received no input so operation was aborted')
else:
self.first_level.append(m.mentions[0]) # Takes first mention in message
await self.message.channel.send(f"{m.mentions[0]} added in the first category")
Also, I assume you want to check if the message author is the button user, simply change m == discord.Member
to
def check(m):
return m.author.id == interaction.user.id and m.channel == self.message.channel
Keep in mind that bot.wait_for('message')
returns discord.Message
Documentation:
discord.Message, discord.Member, wait_for
Additional notes:
Interaction is not responded to, self.message
is unnecessary with interaction.message
or more contextually interaction.channel
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论