2017-06-11 20:04:03 +02:00
from discord . ext import commands
import aiohttp
import asyncio
import discord
import urllib . request , json
import wikipedia , bs4
wikipedia . set_lang ( " fr " )
class Search :
""" Commandes de WWW. """
def __init__ ( self , bot ) :
self . bot = bot
@commands.group ( name = " search " , no_pm = True , pass_context = True )
async def _search ( self , ctx ) :
""" Rechercher sur le world wide web """
if ctx . invoked_subcommand is None :
text = open ( ' texts/search.md ' ) . read ( )
em = discord . Embed ( title = ' Commandes de search TuxBot ' , description = text , colour = 0x89C4F9 )
2017-09-30 18:09:02 +02:00
await ctx . send ( embed = em )
2017-06-11 20:04:03 +02:00
@_search.command ( pass_context = True , name = " docubuntu " )
async def search_docubuntu ( self , ctx , args ) :
2017-09-30 18:09:02 +02:00
attends = await ctx . send ( " _Je te cherche ça {} !_ " . format ( ctx . message . author . mention ) )
2017-06-11 20:04:03 +02:00
html = urllib . request . urlopen ( " https://doc.ubuntu-fr.org/ " + args ) . read ( )
if " avez suivi un lien " in str ( html ) :
2017-09-30 18:09:02 +02:00
await attends . edit ( content = " :sob: Nooooon ! Cette page n ' existe pas, mais tu peux toujours la créer : https://doc.ubuntu-fr.org/ " + args )
2017-06-11 20:04:03 +02:00
else :
2017-09-30 18:09:02 +02:00
await attends . delete ( )
2017-06-11 20:04:03 +02:00
embed = discord . Embed ( description = " Voila j ' ai trouvé ! Voici la page ramenant à votre recherche, toujours aussi bien rédigée :wink: : https://doc.ubuntu-fr.org/ " + args , url = ' http://doc.ubuntu-fr.org/ ' )
embed . set_author ( name = " DocUbuntu-Fr " , url = ' http://doc.ubuntu-fr.org/ ' , icon_url = ' http://outout.tech/tuxbot/ubuntu.png ' )
embed . set_thumbnail ( url = ' http://outout.tech/tuxbot/ubuntu.png ' )
embed . set_footer ( text = " Merci à ceux qui ont pris le temps d ' écrire cette documentation " )
2017-09-30 18:09:02 +02:00
await ctx . send ( embed = embed )
2017-06-11 20:04:03 +02:00
@_search.command ( pass_context = True , name = " aur " )
async def search_aur ( self , ctx , args ) :
2017-09-30 18:09:02 +02:00
attends = await ctx . send ( " _Je te cherche ça {} !_ " . format ( ctx . message . author . mention ) )
2017-06-11 20:04:03 +02:00
erreur = 0
try :
html = urllib . request . urlopen ( " https://aur.archlinux.org/packages/ " + args ) . read ( )
except :
erreur = 1
if erreur == 1 :
2017-09-30 18:09:02 +02:00
await attends . delete ( )
2017-06-11 20:04:03 +02:00
embed = discord . Embed ( description = " :sob: Je n ' ai pas trouvé le packet mais j ' ai lancé une petite recherche, tu y trouveras peut être ton bonheur ? https://aur.archlinux.org/packages/?K= " + args , url = ' https://aur.archlinux.org/ ' )
embed . set_author ( name = " Aur.archlinux " , url = ' https://aur.archlinux.org/ ' , icon_url = ' http://outout.tech/tuxbot/arch.png ' )
embed . set_thumbnail ( url = ' http://outout.tech/tuxbot/arch.png ' )
embed . set_footer ( text = " Pff même pas trouvé ! " )
2017-09-30 18:09:02 +02:00
await ctx . send ( embed = embed )
2017-06-11 20:04:03 +02:00
else :
2017-09-30 18:09:02 +02:00
await attends . delete ( )
embed = discord . Embed ( description = " Et voila, j ' ai trouvé la page sur le packet : https://aur.archlinux.org/packages/ {0} ! \n Ca te dit un petit ``pacaur -S {0} `` ? " . format ( args ) , url = ' https://aur.archlinux.org/ ' )
embed . set_author ( name = " Aur.archlinux " , url = ' https://aur.archlinux.org/ ' , icon_url = ' http://outout.tech/tuxbot/arch.png ' )
embed . set_thumbnail ( url = ' http://outout.tech/tuxbot/arch.png ' )
embed . set_footer ( text = " C ' est vrai que pacman et pacaur sont mieux qu ' APT ^^ " )
await ctx . send ( embed = embed )
2017-06-11 20:04:03 +02:00
@_search.command ( pass_context = True , name = " wikipedia " )
2017-09-30 18:09:02 +02:00
async def search_wikipedia ( self , ctx : commands . Context , args ) :
2017-06-11 20:04:03 +02:00
""" Fait une recherche sur wikipd """
2017-09-30 18:09:02 +02:00
wait = await ctx . send ( " _Je cherche..._ " )
results = wikipedia . search ( args )
nbmr = 0
mmssgg = " "
for value in results :
nbmr = nbmr + 1
mmssgg = mmssgg + " ** {} **: {} \n " . format ( str ( nbmr ) , value )
em = discord . Embed ( title = ' Résultats de : ' + args , description = mmssgg , colour = 0x4ECDC4 )
em . set_thumbnail ( url = " https://upload.wikimedia.org/wikipedia/commons/2/26/Paullusmagnus-logo_ %28la rge % 29.png " )
await wait . delete ( )
sending = [ " 1⃣ " , " 2⃣ " , " 3⃣ " , " 4⃣ " , " 5⃣ " , " 6⃣ " , " 7⃣ " , " 8⃣ " , " 9⃣ " , " 🔟 " ]
def check ( reaction , user ) :
return user == ctx . author and reaction . emoji in sending and reaction . message . id == msg . id
async def waiter ( future : asyncio . Future ) :
reaction , user = await self . bot . wait_for ( ' reaction_add ' , check = check )
future . set_result ( reaction . emoji )
emoji = asyncio . Future ( )
self . bot . loop . create_task ( waiter ( emoji ) )
2017-06-11 20:04:03 +02:00
2017-09-30 18:09:02 +02:00
msg = await ctx . send ( embed = em )
for e in sending :
await msg . add_reaction ( e )
if emoji . done ( ) :
break
while not emoji . done ( ) :
await asyncio . sleep ( 0.1 )
sPage = int ( sending . index ( emoji . result ( ) ) )
args_ = results [ sPage ]
try :
await msg . delete ( )
await ctx . trigger_typing ( )
wait = await ctx . send ( ctx . message . author . mention + " ah ok sympa cette recherche, je l ' effectue de suite ! " )
wp = wikipedia . page ( args_ )
wp_contenu = wp . summary [ : 200 ] + " ... "
em = discord . Embed ( title = ' Wikipedia : ' + wp . title , description = " {} \n _Lien_ : {} " . format ( wp_contenu , wp . url ) , colour = 0x9B59B6 )
em . set_author ( name = " Wikipedia " , url = ' http://wikipedia.org ' , icon_url = ' https://upload.wikimedia.org/wikipedia/commons/2/26/Paullusmagnus-logo_ %28la rge % 29.png ' )
2017-06-11 20:04:03 +02:00
em . set_thumbnail ( url = " https://upload.wikimedia.org/wikipedia/commons/2/26/Paullusmagnus-logo_ %28la rge % 29.png " )
2017-09-30 18:09:02 +02:00
em . set_footer ( text = " Merci à eux de nous fournir une encyclopédie libre ! " )
await wait . delete ( )
await ctx . send ( embed = em )
except wikipedia . exceptions . PageError : #TODO : A virer dans l'event on_error
await ctx . send ( " :open_mouth: Une **erreur interne** est survenue, si cela ce reproduit contactez votre administrateur ou faites une Issue sur ``github`` ! " )
2017-06-11 20:04:03 +02:00
def setup ( bot ) :
bot . add_cog ( Search ( bot ) )