首页 > 解决方案 > 如何为列表中的每个项目多次调用 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 ,它将执行第一个函数,该函数创建一个列表并为列表中的每个通道以不和谐的方式打印它。

我的问题是我不知道如何为列表中的每个频道名称调用第一个函数。

标签: pythondiscorddiscord.pybots

解决方案


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))

推荐阅读