python - 如何为列表中的每个项目多次调用 discord.py 函数?

标签 python discord discord.py bots

我在 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/

相关文章:

discord.py - 是否可以在 discord.py 中按名称获取 channel ID

python - 如何在 python 中将 .dds 文件转换为 .png ?

Python正则表达式选取 "not include"单词

python - 从列表中创建组合,如果分隔符的子字符串位于列表项的 1 个以上子元素中,则删除组合

python - pygame中不播放背景音乐

javascript - Discord.js 嵌入图片剧透

python - Discord Python 机器人 : Changing Status

python - Discord NoneType 对象 Discord 文件中出现错误?

python - 如何在 replit 上运行音乐机器人 (discord.py)

python - Discord.py @bot.event