Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit ff9992a

Browse files
committed
feat(howsign): allow searching multiple words
1 parent c321bf6 commit ff9992a

File tree

3 files changed

+120
-18
lines changed

3 files changed

+120
-18
lines changed

bot.py

+30-15
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
# -----------------------------------------------------------------------------
6060

61-
_spoiler_pattern = re.compile(r"\s*\|\|\s*(\S*)\s*\|\|\s*")
61+
_spoiler_pattern = re.compile(r"\s*\|\|\s*(.*)\s*\|\|\s*")
6262

6363

6464
def get_spoiler_text(val: str) -> Optional[str]:
@@ -126,36 +126,51 @@ async def on_ready():
126126
HOWSIGN_HELP = """Look up a word or phrase
127127
128128
If the word or phrase is sent in spoiler text, i.e. enclosed in `||`, the word will also be blacked out in the reply.
129+
To search multiple words/phrases, separate the values with a comma.
129130
130131
Examples:
131132
{COMMAND_PREFIX}howsign tiger
132133
{COMMAND_PREFIX}howsign ||tiger||
133134
{COMMAND_PREFIX}howsign what's up
135+
{COMMAND_PREFIX}howsign church, chocolate, computer
134136
""".format(
135137
COMMAND_PREFIX=COMMAND_PREFIX
136138
)
137139

138140

