def wikipedia(cmd, message, args):
if args:
try:
summary_task = functools.partial(wiki.page, ' '.join(args).lower())
with ThreadPoolExecutor() as threads:
page = await cmd.bot.loop.run_in_executor(threads, summary_task)
response = discord.Embed(color=0xF9F9F9)
response.set_author(
name=f'Wikipedia: {page.title}',
url=page.url,
icon_url='https://upload.wikimedia.org/wikipedia/commons/6/6e/Wikipedia_logo_silver.png'
)
response.description = f'{page.summary[:800]}...'
except wiki.PageError:
response = discord.Embed(color=0x696969, title='?? No results.')
except wiki.DisambiguationError:
response = discord.Embed(color=0xBE1931, title='? Search too broad, please be more specific.')
else:
response = discord.Embed(color=0xBE1931, title='? Nothing inputted.')
await message.channel.send(None, embed=response)
python类PageError()的实例源码
def wikipedia(cmd, message, args):
if args:
q = ' '.join(args).lower()
try:
result = wp.summary(q)
if len(result) >= 650:
result = result[:650] + '...'
response = discord.Embed(color=0xF9F9F9)
response.add_field(name=f'?? Wikipedia: `{q.upper()}`', value=result)
except wp.PageError:
response = discord.Embed(color=0x696969, title='?? No results.')
except wp.DisambiguationError:
response = discord.Embed(color=0xBE1931, title='? Search too broad, please be more specific.')
else:
response = discord.Embed(color=0xBE1931, title='? Nothing inputted.')
await message.channel.send(None, embed=response)
def wikipedia_parser(ctx, message):
"""
Returns a wikipedia definition
:param ctx:
:param message:
:return:
"""
try:
querry = message.content[6:]
search = wikipedia.summary(str(querry), sentences=4)
await ctx.send_message(message.channel, "```{}```".format(search))
except wikipedia.DisambiguationError as e:
length = len(e.options)
if length == 1:
await ctx.send_message(message.channel, "Did you mean? `{}`".format(e.options[0]))
elif length == 2:
await ctx.send_message(message.channel, "Did you mean? `{}` or `{}`"
.format(e.options[0], e.options[1]))
else:
await ctx.send_message(message.channel,
"Disambiguation in you query. It can mean `{}`, `{}` and {} more."
.format(e.options[0], e.options[1], str(length)))
except wikipedia.PageError:
await ctx.send_message(message.channel, "No pages matched your querry :cry:")
except wikipedia.HTTPTimeoutError:
await ctx.send_message(message.channel, "Hey there, slow down you searches for a bit!")
except wikipedia.RedirectError:
await ctx.send_message(message.channel,
"Error: page title unexpectedly resolves to a redirect. "
"Please re-check your query.")
except wikipedia.WikipediaException:
await ctx.send_message(message.channel, "Error: The search parameter must be set.")
def wikipedia_parser(ctx, message):
"""
Returns a wikipedia definition
:param ctx:
:param message:
:return:
"""
try:
querry = message.content[6:]
search = wikipedia.summary(str(querry), sentences=4)
await ctx.send_message(message.channel, "```{}```".format(search))
except wikipedia.DisambiguationError as e:
length = len(e.options)
if length == 1:
await ctx.send_message(message.channel, "Did you mean? `{}`".format(e.options[0]))
elif length == 2:
await ctx.send_message(message.channel, "Did you mean? `{}` or `{}`"
.format(e.options[0], e.options[1]))
else:
await ctx.send_message(message.channel,
"Disambiguation in you query. It can mean `{}`, `{}` and {} more."
.format(e.options[0], e.options[1], str(length)))
except wikipedia.PageError:
await ctx.send_message(message.channel, "No pages matched your querry :cry:")
except wikipedia.HTTPTimeoutError:
await ctx.send_message(message.channel, "Hey there, slow down you searches for a bit!")
except wikipedia.RedirectError:
await ctx.send_message(message.channel,
"Error: page title unexpectedly resolves to a redirect. "
"Please re-check your query.")
except wikipedia.WikipediaException:
await ctx.send_message(message.channel, "Error: The search parameter must be set.")
def wikipedia(self, context, lang: str = None, query: str = None):
''' Get a page from wikipedia and reply with an embed '''
query = self.bot.get_text(context)
if lang is not None:
if lang.startswith('(') and lang.endswith(')'):
query = query[len(lang) + 1:]
lang = lang[1:-1]
else:
lang = None
if query in [None, '', ' ']:
await self.bot.doubt(context)
return
try:
import wikipedia
if lang is not None and lang in wikipedia.languages().keys():
wikipedia.set_lang(lang)
else:
wikipedia.set_lang('en')
page = wikipedia.page(query)
summary = page.summary
if len(summary) > 1222: # totally arbitrary chosen number
summary = summary[:1220] + '...'
embed = discord.Embed(title=page.title, description=summary, url=page.url)
embed.set_footer(text=page.url)
if self.bot.config['bleeding']:
if len(page.images) > 0:
embed.set_image(url=page.images[0])
await self.bot.say(embed=embed)
await self.bot.replied(context)
except wikipedia.PageError as e:
await self.bot.reply('{}\nMake sure you search for page titles in the language that you have set.'.format(e))
await self.bot.doubt(context)
except KeyError:
pass
except wikipedia.DisambiguationError as e:
msg = '```\n{}\n```'.format(e)
await self.bot.doubt(context)
await self.bot.say(msg)