diff --git a/src/duckduckgo.py b/src/duckduckgo.py new file mode 100644 index 0000000..890b040 --- /dev/null +++ b/src/duckduckgo.py @@ -0,0 +1,44 @@ +import aiohttp +import discord +import asyncio +import logging +import discord.ext.commands as commands +import html.parser + +class Parser(html.parser.HTMLParser): + def __init__(self): + self.links = [] + super().__init__() + + def handle_starttag(self, tag, attrs): + attrs = dict(attrs) + if tag == "a" and attrs.get("class") == "result__a" and "https://duckduckgo.com/y.js?ad_provider" not in attrs["href"]: + self.links.append(attrs["href"]) + +class DuckDuckGo(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.session = aiohttp.ClientSession() + + @commands.command() + async def search(self, ctx, *, query): + async with ctx.typing(): + async with self.session.post("https://html.duckduckgo.com/html/", data={ "q": query, "d": "" }) as resp: + if resp.history: + await ctx.send(resp.url, reference=ctx.message) + else: + p = Parser() + txt = await resp.text() + p.feed(txt) + p.close() + try: + return await ctx.send(p.links[0], reference=ctx.message) + except IndexError: + return await ctx.send("No results.", reference=ctx.message) + + def cog_unload(self): + asyncio.create_task(self.session.close()) + +def setup(bot): + cog = DuckDuckGo(bot) + bot.add_cog(cog) \ No newline at end of file diff --git a/src/main.py b/src/main.py index fa9a60e..ffc6348 100644 --- a/src/main.py +++ b/src/main.py @@ -25,11 +25,11 @@ config = util.config logging.basicConfig(level=logging.INFO, format="%(levelname)s %(asctime)s %(message)s", datefmt="%H:%M:%S %d/%m/%Y") -#intents = discord.Intents.default() -#intents.members = True +intents = discord.Intents.default() +intents.members = True bot = commands.Bot(command_prefix=config["prefix"], description="AutoBotRobot, the most useless bot in the known universe." + util.config.get("description_suffix", ""), - case_insensitive=True, allowed_mentions=discord.AllowedMentions(everyone=False, users=True, roles=True)) + case_insensitive=True, allowed_mentions=discord.AllowedMentions(everyone=False, users=True, roles=True), intents=intents) bot._skip_check = lambda x, y: False messages = prometheus_client.Counter("abr_messages", "Messages seen/handled by bot") diff --git a/src/util.py b/src/util.py index 82829d2..5104fb4 100644 --- a/src/util.py +++ b/src/util.py @@ -265,7 +265,8 @@ extensions = ( "commands", "userdata", "irc_link", - "discord_link" + "discord_link", + "duckduckgo" ) # https://github.com/SawdustSoftware/simpleflake/blob/master/simpleflake/simpleflake.py