async def wolf(self, ctx): """ Query the Simple Wolfram|Alpha API """ try: # removes the "!wolf" portion of the message message = parse.strip_command(ctx.message.content) # Reject empty messages if message == "": await ctx.send("You must pass in a question to get a response") return # Query the API and post its response async with aiohttp.ClientSession(headers=HEADERS) as session: async with session.get( "http://api.wolframalpha.com/v1/result?appid=" + credentials.tokens["WOLFRAMALPHA_APPID"] + "&i=" + quote(message)) as resp: if resp.status is 501: await ctx.send( "WolframAlpha could not understand the question '{}' because {}" .format(message, resp.reason)) return data = await resp.content.read() await ctx.send(data.decode("utf-8")) except Exception as e: await utils.report(str(e), source="wolf command", ctx=ctx)
async def gritty(self, ctx): try: embed = Embed() embed.colour = EMBED_COLORS["gritty"] arg = parse.strip_command(ctx.message.content) # Check if user passed a specific gritty index num = None if arg != "": arg = arg[1:] if arg[ 0] == "#" else arg # Trim off leading '#' if provided try: num = int(arg) except ValueError: await ctx.send(f"`{arg}` is not a number") return url, num = self.get_gritty_url(num) embed.set_image(url=url) embed.description = f"**#{num}**" await ctx.send(embed=embed) except Exception as e: await utils.report(str(e), source="Gritty command", ctx=ctx)
async def picture(self, ctx): try: unsplash_icon = 'https://dwcamp.net/logos/unsplash' headers = { "User-Agent": "suitsBot Discord Bot - https://github.com/DWCamp", "Authorization": "Client-ID " + tokens["UNSPLASH_CLIENT_ID"], "Accept-Version": "v1" } # Parse command content = parse.strip_command(ctx.message.content) # Query defaults to a picture of a snake if content == "": query = "snake" else: query = quote(content) # Get the image URL json = await utils.get_json_with_get( 'https://api.unsplash.com/photos/random?query=' + query, headers=headers) author_url = json[0]['user']['links'][ 'html'] + "?utm_source=SuitsBot&utm_medium=referral" pic_embed = Embed().set_image(url=json[0]['urls']['full']) pic_embed.description = "Photo credit: " + author_url pic_embed.colour = EMBED_COLORS['picture'] pic_embed.set_footer( icon_url=unsplash_icon, text= "https://unsplash.com/?utm_source=SuitsBot&utm_medium=referral" ) await ctx.send(embed=pic_embed) except Exception as e: await utils.report(str(e), source="picture command", ctx=ctx)
async def ud(self, ctx): # Removes the brackets around words, which UD puts around words in definitions # and examples that have their own definitions def strip_brackets(text): text = text.replace("[", " ").replace("]", "") return text """ Query the UrbanDictionary API """ try: # removes the invocation portion of the message message = parse.strip_command(ctx.message.content) max_definitions = 4 # Reject empty messages if message == "": await ctx.send("You must pass in a term to get a definition") return # Query the API and post its response url = "http://api.urbandictionary.com/v0/define?term=" + quote( message) (ud_json, response) = await utils.get_json_with_get(url) if response is not 200: await ctx.send( "There was an error processing your request. I apologize for the inconvenience." ) return if len(ud_json["list"]) > 0: ud_embed = Embed() ud_embed.set_footer( text="UrbanDictionary", icon_url="https://dwcamp.net/logos/urbanDictionary.jpg") counter = 0 first_result = "" while counter < len( ud_json["list"]) and counter <= max_definitions: definition = ud_json["list"][counter] if counter == 0: first_result = definition["word"] ud_embed.title = utils.trim_to_len(first_result, 256).capitalize() if definition["word"] == first_result: def_text = definition["definition"].replace("*", "\\*") example_text = "**Example: " + definition[ "example"].replace("*", "\\*") + "**" ud_embed.add_field( name=str(counter + 1), value=utils.trim_to_len( strip_brackets(def_text + "\n\n" + example_text), 1024), inline=False) counter += 1 ud_embed.colour = EMBED_COLORS['ud'] # Make the embed white await ctx.send(embed=ud_embed) else: await ctx.send( "I can't find any UrbanDictionary results for `" + message + "`") except Exception as e: await utils.report(str(e), source="ud command", ctx=ctx)
async def handle_rss_feed(self, feed, ctx): """ The universal rss feed handler. Takes in an RSSFeed object then does all the magic :param feed: The RSSFeed :param ctx: The ctx object of the inciting message """ try: message = parse.strip_command(ctx.message.content) # Update feed feed.refresh_if_stale() # Show most recent episode if message == "": episode = feed.items[0] await ctx.send(embed=feed.get_embed(episode)) return # check for subcommand and parse it out subcommand = "" parameter = message if message[0] == "-": whitespace = utils.first_whitespace(message) if whitespace == -1: subcommand = message[1:] parameter = "" else: subcommand = message[1:whitespace] parameter = message[whitespace:].strip() # Teach the person how to use this thing if subcommand == "help": command_name = ctx.invoked_with title = f"!{command_name} - User Guide" description = f"Searches the RSS feed for {feed.title}" helpdict = { f"!{command_name}": "Post the most recent item in the feed", f"!{command_name} <word>": "Post the most recent item whose title contains the whole word <word>", f"!{command_name} -help": "Posts this message", f"!{command_name} -reg <regex>": "Perform a regular expression search for the most " "recent item title matching <regex>", f"!{command_name} -refresh": "Force the bot to refresh its cache" } thumbnail = feed.image if feed.image is not None else COMMAND_THUMBNAILS[ "rssfeed"] await ctx.send( embed=utils.embed_from_dict(helpdict, title=title, description=description, thumbnail_url=thumbnail)) return # Dump all the info about the feed if subcommand == "details": await ctx.send(feed.to_string()) return # Test an embed for a given feed if subcommand == "embed": await ctx.send(embed=feed.get_embed(feed.items[0])) return # Test an embed for a given feed if subcommand == "image": embed = Embed() embed.set_image(url=feed.image) await ctx.send(embed=embed) return # If some nerd like Kris or Pat wants to do regex search if subcommand == "reg": episode = feed.search(parameter, regex=True) if episode: await ctx.send(embed=feed.get_embed(episode)) return await ctx.send( f"I couldn't find any results for the regex string `{parameter}`" ) # Force a refresh on the feed if subcommand == "refresh": feed.refresh() await ctx.send( f"Alright, I have refreshed the feed `{feed.feed_id}`") return # Returns search results that the user can select if subcommand == "search": await ctx.send("This has not been implemented yet :sad:") return # If there was a subcommand but it was unrecognized if subcommand != "": await ctx.send( f"I'm sorry, I don't understand the subcommand `{subcommand}`. " + f"Please consult `-help` for more information") return # Search for the term episode = feed.search(parameter) if episode: await ctx.send(embed=feed.get_embed(episode)) return await ctx.send( f"I couldn't find any results in the {feed.title} feed " f"for the term `{parameter}` :worried:") except Exception as e: await utils.report( str(e), source=f"handle_rss_feed() for '{feed.feed_id}'", ctx=ctx)