The first aid command in the Emergency cog (b.aid) uses a keyword-only argument to accept entire messages to parse for keywords. It splits the message into a list of words and aims to match those words against keys in a JSON file using the "in" builtin. Both the listed-argument word and the key are made lower-case as a hacky way of fuzzing the search.
It works terribly to the point of it being a bug.
"I'm having an asthma attack" returns 2 results and neither of them is the result for asthma.
Below the code is a sample of the JSON file. I have full copyright permission to display this information granted by its owner but cannot pass it on.
@commands.command()
async def aid(self, ctx, *, arg):
"""Searches the St John's Ambulance First Aid guides for anything that matches the text passed into the function"""
assert isinstance(arg, str), "I'm sorry, I can only help if you tell me your situation."
await ctx.send("Please wait a moment while I check if I have any resources for the situation")
circumstances = split("\s", arg)
print(circumstances)
#TODO Add a funnel layer to catch the broadest variation of synonyms to database entries.
with open("first_aid.json", "r", encoding="UTF-8") as repo:
resources = json.load(repo)
solutions = resources['items']
outcomes = []
for circumstance in circumstances:
print(circumstance)
for solution in solutions:
print(solution)
print(type(solution))
#TODO I know this is a hack, need to change for i18
#TODO This just isn't picking up obvious matches
if circumstance.lower() in solution['situation'].lower():
print(solution['situation'])
print(solution['url'])
outcomes.append((solution['situation'], solution['url'], solution['language']))
#TODO Mirror the St John's files on my server and add a 'pretty' layer for thumbnails
await ctx.send("I may have found something.")
new_order = [2, 0, 1, 3, 4, 5]
results = [outcomes[i] for i in new_order]
#TODO reintroduce the non-English languages in a future patch when you've figured out a more streamlined way to do this.
for result in results:
print(results)
print(result)
aid = str(result[0])
print(aid)
linkage = str(result[1])
print(linkage)
language = str(result[2])
print(language)
if language != "English":
break
else:
embed = discord.Embed(title="First Aid Card", type="rich", url='https://stjohn.org.au/first-aid-facts',
description="Based on your description, this is what my database has returned",
color=0xff0000)
embed.set_author(name="Brook Newlsy",
url='https://discordapp.com/oauth2/authorize?client_id=695245576475902002&permissions=8&scope=bot',
icon_url='https://i.imgur.com/bQSgvV1.jpg')
embed.set_thumbnail(url='https://i.imgur.com/KP6B8zx.png')
embed.add_field(name="Situation", value=aid, inline=True)
embed.add_field(name="Reference",
value=linkage,
inline=True)
embed.set_footer(text="Please find a medical professional immediately. I'm not medically trained.")
await ctx.send(embed=embed)
A sample of the JSON file's structure (quite basic)
{
"content": "Emergency procedures",
"source": "St John's Ambulance",
"method": "Copyright approval",
"items": [
{
"situation": "COVID-19",
"language": "English",
"filetype": "pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/english/StJohnfactsheet-COVID-19.pdf"
},
{
"situation": "Poster",
"language": "Arabic",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/arabic/DRSABCDA4_poster.pdf"
},
{
"situation": "Allergic",
"language": "Arabic",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/arabic/FS_allergic.pdf"
},
{
"situation": "Choking infant younger than 1 year",
"language": "English",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/english/Factsheets_chokinginfant.pdf"
},
{
"situation": "Concussion",
"language": "English",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/english/Factsheets_concussion.pdf"
},
{
"situation": "CPR on an adult",
"language": "English",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/english/Factsheets_CPRadult.pdf"
},
{
"situation": "CPR on infants",
"language": "English",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/english/Factsheets_CPRinfants.pdf"
},
{
"situation": "Diabetes",
"language": "English",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/english/Factsheets_diabetes.pdf"
},
{
"situation": "Drs ABCD Action Plan",
"language": "English",
"filetype": ".pdf",
"url": "https://stjohn.org.au/first-aid-factsassets/uploads/factsheets/english/Factsheets_DRSABCD.pdf"
}
]
}