简体   繁体   中英

How do I call a discord.py function multiple times for each item in a list?

Im using discord api in python to manage a discord bot. This command creates a list of people in a certain discord channel.

I have a function:

async def attendance(ctx, channel):
    code here that creates a variable with member names
    await bot.say(printdiscordnames)

I want to call the above function each time with a different channel name in a list

So I would do:

async def attendanceall(ctx):
    channel_list = ['voice1', 'voice2', 'voice3']
    for item in channel_list:
        attendance(item)

Basically I want to do !attendanceall in discord and it will do the first function which makes a list and prints it in discord for each channel in the list.

My issue is I dont know how to call the first function for each channel name in the list.

async def attendanceall(ctx):
channel_list = ['voice1', 'voice2', 'voice3']
for item in channel_list:
    asyncio.get_event_loop().create_task(attendance(item))

you can do this to run an asynchronous function or use the one below as another way of running it

async def attendanceall(ctx):
channel_list = ['voice1', 'voice2', 'voice3']
for item in channel_list:
    client.loop.create_task(attendance(item))

try:

async def attendanceall(ctx):
    channel_list = ['voice1', 'voice2', 'voice3']
    for item in channel_list:
        await attendance(item)

refer this link for help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM