Skip to content

Commit f106f37

Browse files
authored
Vote improvements (#45)
* Updated the vote command to use the emoji module instead of regex * Updated changelog * Modified vote command so it reacts with emojis in the order they appeared
1 parent 19a2f2e commit f106f37

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Changed
10+
- Improved emoji parsing logic for the `vote` command
11+
712
## [0.14.0] - 2021-08-25
813

914
### Changed

commanderbot_ext/ext/vote/vote_cog.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
11
import re
22
from typing import Iterable, cast
33

4+
import emoji
45
from discord import Message
56
from discord.ext.commands import Bot, Cog, Context, command
67

78
DEFAULT_VOTE_EMOJIS = ("👍", "👎")
8-
9-
EMOJIS_PATTERN = re.compile(
10-
"["
11-
"\U0001F1E0-\U0001F1FF"
12-
"\U0001F300-\U0001F5FF"
13-
"\U0001F600-\U0001F64F"
14-
"\U0001F680-\U0001F6FF"
15-
"\U0001F700-\U0001F77F"
16-
"\U0001F780-\U0001F7FF"
17-
"\U0001F800-\U0001F8FF"
18-
"\U0001F900-\U0001F9FF"
19-
"\U0001FA00-\U0001FA6F"
20-
"\U0001FA70-\U0001FAFF"
21-
"\U00002702-\U000027B0"
22-
"\U000024C2-\U0001F251"
23-
"\U0000200d"
24-
"]+|"
25-
r"\<\:\w+\:\d+\>"
26-
"",
27-
flags=re.UNICODE,
28-
)
9+
CUSTOM_EMOJI_PATTERN = re.compile(r"\<\:\w+\:\d+\>")
2910

3011

3112
class VoteCog(Cog, name="commanderbot_ext.ext.vote"):
3213
def __init__(self, bot: Bot):
3314
self.bot: Bot = bot
3415

3516
@staticmethod
36-
def get_vote_emojis(message: Message) -> Iterable[str]:
37-
# If the message has content, look for emojis within it.
38-
if message.clean_content:
39-
# If any emojis were found, use them instead of the defaults.
40-
if found_emojis := EMOJIS_PATTERN.findall(str(message.clean_content)):
41-
return found_emojis
42-
# If nothing else was returned, use the default vote emojis.
43-
return DEFAULT_VOTE_EMOJIS
17+
def get_emojis(message: Message) -> Iterable[str]:
18+
# Get message content and cast it to a string
19+
message_content: str = str(message.clean_content)
20+
21+
# Find unicode and custom emojis in the message
22+
found_emojis: list[dict] = emoji.emoji_lis(message_content)
23+
for custom_emoji in CUSTOM_EMOJI_PATTERN.finditer(message_content):
24+
found_emojis.append(
25+
{"location": custom_emoji.start(), "emoji": custom_emoji.group()}
26+
)
27+
28+
# Return early with the default emojis if no emojis were found
29+
if not found_emojis:
30+
return DEFAULT_VOTE_EMOJIS
31+
32+
# Create a list of unique emojis that are sorted in the order they appeared
33+
emojis: list[str] = []
34+
for e in sorted(found_emojis, key=lambda i: i["location"]):
35+
emoji_char: str = str(e["emoji"])
36+
if emoji_char not in emojis:
37+
emojis.append(emoji_char)
38+
39+
return emojis
4440

4541
@command(name="vote")
4642
async def cmd_vote(self, ctx: Context):
4743
# Determine which emoji reactions to seed the message with, silently ignoring
4844
# errors raised by any individual emoji.
49-
for emoji in self.get_vote_emojis(cast(Message, ctx.message)):
45+
for emoji in self.get_emojis(cast(Message, ctx.message)):
5046
try:
5147
await ctx.message.add_reaction(emoji)
5248
except:

poetry.lock

Lines changed: 35 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ lectern = ">=0.14.0"
1919
beet = ">=0.32.1"
2020
aiohttp = "^3.7.4"
2121
PyYAML = "^5.4.1"
22+
emoji = "^1.4.2"
2223

2324
[tool.poetry.dev-dependencies]
2425
black = "^21.5b0"

0 commit comments

Comments
 (0)