python - 如何为列表中的每个项目多次调用 discord.py 函数?
问题描述
我在 python 中使用 discord API 来管理 Discord 机器人。此命令在某个不和谐频道中创建人员列表。
我有一个功能:
async def attendance(ctx, channel):
# code here that creates a variable with member names
await bot.say(printdiscordnames)
我想每次在列表中使用不同的频道名称调用上述函数
所以我会这样做:
async def attendanceall(ctx):
channel_list = ['voice1', 'voice2', 'voice3']
for item in channel_list:
attendance(item)
基本上我想做 !attendanceall in discord ,它将执行第一个函数,该函数创建一个列表并为列表中的每个通道以不和谐的方式打印它。
我的问题是我不知道如何为列表中的每个频道名称调用第一个函数。
解决方案
async def attendanceall(ctx):
channel_list = ['voice1', 'voice2', 'voice3']
for item in channel_list:
asyncio.get_event_loop().create_task(attendance(item))
您可以这样做来运行异步函数或使用下面的函数作为运行它的另一种方式
async def attendanceall(ctx):
channel_list = ['voice1', 'voice2', 'voice3']
for item in channel_list:
client.loop.create_task(attendance(item))
推荐阅读
- azure - 为什么 Azure 门户在授予 AD 应用程序权限时不再具有 Azure Key Vault 功能?
- git - 如何通过 Autohotkey 在 Git Bash 中读取命令的输出
- c++ - C++ 的网络摄像头 API
- android - Android 虚拟设备中的相机应用未显示
- jquery - 在Struts Not Updating中通过Jquery移动多个选择的项目
- powershell - 模块可用但未报告为已安装
- python-3.x - Python图表点击并过滤数据表
- cassandra - 如何使用键实现 cassandra 的分页?
- typescript - 解决 TypeScript 类型中的承诺
- node.js - 可以使用 CodeceptJS 在 chrome 浏览器上使用 devtools