-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.py
54 lines (47 loc) · 1.91 KB
/
wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import discord
from discord.ext import commands
#A pull request is pending for the BeautifulSoup warning: https://github.com/goldsmith/Wikipedia/pull/112
#If you receiving this error and don't want to wait for the fix to be merged in to the wikipedia module, you could fix it manually.
#
#\Python35\Lib\site-packages\wikipedia\wikipedia.py@line 389
#Change:
# lis = BeautifulSoup(html).find_all('li')
#To:
# lis = BeautifulSoup(html, "html.parser").find_all('li')
class Wikipedia:
"""Wikipedia search for the Red-DiscordBot"""
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True, no_pm=True)
async def wikipedia(self, ctx, *text):
"""Wikipedia search."""
if text == ():
await self.bot.say("**Usage:** =wikipedia <search>")
return
else:
await self.bot.say("**Searching...**")
s = "_";
search = ""
search = s.join(text)
user = ctx.message.author
wikiLang = 'en'# Define the Wikipedia language / Most of these are supported » https://nl.wikipedia.org/wiki/ISO_3166-1
ws = None
wikipedia.set_lang(wikiLang)# Set the Wikipedia language.
try:
ws = wikipedia.page(search)
wikiUrl = (ws.url.encode('ascii', 'xmlcharrefreplace'))
await self.bot.say(wikiUrl.decode("utf8"))
except:
await self.bot.say( 'Sorry {}, but i could not find anything on "{}"'.format(user.mention, text))
class ModuleNotFound(Exception):
def __init__(self, m):
self.message = m
def __str__(self):
return self.message
def setup(bot):
global wikipedia
try:
import wikipedia
except:
raise ModuleNotFound("Wikipedia is not installed. Do 'pip3 install wikipedia --upgrade' to use this cog.")
bot.add_cog(Wikipedia(bot))