Sub commands in discord.py?

Viewed 1040

I’m writing a simple bot for a discord server, and I’m trying to explore a few different ideas and two of them require some kind of response and sub command based on responses. I want people to be able to poll something without it being done through reacting to a bots post. I’m unsure of how to go about this.

I’ll give one as an example.

Ideally, it could be structured something like this.

[initiate poll command] [poll name]

Then to respond, a user could do something like this:

[poll name] [yes/no]

Or for a pseudo-code example:

!start_poll Lets_do_this

!lets_do_this yes

Then if it meets a certain threshold of yes’s, a sub command would execute.

1 Answers

My idea would be to create a dictionary. Whenever someone starts a poll, you would add two keys to the dictionary: "[name]_yes", and "[name]_no". Each key would have an empty list. I would then use a command such as "!vote [poll] [yes/no]" to simplify the coding, and whenever somebody voted, I would add their discord ID to the corresponding list. It would be a good idea to check if their name is already on a list, however. Then if you wanted, you could take the size of each list to see how many people have voted for each. The dictionary would end up looking something like:

{
    'lets_do_this_yes': ['Bob', 'Suzy'],
    'lets_do_this_no': ['Dan'],
    'lets_do_this_instead_yes': ['Dan','Joe'],
    'lets_do_this_instead_no': ['Suzy']
}

Note: I haven't used discord.py, but I have experience with Python and Discord.JS

Hopefully this works for you.

Edit: The result example above would not actually have their Discord name, but rather their Discord ID, as that is unique to them where as their name can be changed.

Related