我在 python 中使用 discord API 来管理 Discord 机器人。此命令创建某个不和谐 channel 中的人员列表。
我有一个函数:
async def attendance(ctx, channel):
# code here that creates a variable with member names
await bot.say(printdiscordnames)
我想每次用列表中的不同 channel 名称调用上面的函数
所以我会这样做:
async def attendanceall(ctx):
channel_list = ['voice1', 'voice2', 'voice3']
for item in channel_list:
attendance(item)
基本上,我想在 discord 中执行 !attendanceall,它会执行第一个函数,即创建一个列表并以 discord 形式为列表中的每个 channel 打印它。
我的问题是我不知道如何为列表中的每个 channel 名称调用第一个函数。
最佳答案
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))
关于python - 如何为列表中的每个项目多次调用 discord.py 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53826516/