Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Allow individual rooms to have unique polls
Browse files Browse the repository at this point in the history
  • Loading branch information
TomCasavant committed Dec 21, 2019
1 parent 2b26bdd commit d347950
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Users vote by adding the matching emoji to the poll (i.e. if the first choice ha
## Version 2.0
- Changed voting format to reactions (instead of '!poll vote')

### Version 2.0.1
- Allows every room to have unique poll

## Wish List
- Add user configuration to only allow certain users to create polls
- Add auto-timing ability
- Add ability to run multiple polls at once
- Make emojis configurable
- Add placeholder emojis on each poll (to let users just click one)
2 changes: 1 addition & 1 deletion maubot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ maubot: 0.1.0
id: casavant.tom.poll

# A PEP 440 compliant version string.
version: 2.0.0
version: 2.0.1

# The SPDX license identifier for the plugin. https://spdx.org/licenses/
# Optional, assumes all rights reserved if omitted.
Expand Down
28 changes: 17 additions & 11 deletions poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def close_poll(self):


class PollPlugin(Plugin):
currentPoll = Poll("None", ["None"])
currentPolls = {}

@command.new("poll", help="Make a poll")
async def poll(self) -> None:
Expand All @@ -81,30 +81,36 @@ async def handler(self, evt: MessageEvent, poll_setup: str) -> None:
if len(choices) <= 1:
response = "You need to enter at least 2 choices."
else:
self.currentPoll = Poll(question, choices)
self.currentPolls[evt.room_id] = Poll(question, choices)
# Show users active poll
choice_list = "<br />".join(
[f"{self.currentPoll.emojis[i]} - {choice}" for i, choice in enumerate(choices)]
[f"{self.currentPolls[evt.room_id].emojis[i]} - {choice}" for i, choice in enumerate(choices)]
)
response = f"{question}<br />{choice_list}"
self.currentPoll.event_id = await evt.reply(response, allow_html=True)
self.currentPolls[evt.room_id].event_id = await evt.reply(response, allow_html=True)

@poll.subcommand("results", help="Prints out the current results of the poll")
async def handler(self, evt: MessageEvent) -> None:
await evt.mark_read()
await evt.reply(self.currentPoll.get_results(), allow_html=True)
if evt.room_id in self.currentPolls:
await evt.reply(self.currentPolls[evt.room_id].get_results(), allow_html=True)
else:
await evt.reply("There is no active poll in this room", allow_html=True)

@poll.subcommand("close", help="Ends the poll")
async def handler(self, evt: MessageEvent) -> None:
await evt.mark_read()
self.currentPoll.close_poll()
await evt.reply("This poll is now over. Type !poll results to see the results.")
if evt.room_id in self.currentPolls:
self.currentPolls[evt.room_id].close_poll()
await evt.reply("This poll is now over. Type !poll results to see the results.")
else:
await evt.reply("There is no active poll in this room")

@command.passive(regex=r"(?:("+'|'.join(REACTIONS) + r")[\U0001F3FB-\U0001F3FF]?)",
field=lambda evt: evt.content.relates_to.key,
event_type=EventType.REACTION, msgtypes=None)
async def get_react_vote(self, evt: ReactionEvent, _: Tuple[str]) -> None:
if (evt.content.relates_to.event_id == self.currentPoll.event_id): # Is this on the correct message?
if not self.currentPoll.hasVoted(evt.sender): # has the user already voted?
if (evt.content.relates_to.key in self.currentPoll.emojis): # Is this a possible choice?
self.currentPoll.vote(self.currentPoll.emojis.index(evt.content.relates_to.key), evt.sender) # Add vote/sender to poll
if (evt.content.relates_to.event_id == self.currentPolls[evt.room_id].event_id): # Is this on the correct message?
if not self.currentPolls[evt.room_id].hasVoted(evt.sender): # has the user already voted?
if (evt.content.relates_to.key in self.currentPolls[evt.room_id].emojis): # Is this a possible choice?
self.currentPolls[evt.room_id].vote(self.currentPolls[evt.room_id].emojis.index(evt.content.relates_to.key), evt.sender) # Add vote/sender to poll

0 comments on commit d347950

Please sign in to comment.