Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. from urllib.parse import urlparse
  4. try: # check if BeautifulSoup4 is installed
  5.     from bs4 import BeautifulSoup
  6.     soupAvailable = True
  7. except:
  8.     soupAvailable = False
  9. import aiohttp
  10.  
  11.  
  12. class starcitizen:
  13.     """Star Citizen Cog For Red!"""
  14.    
  15.     def __init__(self, bot):
  16.         self.bot = bot
  17.  
  18.  
  19.     async def gethandle(self, usertext):
  20.         citurl = 'https://robertsspaceindustries.com/citizens/'
  21.         dodebug = True
  22.         url = citurl + usertext #build the web adress
  23.         async with aiohttp.get(url) as response:
  24.                     soupObject = BeautifulSoup(await response.text(), "html.parser")
  25.         if soupObject.find(class_='info').find('p').next_sibling.next_sibling.find('strong'):
  26.             handle = soupObject.find(class_='info').find('p').next_sibling.next_sibling.find('strong').get_text()
  27.             if dodebug == True:
  28.                 await self.bot.say("Found!")
  29.             return handle
  30.  
  31.     @commands.command()
  32.     async def c(self, *text):
  33.         """Search Citizen Profiles!"""
  34.         dodebug = True
  35.         #Your code will go here
  36.              
  37.         try:
  38.             #####GET VARIABLES#####
  39.             usertext = text[0]
  40.             if dodebug == True:
  41.                 await self.bot.say("Gathering what i have found...")
  42.  
  43.             #####PRINT OUTPUT IN DISCORD#####
  44.  
  45.             if dodebug == True:
  46.                 await self.bot.say("checking for handle...")
  47.  
  48.             handle = gethandle(self, usertext)
  49.  
  50.             if handle:
  51.                 if dodebug == True:
  52.                     await self.bot.say("handle is embedding!")
  53.                 embed=discord.Embed(title="Results for " + handle, url="https://robertsspaceindustries.com/citizens/" + handle, color=0xbf0000)
  54.                 embed.add_field(name='Handle', value=handle, inline=True)
  55.  
  56.             if handle:
  57.                 await self.bot.say(embed=embed)
  58.             else:
  59.                 await self.bot.say("Citizen Not Found.")
  60.  
  61.  
  62.         except:
  63.             await self.bot.say("Error. And I have no way to know what it is! Good luck to you!")
  64.  
  65.  
  66. def setup(bot):
  67.     bot.add_cog(starcitizen(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement