From 59f69c7727b389cc090ead70e8590b9fa29d1532 Mon Sep 17 00:00:00 2001 From: Nathaniel F Date: Wed, 2 Sep 2020 18:35:27 -0400 Subject: [PATCH] [General] URL encode user input for [p]urban and [p]lmgtfy (#4024) * URL encode user input in the general cog Adds URL encoding to `[p]lmgtfy` via `urllib2.parse.quote_plus()` and to `[p]urban` via `aiohttp.ClientSession.get`'s `params` argument * Black reformatting --- redbot/cogs/general/general.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/redbot/cogs/general/general.py b/redbot/cogs/general/general.py index 39bf56237..f7c443e9a 100644 --- a/redbot/cogs/general/general.py +++ b/redbot/cogs/general/general.py @@ -3,6 +3,7 @@ import time from enum import Enum from random import randint, choice from typing import Final +import urllib.parse import aiohttp import discord from redbot.core import commands @@ -214,9 +215,7 @@ class General(commands.Cog): @commands.command() async def lmgtfy(self, ctx, *, search_terms: str): """Create a lmgtfy link.""" - search_terms = escape( - search_terms.replace("+", "%2B").replace(" ", "+"), mass_mentions=True - ) + search_terms = escape(urllib.parse.quote_plus(search_terms), mass_mentions=True) await ctx.send("https://lmgtfy.com/?q={}".format(search_terms)) @commands.command(hidden=True) @@ -482,12 +481,14 @@ class General(commands.Cog): """ try: - url = "https://api.urbandictionary.com/v0/define?term=" + str(word).lower() + url = "https://api.urbandictionary.com/v0/define" + + params = {"term": str(word).lower()} headers = {"content-type": "application/json"} async with aiohttp.ClientSession() as session: - async with session.get(url, headers=headers) as response: + async with session.get(url, headers=headers, params=params) as response: data = await response.json() except aiohttp.ClientError: