Created
November 6, 2018 03:23
-
-
Save Scoder12/5a539a7290568162321652107c2c363a to your computer and use it in GitHub Desktop.
Discord.py Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import discord | |
import requests | |
import datetime | |
import traceback | |
from urllib.parse import urlencode | |
from urllib.parse import quote_plus | |
from discord.ext import commands | |
import os | |
os.chdir("..") | |
import configvars | |
async def error(msg, ctx): | |
logging.error("!ERROR!: " + str(msg)) | |
await ctx.send(msg) | |
return | |
def gen_shorten_embed(url, shortened, timestamp): | |
description = "Link shortened! \n Original URL: %s\nShortened\ | |
URL: %s\n\nShortened URL is %s characters shorter. \ | |
" % (url, shortened, (int(len(url)) - int(len(shortened)))) | |
return discord.Embed(title=shortened, | |
colour=discord.Colour(0xd0021b), | |
url=shortened, | |
description="description", timestamp=timestamp) | |
class ShortenCog: | |
def __init__(self, bot): | |
self.bot = bot | |
@commands.command(name='cuturl') | |
async def cuturl(ctx, url): | |
"""Generates a CutUrls shortlink. | |
Aruments: | |
The URL to be shortened. | |
DISCLAIMER: The use of this shortener benefits the developer. | |
""" | |
async with ctx.message.channel.typing: | |
if not url.startswith("http://") and not url.startswith("https://"): | |
await ctx.send("Links should start with http:// or https://") | |
return | |
try: | |
res = requests.get("https://cut-urls.com/api?api="+configvars.CUTURLS_TOKEN+"&url="+quote_plus(url), timeout=10) | |
except requests.exceptions.Timeout: | |
await error("Request to TinyUrl Expired. ") | |
timestamp = datetime.datetime.now() | |
try: | |
r = res.json() | |
except Exception as e: | |
await error("Error in jsonifying response: ```%s```\nText Dump: ```%s```" % (traceback.format_exc(), res.text), ctx) | |
return | |
if res.status_code != 200: | |
await error("Error: could not connect to CutUrls. Status code was " + str(r.status_code), ctx) | |
return | |
elif r['status'] == 'success': | |
shortened = r['shortenedUrl'] | |
embed = gen_shorten_embed(url, shortened, timestamp) | |
await ctx.send(embed=embed) | |
return | |
elif r['status'] == 'error': | |
await error("An error occurred. The message is: '" + r['message'] + "'") | |
return | |
else: | |
await error("Error: unknown status. \n" + str(r)) | |
return | |
@commands.command(name="tinyurl") | |
async def tinyurl(ctx, url): | |
"""Generates a tinyurl.com shortlink. | |
Arguments: | |
The URL to be shortened. | |
Fun fact: TinyUrl invented the url shortener in 2002! | |
""" | |
if not url.startswith("http://") and not url.startswith("https://"): | |
await ctx.send("Links should start with http:// or https://") | |
return | |
try: | |
r = requests.get("https://tinyurl.com/api-create.php?"+urlencode(url=url)) | |
timestamp = datetime.datetime.now() | |
except requests.exceptions.Timeout: | |
await error("Request to TinyUrl Expired. ") | |
if r.status_code != 200: | |
await error("Please contact error: \nError in tinyurl request: \ | |
Status code %s Text dump: \n```%s```" % (r.staus_code, r.text), ctx) | |
return | |
embed = gen_shorten_embed(url, r.text, timestamp) | |
await ctx.send(embed=embed) | |
@commands.command(name="bitly") | |
async def bitly(ctx, url): | |
if not url.startswith("http://") and not url.startswith("https://"): | |
await ctx.send("Links should start with http:// or https://") | |
return | |
r = requests.get("https://api-ssl.bitly.com/v3/shorten?access_token="\ | |
+configvars.BITLY_TOKEN+"&longurl="+url, timeout=10) | |
timestamp = datetime.datetime.now() | |
if r.status_code != 200: | |
await error("Error in bitly request\ | |
Status code %s Text dump: \n%s" % (r.staus_code, r.text)) | |
return | |
try: | |
json = r.json() | |
except Exception as e: | |
await error("Error in jsonifying response: ```%s```\nText Dump: ```%s```" % (traceback.format_exc(), r.text), ctx) | |
return | |
if json["status_txt"] != "OK": | |
await error("Bitly reported error status: %s" % json["status_txt"]) | |
return | |
shortened = str(json["data"]["url"]) | |
embed = gen_shorten_embed(url, shortened, timestamp) | |
await ctx.send(embed=embed) | |
return | |
def setup(bot): | |
bot.add_cog(ShortenCog) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment