Untitled

By Walloping Gorilla, 5 Years ago, written in Python, viewed 104 times.
URL http://pb.stoleyour.com/view/0474281e Embed
Download Paste or View RawExpand paste to full width of browser
  1. import discord
  2. import asyncio
  3. import urllib.request
  4. import requests
  5. from discord.ext import commands
  6.  
  7. class ShortenUrl:
  8.  
  9.     def __init__(self, bot):
  10.         self.bot = bot
  11.  
  12.     @commands.command(pass_context=True)
  13.     """
  14.    Checks if the user matches the role required to process the command
  15.    otherwise it returns "nope".
  16.    
  17.    We use the comman ouo as we are using the ouo webapp to process the
  18.    shortening of the url.
  19.    """
  20.     async def ouo(self, ctx):
  21.         with open('data.json') as data_file:
  22.             data_loaded = json.load(data_file)
  23.        
  24.         if data_loaded.get(ctx.message.author.id):        
  25.             url = ("http://ouo.io/api/" + data_loaded.get(ctx.message.author.id) + ctx.message.content)
  26.             handle = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
  27.             html = urllib.request.urlopen(handle).read().decode('utf-8')
  28.             await self.bot.say("Shortened: " + html)
  29.         else:
  30.             await self.bot.say("nope")
  31.  
  32. def setup(bot):
  33.     bot.add_cog(ShortenUrl(bot))

Reply to "Untitled"

Here you can reply to the paste above