mirror of
https://github.com/polyjitter/searchbot-discord.git
synced 2024-08-14 22:46:55 +00:00
Minor performance and options changes
This commit is contained in:
parent
ae7e0e20b5
commit
da236609d4
4 changed files with 236 additions and 58 deletions
|
@ -12,6 +12,7 @@
|
||||||
"PREFIXLESS_DMS": true,
|
"PREFIXLESS_DMS": true,
|
||||||
"MENTION_ASSIST": true,
|
"MENTION_ASSIST": true,
|
||||||
"CUSTOM_HELP": true,
|
"CUSTOM_HELP": true,
|
||||||
|
"REMOVE_MENTIONS": true,
|
||||||
|
|
||||||
"PERMS": 378944,
|
"PERMS": 378944,
|
||||||
"BLOCKED": [],
|
"BLOCKED": [],
|
||||||
|
|
|
@ -64,9 +64,7 @@ class Core(commands.Cog):
|
||||||
"for debug and maintenance purposes. "
|
"for debug and maintenance purposes. "
|
||||||
"These logs are shared with nobody "
|
"These logs are shared with nobody "
|
||||||
"other than those who help develop this bot. "
|
"other than those who help develop this bot. "
|
||||||
"If you do not agree to this, please remove this bot. "
|
"If you do not agree to this, please remove this bot._\n\n"
|
||||||
"You may contact the devs at any "
|
|
||||||
"option listed in `info` for deletion requests._\n\n"
|
|
||||||
"_You may recall this message at any time with `tutorial`._"
|
"_You may recall this message at any time with `tutorial`._"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -121,8 +119,6 @@ class Core(commands.Cog):
|
||||||
|
|
||||||
# Footer
|
# Footer
|
||||||
msg += (
|
msg += (
|
||||||
"_When in doubt, you may email the creator of the codebase at "
|
|
||||||
"`polyjitter@fastmail.com`._\n"
|
|
||||||
f"_See {prefix}`help` for help, `invite` to add the bot, "
|
f"_See {prefix}`help` for help, `invite` to add the bot, "
|
||||||
"and `stats` for statistics._"
|
"and `stats` for statistics._"
|
||||||
)
|
)
|
||||||
|
@ -323,11 +319,13 @@ Guild count: {len(self.bot.guilds)}
|
||||||
for c in guild.channels:
|
for c in guild.channels:
|
||||||
if type(c) is discord.TextChannel:
|
if type(c) is discord.TextChannel:
|
||||||
text_channels.append(c)
|
text_channels.append(c)
|
||||||
|
break
|
||||||
|
|
||||||
# Sets channel to general if it exists
|
# Sets channel to general if it exists
|
||||||
for c in guild.channels:
|
for c in guild.channels:
|
||||||
if c.name == 'general':
|
if c.name == 'general':
|
||||||
channel = c
|
channel = c
|
||||||
|
break
|
||||||
|
|
||||||
# XXX This looks like garbage
|
# XXX This looks like garbage
|
||||||
# Else posts in first open channel
|
# Else posts in first open channel
|
||||||
|
@ -335,6 +333,7 @@ Guild count: {len(self.bot.guilds)}
|
||||||
for c in guild.channels:
|
for c in guild.channels:
|
||||||
if c.permissions_for(guild.me).send_messages:
|
if c.permissions_for(guild.me).send_messages:
|
||||||
channel = c
|
channel = c
|
||||||
|
break
|
||||||
|
|
||||||
# Send tutorial message
|
# Send tutorial message
|
||||||
if channel:
|
if channel:
|
||||||
|
@ -347,7 +346,7 @@ Guild count: {len(self.bot.guilds)}
|
||||||
"You wish to use me in!_"
|
"You wish to use me in!_"
|
||||||
)
|
)
|
||||||
|
|
||||||
await owner.send(guild_msg)
|
await guild.owner.send(guild_msg)
|
||||||
return # Ends here if there are no good channels to send to
|
return # Ends here if there are no good channels to send to
|
||||||
|
|
||||||
# Owner Disclosure
|
# Owner Disclosure
|
||||||
|
@ -361,14 +360,12 @@ Guild count: {len(self.bot.guilds)}
|
||||||
f"I have sent a tutorial message to `{channel.name}` "
|
f"I have sent a tutorial message to `{channel.name}` "
|
||||||
"describing how I may be used.\n\n"
|
"describing how I may be used.\n\n"
|
||||||
"If you do not wish to have me there, "
|
"If you do not wish to have me there, "
|
||||||
"simply kick me from the server. You may contact the devs "
|
"simply kick me from the server.\n\n"
|
||||||
"at any "
|
|
||||||
"option listed in `info` for deletion requests.\n\n"
|
|
||||||
"_Thanks for your time!_"
|
"_Thanks for your time!_"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Send owner disclosure
|
# Send owner disclosure
|
||||||
await owner.send(owner_msg)
|
await guild.owner.send(owner_msg)
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
|
|
176
extensions/models/search_source.py
Normal file
176
extensions/models/search_source.py
Normal file
|
@ -0,0 +1,176 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# search source
|
||||||
|
# Provides paginator sources for the search cog.
|
||||||
|
|
||||||
|
"""Search Source File"""
|
||||||
|
|
||||||
|
from typing import Callable, List, Tuple, Optional, Any
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from discord.ext import menus
|
||||||
|
import html2text
|
||||||
|
import re
|
||||||
|
|
||||||
|
FetcherArgs = Tuple[Any]
|
||||||
|
Fetcher = Callable[..., List]
|
||||||
|
|
||||||
|
# Markdown converter
|
||||||
|
tomd = html2text.HTML2Text()
|
||||||
|
tomd.ignore_links = True
|
||||||
|
tomd.ignore_images = True
|
||||||
|
tomd.ignore_tables = True
|
||||||
|
tomd.ignore_emphasis = True
|
||||||
|
tomd.body_width = 0
|
||||||
|
|
||||||
|
|
||||||
|
# TODO Change around value names, make it general
|
||||||
|
class Result:
|
||||||
|
"""A class that holds the general data for a search result.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
title (str): Title of the content.
|
||||||
|
|
||||||
|
url (str): The direct link to the content.
|
||||||
|
|
||||||
|
desc (str): The content's description.
|
||||||
|
|
||||||
|
source (Optional[str]): The source site. Defaults to url.
|
||||||
|
|
||||||
|
image (Optional[str]): The content's image.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, title: str, url: str,
|
||||||
|
desc: str = "No description provided.",
|
||||||
|
source: Optional[str] = None, image: Optional[str] = None):
|
||||||
|
self.url = url
|
||||||
|
if title in [None, ""]:
|
||||||
|
self.title = "Unknown"
|
||||||
|
else:
|
||||||
|
self.title = title
|
||||||
|
self.desc = desc
|
||||||
|
self.source = source
|
||||||
|
self.image = image
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
fmt = f"<Image url={self.url} title={self.title} source={self.source}>"
|
||||||
|
return fmt
|
||||||
|
|
||||||
|
|
||||||
|
class NormalSource(menus.AsyncIteratorPageSource):
|
||||||
|
def __init__(self, query: str, fetcher: FetcherArgs, per_page: int,
|
||||||
|
header: str = "", footer: str = ""):
|
||||||
|
self.header = header
|
||||||
|
self.footer = footer
|
||||||
|
self.query = query
|
||||||
|
|
||||||
|
super().__init__(self._generate(fetcher), per_page=per_page)
|
||||||
|
|
||||||
|
async def _generate(self, fetcher: Fetcher):
|
||||||
|
offset = 0
|
||||||
|
per_request = 10
|
||||||
|
# TODO put the generation in the fetcher itself
|
||||||
|
# Qwant: image - media, source - url, title - title
|
||||||
|
while results := await fetcher(
|
||||||
|
offset, per_request, self.query
|
||||||
|
):
|
||||||
|
results
|
||||||
|
for r in results:
|
||||||
|
yield r
|
||||||
|
offset += per_request
|
||||||
|
|
||||||
|
async def format_page(self, menu, entries):
|
||||||
|
start = menu.current_page * self.per_page
|
||||||
|
|
||||||
|
# Escapes all nasties for displaying
|
||||||
|
query_display = discord.utils.escape_mentions(self.query)
|
||||||
|
query_display = discord.utils.escape_markdown(query_display)
|
||||||
|
|
||||||
|
# Return if no results
|
||||||
|
try:
|
||||||
|
entries[0]
|
||||||
|
except IndexError:
|
||||||
|
return f"No results found for `{query_display}`."
|
||||||
|
|
||||||
|
# Gets the first entry's stuff
|
||||||
|
first_title = tomd.handle(entries[0].title).rstrip('\n')
|
||||||
|
first_url = entries[0].url
|
||||||
|
if start == 0:
|
||||||
|
first_desc = tomd.handle(entries[0].desc).rstrip('\n')
|
||||||
|
first = f"**{first_title}** {first_url}\n{first_desc}\n\n"
|
||||||
|
else:
|
||||||
|
first = f"**{first_title}** {first_url}\n"
|
||||||
|
|
||||||
|
# Builds the substring for each of the other results.
|
||||||
|
other_results: List[str] = []
|
||||||
|
|
||||||
|
for e in entries[1:5]:
|
||||||
|
title = tomd.handle(e.title).rstrip('\n')
|
||||||
|
url = e.url
|
||||||
|
other_results.append(f"**{title}** {url}")
|
||||||
|
|
||||||
|
other_msg = "\n".join(other_results)
|
||||||
|
|
||||||
|
# Builds message
|
||||||
|
msg = f"{first}{other_msg}"
|
||||||
|
msg = re.sub(
|
||||||
|
r'(https?://(?:www\.)?[-a-zA-Z0-9@:%._+~#=]+\.'
|
||||||
|
r'[a-zA-Z0-9()]+\b[-a-zA-Z0-9()@:%_+.~#?&/=]*)',
|
||||||
|
r'<\1>',
|
||||||
|
msg
|
||||||
|
)
|
||||||
|
|
||||||
|
content = (
|
||||||
|
f"{self.header}\n\n"
|
||||||
|
f"Showing results *{start} - {start + 5}* "
|
||||||
|
f"for `{self.query}`.\n\n"
|
||||||
|
f"{msg}\n\n"
|
||||||
|
f"{self.footer}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
class ImageSource(menus.AsyncIteratorPageSource):
|
||||||
|
def __init__(self, query: str, fetcher: FetcherArgs, args: FetcherArgs,
|
||||||
|
header: str = "", footer: str = ""):
|
||||||
|
self.header = header
|
||||||
|
self.footer = footer
|
||||||
|
self.query = query
|
||||||
|
super().__init__(self._generate(fetcher, args), per_page=1)
|
||||||
|
|
||||||
|
async def _generate(self, fetcher: Fetcher, fetch_args: FetcherArgs):
|
||||||
|
offset = 0
|
||||||
|
per_request = 10
|
||||||
|
# TODO put the generation in the fetcher itself
|
||||||
|
# Qwant: image - media, source - url, title - title
|
||||||
|
while results := await fetcher(
|
||||||
|
offset, per_request, self.query, *fetch_args
|
||||||
|
):
|
||||||
|
results
|
||||||
|
for r in results:
|
||||||
|
yield r
|
||||||
|
offset += per_request
|
||||||
|
|
||||||
|
async def format_page(self, menu, entry):
|
||||||
|
start = menu.current_page * self.per_page
|
||||||
|
|
||||||
|
content = (
|
||||||
|
f"{self.header}\n\n"
|
||||||
|
f"Showing image result `{start}` for `{self.query}``.\n\n"
|
||||||
|
f"<{entry.image}>"
|
||||||
|
f"{self.footer}\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
embed = discord.Embed(
|
||||||
|
title=entry.title,
|
||||||
|
url=entry.image,
|
||||||
|
description=entry.source
|
||||||
|
)
|
||||||
|
embed.set_image(url=entry.image)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"content": content,
|
||||||
|
"embed": embed
|
||||||
|
}
|
4
main.py
4
main.py
|
@ -53,6 +53,10 @@ class Bot(commands.Bot):
|
||||||
self.custom_help: bool = self.config['CUSTOM_HELP']
|
self.custom_help: bool = self.config['CUSTOM_HELP']
|
||||||
self.mention_assist: bool = self.config['MENTION_ASSIST']
|
self.mention_assist: bool = self.config['MENTION_ASSIST']
|
||||||
self.prefixless_dms: bool = self.config['PREFIXLESS_DMS']
|
self.prefixless_dms: bool = self.config['PREFIXLESS_DMS']
|
||||||
|
if self.config['REMOVE_MENTIONS']:
|
||||||
|
self.allowed_mentions = discord.AllowedMentions.none()
|
||||||
|
else:
|
||||||
|
self.allowed_mentions = discord.AllowedMentions.all()
|
||||||
|
|
||||||
# RethinkDB
|
# RethinkDB
|
||||||
if self.config['RETHINK']['DB']:
|
if self.config['RETHINK']['DB']:
|
||||||
|
|
Loading…
Reference in a new issue