141+
def word_display(word: str, *, has_spoiler: bool):
142+
quoted_word = quote_plus(word)
143+
template = HOWSIGN_SPOILER_TEMPLATE if has_spoiler else HOWSIGN_TEMPLATE
144+
return template.format(
145+
word_uppercased=word.upper(),
146+
lifeprint=f"https://www.google.com/search?&q=site%3Alifeprint.com+{quoted_word}",
147+
handspeak=f"https://www.google.com/search?&q=site%3Ahandspeak.com+{quoted_word}",
148+
signingsavvy=f"https://www.signingsavvy.com/search/{quoted_word}",
149+
spread_the_sign=f"https://www.spreadthesign.com/en.us/search/?q={quoted_word}",
150+
youglish=f"https://youglish.com/pronounce/{quoted_word}/signlanguage/asl",
151+
)
152+
153+
139154
def howsign_impl(word: str):
140155
spoiler = get_spoiler_text(word)
141156
word = spoiler if spoiler else word
142-
title = f"||{word.upper()}||" if spoiler else word.upper()
143-
template = HOWSIGN_SPOILER_TEMPLATE if spoiler else HOWSIGN_TEMPLATE
144157
logger.info(f"sending links for: '{word}'")
145-
quoted_word = quote_plus(word)
146-
return {
147-
"embed": discord.Embed(
158+
has_multiple = "," in word
159+
if has_multiple:
160+
words = word.split(",")
161+
embed = discord.Embed()
162+
for word in words:
163+
word = word.strip()
164+
title = f"||{word.upper()}||" if spoiler else word.upper()
165+
embed.add_field(name=title, value=word_display(word, has_spoiler=spoiler))
166+
else:
167+
title = f"||{word.upper()}||" if spoiler else word.upper()
168+
embed = discord.Embed(
148169
title=title,
149-
description=template.format(
150-
word_uppercased=word.upper(),
151-
lifeprint=f"https://www.google.com/search?&q=site%3Alifeprint.com+{quoted_word}",
152-
handspeak=f"https://www.google.com/search?&q=site%3Ahandspeak.com+{quoted_word}",
153-
signingsavvy=f"https://www.signingsavvy.com/search/{quoted_word}",
154-
spread_the_sign=f"https://www.spreadthesign.com/en.us/search/?q={quoted_word}",
155-
youglish=f"https://youglish.com/pronounce/{quoted_word}/signlanguage/asl",
156-
),
170+
description=word_display(word, has_spoiler=spoiler),
157171
)
158-
}
172+
173+
return {"embed": embed}
159174

160175

161176
@bot.command(name="howsign", aliases=("sign",), help=HOWSIGN_HELP)

tests/__snapshots__/test_bot.ambr

+79-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@
116116
',
117117
}
118118
---
119+
# name: test_howsign[need, ask]
120+
<class 'dict'> {
121+
'embed': <class 'Embed'> {
122+
Empty=Embed.Empty,
123+
author=EmbedProxy(),
124+
color=Embed.Empty,
125+
colour=Embed.Empty,
126+
description=Embed.Empty,
127+
fields=<class 'list'> [
128+
EmbedProxy(inline=True, name='NEED', value='[👋 **Handspeak** - Search results](https://www.google.com/search?&q=site%3Ahandspeak.com+need)\n[🧬 **Lifeprint** - Search results](https://www.google.com/search?&q=site%3Alifeprint.com+need)\n[🤝 **SigningSavvy** - Sign for NEED](https://www.signingsavvy.com/search/need)\n[🌐 **Spread The Sign** - NEED](https://www.spreadthesign.com/en.us/search/?q=need)\n[📹 **YouGlish** - ASL videos containing NEED](https://youglish.com/pronounce/need/signlanguage/asl)\n'),
129+
EmbedProxy(inline=True, name='ASK', value='[👋 **Handspeak** - Search results](https://www.google.com/search?&q=site%3Ahandspeak.com+ask)\n[🧬 **Lifeprint** - Search results](https://www.google.com/search?&q=site%3Alifeprint.com+ask)\n[🤝 **SigningSavvy** - Sign for ASK](https://www.signingsavvy.com/search/ask)\n[🌐 **Spread The Sign** - ASK](https://www.spreadthesign.com/en.us/search/?q=ask)\n[📹 **YouGlish** - ASL videos containing ASK](https://youglish.com/pronounce/ask/signlanguage/asl)\n'),
130+
],
131+
footer=EmbedProxy(),
132+
image=EmbedProxy(),
133+
provider=EmbedProxy(),
134+
thumbnail=EmbedProxy(),
135+
timestamp=Embed.Empty,
136+
title=Embed.Empty,
137+
type='rich',
138+
url=Embed.Empty,
139+
video=EmbedProxy(),
140+
},
141+
}
142+
---
119143
# name: test_howsign[tiger]
120144
<class 'dict'> {
121145
'embed': <class 'Embed'> {
@@ -128,7 +152,7 @@
128152
[🧬 **Lifeprint** - Search results](https://www.google.com/search?&q=site%3Alifeprint.com+tiger)
129153
[🤝 **SigningSavvy** - Sign for TIGER](https://www.signingsavvy.com/search/tiger)
130154
[🌐 **Spread The Sign** - TIGER](https://www.spreadthesign.com/en.us/search/?q=tiger)
131-
[📹 **YouGlish** - Videos containing TIGER](https://youglish.com/pronounce/tiger/signlanguage/asl)
155+
[📹 **YouGlish** - ASL videos containing TIGER](https://youglish.com/pronounce/tiger/signlanguage/asl)
132156

133157
',
134158
fields=<class 'list'> [
@@ -157,7 +181,7 @@
157181
[🧬 **Lifeprint** - Search results](https://www.google.com/search?&q=site%3Alifeprint.com+what%27s+up)
158182
[🤝 **SigningSavvy** - Sign for WHAT'S UP](https://www.signingsavvy.com/search/what%27s+up)
159183
[🌐 **Spread The Sign** - WHAT'S UP](https://www.spreadthesign.com/en.us/search/?q=what%27s+up)
160-
[📹 **YouGlish** - Videos containing WHAT'S UP](https://youglish.com/pronounce/what%27s+up/signlanguage/asl)
184+
[📹 **YouGlish** - ASL videos containing WHAT'S UP](https://youglish.com/pronounce/what%27s+up/signlanguage/asl)
161185

162186
',
163187
fields=<class 'list'> [
@@ -174,6 +198,30 @@
174198
},
175199
}
176200
---
201+
# name: test_howsign[||need, ask||]
202+
<class 'dict'> {
203+
'embed': <class 'Embed'> {
204+
Empty=Embed.Empty,
205+
author=EmbedProxy(),
206+
color=Embed.Empty,
207+
colour=Embed.Empty,
208+
description=Embed.Empty,
209+
fields=<class 'list'> [
210+
EmbedProxy(inline=True, name='||NEED||', value='[👋 **Handspeak** - Search results](https://www.google.com/search?&q=site%3Ahandspeak.com+need)\n[🧬 **Lifeprint** - Search results](https://www.google.com/search?&q=site%3Alifeprint.com+need)\n[🤝 **SigningSavvy** - Sign for ||NEED||](https://www.signingsavvy.com/search/need)\n[🌐 **Spread The Sign** - ||NEED||](https://www.spreadthesign.com/en.us/search/?q=need)\n[📹 **YouGlish** - Videos containing ||NEED||](https://youglish.com/pronounce/need/signlanguage/asl)\n'),
211+
EmbedProxy(inline=True, name='||ASK||', value='[👋 **Handspeak** - Search results](https://www.google.com/search?&q=site%3Ahandspeak.com+ask)\n[🧬 **Lifeprint** - Search results](https://www.google.com/search?&q=site%3Alifeprint.com+ask)\n[🤝 **SigningSavvy** - Sign for ||ASK||](https://www.signingsavvy.com/search/ask)\n[🌐 **Spread The Sign** - ||ASK||](https://www.spreadthesign.com/en.us/search/?q=ask)\n[📹 **YouGlish** - Videos containing ||ASK||](https://youglish.com/pronounce/ask/signlanguage/asl)\n'),
212+
],
213+
footer=EmbedProxy(),
214+
image=EmbedProxy(),
215+
provider=EmbedProxy(),
216+
thumbnail=EmbedProxy(),
217+
timestamp=Embed.Empty,
218+
title=Embed.Empty,
219+
type='rich',
220+
url=Embed.Empty,
221+
video=EmbedProxy(),
222+
},
223+
}
224+
---
177225
# name: test_howsign[||tiger||]
178226
<class 'dict'> {
179227
'embed': <class 'Embed'> {
@@ -203,3 +251,32 @@
203251
},
204252
}
205253
---
254+
# name: test_howsign[||what's up||]
255+
<class 'dict'> {
256+
'embed': <class 'Embed'> {
257+
Empty=Embed.Empty,
258+
author=EmbedProxy(),
259+
color=Embed.Empty,
260+
colour=Embed.Empty,
261+
description='
262+
[👋 **Handspeak** - Search results](https://www.google.com/search?&q=site%3Ahandspeak.com+what%27s+up)
263+
[🧬 **Lifeprint** - Search results](https://www.google.com/search?&q=site%3Alifeprint.com+what%27s+up)
264+
[🤝 **SigningSavvy** - Sign for ||WHAT'S UP||](https://www.signingsavvy.com/search/what%27s+up)
265+
[🌐 **Spread The Sign** - ||WHAT'S UP||](https://www.spreadthesign.com/en.us/search/?q=what%27s+up)
266+
[📹 **YouGlish** - Videos containing ||WHAT'S UP||](https://youglish.com/pronounce/what%27s+up/signlanguage/asl)
267+
268+
',
269+
fields=<class 'list'> [
270+
],
271+
footer=EmbedProxy(),
272+
image=EmbedProxy(),
273+
provider=EmbedProxy(),
274+
thumbnail=EmbedProxy(),
275+
timestamp=Embed.Empty,
276+
title="||WHAT'S UP||",
277+
type='rich',
278+
url=Embed.Empty,
279+
video=EmbedProxy(),
280+
},
281+
}
282+
---

tests/test_bot.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@
88
random.seed(1)
99

1010

11-
@pytest.mark.parametrize("word", ("tiger", "||tiger||", "what's up"))
11+
@pytest.mark.parametrize(
12+
"word",
13+
(
14+
"tiger",
15+
"||tiger||",
16+
"what's up",
17+
"||what's up||",
18+
"need, ask",
19+
"||need, ask||",
20+
),
21+
)
1222
def test_howsign(snapshot, word):
1323
result = bot.howsign_impl(word)
1424
assert result == snapshot

0 commit comments

Comments
 (0)