[英]How do I make another command run when a button is pressed in discord?
我正在尝试向我的机器人添加按钮,并且我希望在单击按钮时执行另一个已经存在的命令。 如何才能做到这一点? 例如,当您按下“1”按钮时,将执行“Hi”
@client.command()
async def Hi(ctx):
await ctx.send('Hi')
@client.command(pass_context = True)
async def test(ctx):
await ctx.send(
embed=discord.Embed(title="text"),
components=[
Button(style=ButtonStyle.green, label="1")
]
)
response = await client.wait_for("button_click")
if response.channel == ctx.channel:
我希望这是您正在寻找的(我假设您使用的是 discord.py)。 我查看了这个问题,发现了三个问题:
components=[ Button(style=ButtonStyle.green, label="1") ] )
除非您使用的是另一个库,否则我注意到它无法工作。
以及修复了使用 if await client.wait_for("button_click")
并将其更改为await bot.wait_for("reaction_add")
的正确用法。
最后,if 语句不起作用,因此我将其替换为 check 函数,如果用户使用正确的1️⃣
表情符号做出响应,该函数将返回一个值。
@client.command(pass_context = True)
async def test(ctx):
message = await ctx.send(embed=discord.Embed(title="Text"))
emoji = '1️⃣' # The emoji you want the bot to react with
await message.add_reaction(emoji) # Bot reacts with that emoji you assigned
def check(response, user):
return str(response.emoji) in emoji # Checks that the user has responded with the correct emoji
response, user = await client.wait_for("reaction_add") # Waits for the user to react with the emoji
await ctx.send("Hi!")
希望这有效并解决您的问题。 祝你的项目好运!
您可以从另一个命令调用该命令。 不过,在这种情况下,命令真的很小,你最好把它放在那里。
请参阅文档中的此处,但请注意使用转换器、检查、冷却、预调用或调用后挂钩,因为它们没有被处理。
@client.command()
async def Hi(ctx):
await ctx.send('Hi')
# then do this in a command:
# ...
# no arguments as it is right now
await ctx.invoke(Hi)
# or for other commands if you need arguments
# sample_command(ctx, number: int, string: str)
await ctx.invoke(sample_command, 20, 'stack')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.