import discord import asyncio import urllib.request import requests from discord.ext import commands class ShortenUrl: def __init__(self, bot): self.bot = bot @commands.command(pass_context=True) """ Checks if the user matches the role required to process the command otherwise it returns "nope". We use the comman ouo as we are using the ouo webapp to process the shortening of the url. """ async def ouo(self, ctx): with open('data.json') as data_file: data_loaded = json.load(data_file) if data_loaded.get(ctx.message.author.id): url = ("http://ouo.io/api/" + data_loaded.get(ctx.message.author.id) + ctx.message.content) handle = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) html = urllib.request.urlopen(handle).read().decode('utf-8') await self.bot.say("Shortened: " + html) else: await self.bot.say("nope") def setup(bot): bot.add_cog(ShortenUrl(bot))