Created
August 8, 2017 04:30
-
-
Save Vexs/04da961dd85b25c093903194741de043 to your computer and use it in GitHub Desktop.
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
import discord | |
import aiohttp | |
import re | |
def nexus_json_to_embed(json_data, game_name): | |
title = json_data.get('name', '')[:256] | |
# replace html escapes with ascii values. ex- " turns into quotes | |
description = re.sub(r'&#(\d{2});', lambda x: chr(int(x.group(1))), json_data.get('summary', '')[:1024]) | |
url = json_data.get('mod_page_uri', '') | |
author = json_data.get('author')[:256] | |
endorsements = json_data.get('OLD_endorsements', '') | |
downloads = json_data.get('NEW_downloads', '') | |
embed = discord.Embed(title=author, description=description, url=url, color=0xEDA141) | |
embed.set_author(name=title) | |
if endorsements != '': | |
embed.add_field(name='Endorsements:', value=str(endorsements)) | |
if downloads != '': | |
embed.add_field(name='Downloads:', value=str(downloads)) | |
embed.add_field(name='For {}'.format(game_name), value='') | |
return embed | |
class NexusModsEmbed: | |
"""""" | |
def __init__(self, bot): | |
self.bot = bot | |
self.regex = re.compile(r'www\.nexusmods\.com\/(\w+)\/mods\/(\d+)', re.IGNORECASE) | |
self.url_format = "http://www.nexusmods.com/{}/mods/{}/?" | |
async def on_message(self,message): | |
search = re.search(self.regex, message.content) | |
if search is not None: | |
mod_url = self.url_format.format(search.group(1), search.group(2)) | |
with aiohttp.ClientSession() as session: | |
async with session.get(mod_url, headers={'User-Agent':"Nexus Client v0.63.14"}) as resp: | |
json = await resp.json() | |
if resp.status == 200: | |
await self.bot.send_message(message.channel, embed=nexus_json_to_embed(json, search.group(1))) | |
else: | |
await self.bot.send_message(message.channel, 'Error {}'.format(resp.status)) | |
def setup(bot): | |
bot.add_cog(NexusModsEmbed(bot)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment