首页 > 解决方案 > 在我的机器人提出问题后,如何获取命令用户的消息?

问题描述

我正在尝试创建一个 Discord 音乐机器人。当有人使用 !play(歌曲名称)时,它会在 Google 上搜索歌曲名称并获取所有带有“youtube.com/watch”的链接并下载。现在我想出了如何在终端本地执行此操作。问题是如果多个链接显示为“youtube.com/watch”,我不知道如何询问命令的作者他们想要什么歌曲选择这是我的代码:

ytlinks = searchForLink(link, 10)
# ask which link
if len(ytlinks) > 1:
    await ctx.send(str(ytlinks))
    await ctx.send("Which link do you want to use?")
    # want code here to take next message after the "Which link do you want to use?" and set to var "linkchoice"
    linkchoice = ''
    if (not int(linkchoice) <= 10) or (not int(linkchoice) >= 1):
        await ctx.send("Not an integer or not valid option")
    linkchoice = int(linkchoice) - 1
    ytlinks = ytlinks[int(linkchoice)]

标签: discord.py

解决方案


正如我在评论中所说,这是一个基本示例,其中包含有关如何实现的代码wait_for

#just hardcoding some list for demonstration purposes
ytlinks = ["https://youtube.com/123", "https://youtube.com/456", "https://youtube.com/789"]

if len(ytlinks) > 1:
    await ctx.send(str(ytlinks))
    await ctx.send("Which link do you want to use?")

    def check(message):
        return message.author == ctx.author

    try:
        message = await self.bot.wait_for('message', timeout=60.0, check=check)
    except asyncio.TimeoutError:
        ctx.send("You took too long to respond!")
        return
    else:
        linkchoice = message.content
        if (not int(linkchoice) <= 10) or (not int(linkchoice) >= 1):
            await ctx.send("Not an integer or not valid option")
            return
        await ctx.send(f"You chose link #{linkchoice}:\n{ytlinks[int(linkchoice)-1]}")

推荐阅读