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

Commit 2a93b3a

Browse files
authored
fix: store idioms and sentence locally (#15)
1 parent de25578 commit 2a93b3a

File tree

8 files changed

+1896
-27
lines changed

8 files changed

+1896
-27
lines changed

NOTICE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
6868
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
6969
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
7070
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
71+
72+
73+
sentences.json and phrases.json came from https://randomwordgenerator.com/ with permission from the website's maintainers.

bot.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from typing import Optional
99
from urllib.parse import quote_plus
1010

11-
import aiohttp
1211
import discord
1312
import gspread
1413
from discord.ext import commands
@@ -320,13 +319,6 @@ async def feedback_error(ctx, error):
320319
# -----------------------------------------------------------------------------
321320

322321

323-
async def get_random_sentence() -> str:
324-
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as client:
325-
resp = await client.get("https://randomwordgenerator.com/json/sentences.json")
326-
data = await resp.json()
327-
return random.choice(data["data"])["sentence"]
328-
329-
330322
SENTENCE_HELP = """Display a random sentence
331323
332324
Enter {COMMAND_PREFIX}sentence || to display the sentence in spoiler text.
@@ -335,9 +327,8 @@ async def get_random_sentence() -> str:
335327
)
336328

337329

338-
@bot.command(name="sentence", help=SENTENCE_HELP)
339-
async def sentence_command(ctx, spoiler: Optional[str]):
340-
sentence = await get_random_sentence()
330+
def sentence_impl(spoiler: Optional[str]):
331+
sentence = catchphrase.sentence()
341332
should_spoil = spoiler and spoiler.startswith("||")
342333
if should_spoil:
343334
sentence = f"||{sentence}||"
@@ -347,19 +338,15 @@ async def sentence_command(ctx, spoiler: Optional[str]):
347338
else f"sending random sentence: '{sentence}'"
348339
)
349340
logger.info(log)
350-
await ctx.send(sentence)
341+
return {"content": sentence}
351342

352343

353-
# -----------------------------------------------------------------------------
344+
@bot.command(name="sentence", help=SENTENCE_HELP)
345+
async def sentence_command(ctx, spoiler: Optional[str]):
346+
await ctx.send(**sentence_impl(spoiler=spoiler))
354347

355348

356-
async def get_random_idiom() -> dict:
357-
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as client:
358-
resp = await client.get(
359-
"https://randomwordgenerator.com/json/phrases.json",
360-
)
361-
data = await resp.json()
362-
return random.choice(data["data"])
349+
# -----------------------------------------------------------------------------
363350

364351

365352
IDIOM_HELP = """Display a random English idiom
@@ -378,9 +365,8 @@ async def get_random_idiom() -> dict:
378365
"""
379366

380367

381-
@bot.command(name="idiom", help=IDIOM_HELP)
382-
async def idiom_command(ctx, spoiler: Optional[str]):
383-
data = await get_random_idiom()
368+
def idiom_impl(spoiler: Optional[str]):
369+
data = catchphrase.idiom()
384370
should_spoil = spoiler and spoiler.startswith("||")
385371
log = (
386372
f"sending random idiom in spoiler text: {data}"
@@ -389,7 +375,15 @@ async def idiom_command(ctx, spoiler: Optional[str]):
389375
)
390376
logger.info(log)
391377
template = IDIOM_SPOILER_TEMPLATE if should_spoil else IDIOM_TEMPLATE
392-
await ctx.send(template.format(idiom=data["phrase"], meaning=data["meaning"]))
378+
content = template.format(idiom=data["phrase"], meaning=data["meaning"])
379+
return {
380+
"content": content,
381+
}
382+
383+
384+
@bot.command(name="idiom", help=IDIOM_HELP)
385+
async def idiom_command(ctx, spoiler: Optional[str]):
386+
await ctx.send(**idiom_impl(spoiler))
393387

394388

395389
# -----------------------------------------------------------------------------

lib/catchphrase/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
import itertools
22
import random
33
from pathlib import Path
4+
import json
45

56
import yaml
67

78

89
HERE = Path(__file__).parent
910

1011
with (HERE / "game_words.yaml").open("r") as fp:
11-
_game_words = yaml.load(fp, Loader=yaml.SafeLoader)
12+
CATCHPHRASE = yaml.load(fp, Loader=yaml.SafeLoader)["catchphrase"]
13+
with (HERE / "sentences.json").open("r") as fp:
14+
SENTENCES = json.load(fp)["data"]
15+
with (HERE / "phrases.json").open("r") as fp:
16+
IDIOMS = json.load(fp)["data"]
1217

13-
CATCHPHRASE = _game_words["catchphrase"]
1418
CATEGORIES = list(CATCHPHRASE.keys())
1519
ALL_WORDS = list(itertools.chain(*CATCHPHRASE.values()))
1620

1721

1822
def catchphrase(category: str = None):
1923
word_list = CATCHPHRASE[category] if category else ALL_WORDS
2024
return random.choice(word_list)
25+
26+
27+
def sentence():
28+
return random.choice(SENTENCES)["sentence"]
29+
30+
31+
def idiom():
32+
return random.choice(IDIOMS)

lib/catchphrase/phrases.json

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{"data":[
2+
{"phrase":"A Chip on Your Shoulder","meaning":"Being angry about something that happened in the past."},
3+
{"phrase":"A Dime a Dozen","meaning":"Something that is extremely common."},
4+
{"phrase":"A Fool and His Money are Soon Parted","meaning":"It's easy for a fool to lose his/her money."},
5+
{"phrase":"A Piece of Cake","meaning":"A task that is simple to accomplish."},
6+
{"phrase":"An Arm and a Leg","meaning":"Something that is extremely expensive."},
7+
{"phrase":"All Greek To Me","meaning":"When something is incomprehensible due to complexity; unintelligble."},
8+
{"phrase":"Back to Square One","meaning":"To go back to the beginning; back to the drawing board."},
9+
{"phrase":"Back To the Drawing Board","meaning":"Starting over again on a new design from a previously failed attempt."},
10+
{"phrase":"Barking Up The Wrong Tree","meaning":"To make a wrong assumption about something."},
11+
{"phrase":"Beating a Dead Horse","meaning":"To bring up an issue that has already been resolved."},
12+
{"phrase":"Beating Around the Bush","meaning":"Someone who is beating around the bush is someone who avoids the main point."},
13+
{"phrase":"Between a Rock and a Hard Place","meaning":"Being faced with two difficult choices."},
14+
{"phrase":"Birds of a Feather Flock Together","meaning":"People tend to associate with others who share similar interests or values."},
15+
{"phrase":"Break The Ice","meaning":"Breaking down a social stiffness."},
16+
{"phrase":"Burst Your Bubble","meaning":"To ruin someone's happy moment."},
17+
{"phrase":"Close But No Cigar","meaning":"Coming close to a successful outcome only to fall short at the end."},
18+
{"phrase":"Cry Over Spilt Milk","meaning":"It's useless to worry about things that  already happened and cannot be changed."},
19+
{"phrase":"Cry Wolf","meaning":"Someone that calls for help when it is not needed. Someone who is lying."},
20+
{"phrase":"Cup Of Joe","meaning":"A cup of joe is an American nickname for a cup of coffee."},
21+
{"phrase":"Curiosity Killed The Cat","meaning":"Typically said to indicate that any further investigation into a situation may lead to harm."},
22+
{"phrase":"Cut The Mustard","meaning":"To cut the mustard is to meet a required standard, or to meet expectations."},
23+
{"phrase":"Cut To The Chase","meaning":"To get to the point, leaving out all of the unnecessary details."},
24+
{"phrase":"Don't Count Your Chickens Before They Hatch","meaning":"Do not rely on something you are not sure of."},
25+
{"phrase":"Don't Look a Gift Horse In The Mouth","meaning":"When you receive a gift from someone, do not be ungrateful."},
26+
{"phrase":"Down And Out","meaning":"(1) A term used in a boxing. (2) Someone who has become incapacitated."},
27+
{"phrase":"Down For The Count","meaning":"Someone or something that looks to be defeated, or nearly so."},
28+
{"phrase":"Down To Earth","meaning":"Practical or humble; unpretentious."},
29+
{"phrase":"Down To The Wire","meaning":"A tense situation where the outcome is decided only in the last few seconds."},
30+
{"phrase":"Drawing a Blank","meaning":"Failing to recall a memory. Unable to remember something."},
31+
{"phrase":"Drive Me Nuts","meaning":"To greatly frustrate someone. To drive someone crazy, insane, bonkers, or bananas."},
32+
{"phrase":"Dropping Like Flies","meaning":"To fall down ill or to die in large numbers."},
33+
{"phrase":"Easy As Pie","meaning":"Something that is easy."},
34+
{"phrase":"Eat My Hat","meaning":"Having confidence in a specific outcome; being almost sure about something."},
35+
{"phrase":"Elephant in the Room","meaning":"Ignoring a large, obvious problem or failing to address an issue that stands out in a major way."},
36+
{"phrase":"Elvis Has Left The Building","meaning":"Something that is all over."},
37+
{"phrase":"Every Cloud Has a Silver Lining","meaning":"To be optimistic, even in difficullt times."},
38+
{"phrase":"Everything But The Kitchen Sink","meaning":"Including nearly everything possible."},
39+
{"phrase":"Fight Fire With Fire","meaning":"To retaliate with an attack that is similar to the attack used against you."},
40+
{"phrase":"Fish Out Of Water","meaning":"Someone being in a situation that they are unfamiliar or unsuited for."},
41+
{"phrase":"Fit as a Fiddle","meaning":"Being fit as a fiddle means to be in perfect health."},
42+
{"phrase":"Flea Market","meaning":"A type of bazaar where inexpensive goods are sold or bartered."},
43+
{"phrase":"Foaming At The Mouth","meaning":"To be enraged and show it."},
44+
{"phrase":"Fool's Gold","meaning":"Iron pyrities is a worthless mineral that resembles gold."},
45+
{"phrase":"Give a Man a Fish","meaning":"It's better to teach a person how to do something than to do that something for them."},
46+
{"phrase":"Go For Broke","meaning":"To risk it all, even if it means losing everything. To go all out."},
47+
{"phrase":"Go Out On a Limb","meaning":"Putting yourself in a risky situation in order to help someone; or to hazard a guess."},
48+
{"phrase":"Goody Two-Shoes","meaning":"A smugly virtuous person."},
49+
{"phrase":"Greased Lightning","meaning":"Very fast or quick."},
50+
{"phrase":"Hands Down","meaning":"Anything that's easy or has no difficulty; something that is a certainty."},
51+
{"phrase":"Happy as a Clam","meaning":"The state of being happy; feeling delighted."},
52+
{"phrase":"Hard Pill to Swallow","meaning":"Something that's difficult to accept."},
53+
{"phrase":"Head Over Heels","meaning":"Falling deeply in love with another person."},
54+
{"phrase":"Heads Up","meaning":"Used as an advanced warning. To become keenly aware."},
55+
{"phrase":"Hear, Hear","meaning":"A shout of agreement, or to draw attention to a speaker."},
56+
{"phrase":"High And Dry","meaning":"To be left behind; abandoned. Being in a helpless situation without a way to recover."},
57+
{"phrase":"Hit Below The Belt","meaning":"A boxing term. Also often used to refer to inappropriate words, or comments that are too personal."},
58+
{"phrase":"I Smell a Rat","meaning":"A feeling that something is not quite right, or awry."},
59+
{"phrase":"If You Can't Stand the Heat, Get Out of the Kitchen","meaning":"One should discontinue with a task if they are unable to cope with it due to pressure."},
60+
{"phrase":"In a Pickle","meaning":"Being in a difficult predicament; a mess; an undesirable situation."},
61+
{"phrase":"In the Red","meaning":"Losing money. Being in debt."},
62+
{"phrase":"It's Not All It's Cracked Up To Be","meaning":"Failing to meet expectations; not being as good as people say."},
63+
{"phrase":"It's Not Brain Surgery","meaning":"A task that's easy to accomplish, a thing lacking complexity."},
64+
{"phrase":"Jack of All Trades Master of None","meaning":"Having suitable skill in multiple things, but not being an expert in any of them."},
65+
{"phrase":"Jaws of Death","meaning":"Being in a dangerous or very deadly situation."},
66+
{"phrase":"Jaws of Life","meaning":"Usually this references a tool used by rescuers when they pry or cut open a car to save the occupant."},
67+
{"phrase":"Jig Is Up","meaning":"For a ruse or trick to be discovered; to be caught."},
68+
{"phrase":"Jumping the Gun","meaning":"Something that occurs too early before preparations are ready. Starting too soon."},
69+
{"phrase":"Keep On Truckin'","meaning":"To keep going, pressing forward; never stopping."},
70+
{"phrase":"Keep Your Eyes Peeled","meaning":"To be watchful; paying careful attention to something."},
71+
{"phrase":"Keep Your Shirt On","meaning":"Keeping calm. Usually said by someone who is trying to avoid making others upset."},
72+
{"phrase":"Knock Your Socks Off","meaning":"To be taken by surprise."},
73+
{"phrase":"Know the Ropes","meaning":"Having a familiarity or understanding of how something works."},
74+
{"phrase":"Knuckle Down","meaning":"Getting sincere about something; applying oneself seriously to a job."},
75+
{"phrase":"Let Her Rip","meaning":"Permission to start, or it could mean 'go faster!'"},
76+
{"phrase":"Lickety Split","meaning":"To go at a quick pace; no delaying!"},
77+
{"phrase":"Like Father Like Son","meaning":"Resembling one's parents in terms of appearance or behavior."},
78+
{"phrase":"Long In The Tooth","meaning":"Old in age. Mainly used when referring to people or horses."},
79+
{"phrase":"Love Birds","meaning":"A pair of people who have a shared love for each other."},
80+
{"phrase":"Lovey Dovey","meaning":"The affectionate stuff that people do when they are in love, such as kissing and hugging."},
81+
{"phrase":"Man of Few Words","meaning":"A person who does not speak a great deal; someone who talks with as few words as possible."},
82+
{"phrase":"Money Doesn't Grow On Trees","meaning":"Suggests that money is a resource that must be earned and is not one that's easily acquired."},
83+
{"phrase":"Mountain Out of a Molehill","meaning":"One who escalates small things and turns them into big problems."},
84+
{"phrase":"Mouth-watering","meaning":"Delicious; something that looks or tastes appetizing."},
85+
{"phrase":"My Cup of Tea","meaning":"Someone or something that one finds to be agreeable or delightful."},
86+
{"phrase":"Needle In a Haystack","meaning":"Something that is impossible or extremely difficult to find, especially because the area you have to search is too large."},
87+
{"phrase":"No-Brainer","meaning":"Anything that requires minimal brain activity to accomplish."},
88+
{"phrase":"No Ifs, Ands, or Buts","meaning":"Finishing a task without making any excuses."},
89+
{"phrase":"Not the Sharpest Tool in the Shed","meaning":"Someone who isn't witty or sharp, but rather, they are ignorant, unintelligent, or senseless."},
90+
{"phrase":"Off One's Base","meaning":"A person that is crazy or behaving in idiotic ways"},
91+
{"phrase":"On Cloud Nine","meaning":"Having strong feelings of happiness or satisfaction. "},
92+
{"phrase":"On the Ropes","meaning":"Being in a situation that looks to be hopeless!"},
93+
{"phrase":"On the Same Page","meaning":"Thinking alike or understanding something in a similar way with others."},
94+
{"phrase":"Par For the Course","meaning":"What you would expect to happen; something normal or common."},
95+
{"phrase":"Playing For Keeps","meaning":"Said when things are about to get serious."},
96+
{"phrase":"Playing Possum","meaning":"Pretending to be dead, or to be deceitful about something."},
97+
{"phrase":"Plot Thickens - The","meaning":"A situation that has gotten way more serious or interesting due to recent complexities or developments."},
98+
{"phrase":"Poke Fun At","meaning":"Making fun of something or someone; ridicule."},
99+
{"phrase":"Put a Sock In It","meaning":"Asking someone to be quiet or to shut up."},
100+
{"phrase":"Quality Time","meaning":"Spending time with another to strengthen the relationship."},
101+
{"phrase":"Quick and Dirty","meaning":"Things that are fixed with great speed, but as a result, it's probably not going to work very well."},
102+
{"phrase":"Quick On the Draw","meaning":"Performing an action with the greatest of haste."},
103+
{"phrase":"Rain on Your Parade","meaning":"To spoil someone's fun or plans; ruining a pleasurable moment"},
104+
{"phrase":"Raining Cats and Dogs","meaning":"When it is raining heavily."},
105+
{"phrase":"Read 'Em and Weep","meaning":"Often said by the winner in poker, as the others 'weep' over the loss."},
106+
{"phrase":"Ride Him, Cowboy!","meaning":"A cheer people yell, usually at rodeos when cowboys are clinging to the backs of untamed horses."},
107+
{"phrase":"Right Off the Bat","meaning":"Immediately, done in a hurry; without delay."},
108+
{"phrase":"Right Out of the Gate","meaning":"Right from the beginning; to do something from the start."},
109+
{"phrase":"Ring Any Bells?","meaning":"Recalling a memory; causing a person to remember something or someone."},
110+
{"phrase":"Roll With the Punches","meaning":"To tolerate or endure through the unexpected mishappenings you may encounter from time to time."},
111+
{"phrase":"Scot-free","meaning":"Getting away freely from custody, punishment, or any type of risky situation."},
112+
{"phrase":"Short End of the Stick","meaning":"Getting the bad end of a deal, or receiving the least desirable outcome from something."},
113+
{"phrase":"Shot In the Dark","meaning":"An attempt that has little chance for success."},
114+
{"phrase":"Son of a Gun","meaning":"A person, usually one who is behaving badly."},
115+
{"phrase":"Swinging For the Fences","meaning":"Giving something your all."},
116+
{"phrase":"Talk the Talk","meaning":"Supporting what you say, not just with words, but also through action or evidence."},
117+
{"phrase":"There's No I in Team","meaning":"To not work alone, but rather, together with others in order to achieve a certain goal."},
118+
{"phrase":"Throw In the Towel","meaning":"Giving up; to surrender."},
119+
{"phrase":"Top Drawer","meaning":"High quality, exceptional; something that's very valuable."},
120+
{"phrase":"Tough It Out","meaning":"To remain resillient even in hard times; enduring."},
121+
{"phrase":"Tug of War","meaning":"It can refer to the popular rope pulling game or it can mean a struggle for authority."},
122+
{"phrase":"Two Down, One to Go","meaning":"Two things have been completed, but there is one more that has yet to be finished."},
123+
{"phrase":"Ugly Duckling","meaning":"One who may seem plain at first in appearance or capability, but later turns out to be beautiful or great."},
124+
{"phrase":"Under the Weather","meaning":"Not feeling well, in health or mood."},
125+
{"phrase":"Under Your Nose","meaning":"Missing something that should be really obvious."},
126+
{"phrase":"Up In Arms","meaning":"Angry; being roused to the point that you are ready to fight."},
127+
{"phrase":"Wake Up Call","meaning":"An occurance of sorts that brings a problem to somebody's attention and they realize it needs fixing."},
128+
{"phrase":"What Goes Up Must Come Down","meaning":"Things that go up must eventually return to the earth due to gravity."},
129+
{"phrase":"What Am I, Chopped Liver?","meaning":"A rhetorical question used by a person who feels they are being given less consideration than someone else."},
130+
{"phrase":"When the Rubber Hits the Road","meaning":"When something is about to begin, get serious, or put to the test."},
131+
{"phrase":"Wild Goose Chase","meaning":"Futilely pursuing something that will never be attainable."},
132+
{"phrase":"Wouldn't Harm a Fly","meaning":"Nonviolent; someone who is mild or gentle."},
133+
{"phrase":"Yada Yada","meaning":"A way to notify a person that what they're saying is predictable or boring."},
134+
{"phrase":"You Can't Judge a Book By Its Cover","meaning":"Don't judge someone or something only by the outward appearance."},
135+
{"phrase":"You Can't Teach an Old Dog New Tricks","meaning":"It can be challenging to teach a person something new."}
136+
]}

0 commit comments

Comments
 (0)