简体   繁体   中英

How do I run create a discord.py bot command that will run another bot command multiple times with different parameters?

I have a command that is !attendance channel, where channel is the voice channel you want read. The bot then prints a list of members in that channel in discord.

My question is if there is a way to have a list of channels and have the bot run through every one with only 1 command. For example doing !attendanceall and having the bot give 3 different channels in a list to the !attendance command.

Ive tried making another command and calling the attendance command but it does not work.

@bot.command(pass_context = True)
async def attendanceall(ctx):
    voice_list = ['channel1', 'channel2']
    for item in voice_list:
        attendance(item)

# the start of the attendance command if the variables matter
bot.command(pass_context = True)
async def attendance(ctx, channel : str = None, useDiscordID : bool = False):
    # the code that creates a list of all members goes here that isnt important
    # eventually I tell the bot to send the list of members
    await ctx.send(attendancelist)

I want to have a fixed list called voice_list that when using the !attendanceall command pings the !attendance command and runs it for each list item.

It's probably better to separate the logic of whatever you're doing from the commands. So if you had commands to do some operation on a specific channel or all channels that could look something like:

@bot.command()
async def do_to_all(ctx):
    for channel in ctx.guild.channels:
        await do_thing(channel)

@bot.command()
async def do_for_channel(ctx, channel: discord.GuildChannel):
    await do_thing(channel)

async def do_thing(channel):
    ...

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