import discord import asyncio import urllib.request import requests import json import os.path from discord.ext import commands class ShortenUrl: def __init__(self, bot): self.bot = bot @commands.command(pass_context=True) # #Checks if the user exists in the database and returns a shortened link on success. # #Database format is {"USERID":"APIKEY","117450987848990720":"B3Nm9g8L?s="} # async def ouo(self, ctx): if os.path.isfile('users.json'): with open('users.json') as user_file: users_loaded = json.load(user_file) if users_loaded.get(ctx.message.author.id): storedUrl = ctx.message.content[5:] storedUrl = storedUrl.replace("&", "%26") storedUrl = storedUrl.replace("#", "%23") storedUrl = ("http://ouo.io/api/" + users_loaded.get(ctx.message.author.id) + storedUrl) urlRequest = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) shortenedUrl = urllib.request.urlopen(urlRequest).read().decode('utf-8') await self.bot.say("Shortened: " + shortenedUrl) else: await self.bot.say("nope") else: await self.bot.say("Please create users.json") def setup(bot): bot.add_cog(ShortenUrl(bot))