Taerpe
Taerpe

Reputation: 145

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.

Upvotes: 1

Views: 5413

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61032

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

Upvotes: 1

Related Questions