discord.py - 在我的机器人提出问题后,如何获取命令用户的消息?
问题描述
我正在尝试创建一个 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)]
解决方案
正如我在评论中所说,这是一个基本示例,其中包含有关如何实现的代码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]}")
推荐阅读
- python - 如何使用代理字典中的随机代理向 url 发送 get 请求?
- r - 在每个服务的代码周围加上引号的快速方法
- html - 使用角度删除表中的行时如何刷新结果计数
- c# - SQL Server 和 C# 中 DateTime 的毫秒数不相同
- python - 无法理解 keras 密集层的输出
- python-3.x - 我应该在哪里修改我的广度优先搜索算法以找到 2 个节点之间的最短路径?
- jdbc - Can Kubernetes services deployed by helm configured to be restarted when manually deleting via kubectl?
- javascript - 如何使搜索图标在 ag-grid 的列中永久可见?
- database - 如何在一段时间后使用 postgresql 更新值
- html - 如何在 Span 中居中图像