简体   繁体   中英

How do I create a list with a command using discord.py?

I am making a discord bot that is going to make 2 teams out of a list that the user created with one command. I just don't know how to create a list out of the elements that the user inputs. I want the user to write for example !team ab c def which should create a list that looks like this: list = ["a", "b", "c", "d", "e", "f"] . I want the command that the user inputs in discord to start with !team , and after that the user is going to write all of the list elements.

Something like this:

async def on_message(self, message):
    items = message.content.split()
    print(items)

First, you might want to use a command rather than an on_message event like another user had suggested. To get a list from a command, you can use *args . You can view more about variables in commands in the discord.py docs. *args , when used, would return a list such as ('a', 'b', 'c') depending on the given input. Do view the example below.

@client.command() # or @bot.command() depending on what you're using
async def team(ctx, *args): # command would be run as !team a b c d e
    print(args) # printing how the list would look like
    await ctx.send(', '.join(args)) # makes it look neater rather than sending a normal list

Here is how the above command would look.

上面的命令工作

If you use commands instead on the on_messags event this will be easier:

@bot.command(name='listcommands')
async def list_commands(ctx, *message):
    List = message.split()
    await ctx.send(response)

But you could do this with an on_message event:

@client.event
asynce def on_message(message):
    If (message.author == client.user)
           return

    If message.content.startswith('$commands')
            List = message.content.split('$commands ', 1)

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