async def bottomify(self, ctx: LightningContext, *, flags): """Turns a message into bottom""" if flags.random is True and flags.lastmessage is True: raise commands.BadArgument( "--lastmessage and --random cannot be mixed together.") if flags.random is True: message = await self.get_previous_messages(ctx, ctx.channel, 35) if message.content: text = message.content else: raise LightningError('Failed to find any message content.') elif flags.lastmessage is True: messages = await self.get_previous_messages( ctx, ctx.channel, 1, False) message = messages[0] if message.content: text = message.content else: raise LightningError( 'Failed to find message content in the previous message.') else: text = flags.rest if not text: raise commands.BadArgument("Missing text to translate into bottom") if flags.regress: try: await ctx.send(bottom.decode(text)) except ValueError: await ctx.send("Failed to decode message.") else: await ctx.send(bottom.encode(text))
async def owoify(self, ctx: LightningContext, **args) -> None: """Turns a message into owo-speak""" if args['random'] is True and args['lastmessage'] is True: raise commands.BadArgument( "--lastmessage and --random cannot be mixed together.") if args['random'] is True: message = await self.get_previous_messages(ctx, ctx.channel, 20) if message.content: text = message.content else: raise LightningError('Failed to find any message content.') elif args['lastmessage'] is True: messages = await self.get_previous_messages( ctx, ctx.channel, 1, False) message = messages[0] if message.content: text = message.content else: raise LightningError( 'Failed to find message content in the previous message.') else: text = args['rest'] if not text: raise commands.BadArgument("Missing text to translate into owo") fmt = slowo.UwU.ify(text) await ctx.send(fmt)
async def convert(self, ctx, argument): if len(argument) == 2: try: message = int(argument[0]) except ValueError: raise LightningError("Not a valid message ID.") channel = await ReadableChannel().convert(ctx, argument[1]) return message, channel # regex from d.py argument = argument[0] link_regex = re.compile( r'^https?://(?:(ptb|canary)\.)?discord(?:app)?\.com/channels/' r'(?:([0-9]{15,21})|(@me))' r'/(?P<channel_id>[0-9]{15,21})/(?P<message_id>[0-9]{15,21})/?$') match = link_regex.match(argument) if not match: # Same message from channel... try: message = int(argument) except ValueError: raise LightningError("Not a valid message ID.") channel = ctx.channel return message, channel message_id = int(match.group("message_id")) channel_id = match.group("channel_id") channel = await ReadableChannel().convert(ctx, channel_id) return message_id, channel
def __init__(self, url): url_regex = re.compile("https?:\\/\\/(?:www\\.)?.+") if not url_regex.match(url): raise LightningError("Invalid URL") url = yarl.URL(url) if url.host not in WHITELISTED_HOSTS: raise LightningError(f"`\"{url}\"` is not supported.") self.url = url
async def make_request(self, method: str, url: str, *, github=False, json=None): """Makes a request to either the GitLab API or Github API Parameters ----------- method: str The type of method to use. url: str The URL you are requesting. """ if github: headers = self.github_headers headers.update({'Accept': 'application/vnd.github.v3+json'}) else: headers = self.gitlab_headers headers.update({"User-Agent": "Lightning Bot Git Cog"}) async with self.bot.aiosession.request(method, url, headers=headers, json=json) as resp: ratelimit = resp.headers.get("RateLimit-Remaining") if ratelimit == 0: raise LightningError( "Currently ratelimited. Try again later(?)") elif 300 > resp.status >= 200: data = await resp.json() else: raise discord.HTTPException(response=resp, message=str(resp.reason)) return data
async def tinydb(self, ctx: LightningContext, *, search: str) -> None: """Searches for 3DS homebrew on tinydb""" if len(search) <= 3: raise LightningError("Search term cannot be 3 characters or less!") if len(search) >= 50: raise LightningError( "Search term cannot be 50 characters or more!") url = f"https://api.homebrew.space/search/{urllib.parse.quote(search)}" async with self.bot.aiosession.get(url, timeout=30.0) as resp: if resp.status == 200: data = await resp.json() else: raise LightningError( "Tinydb api not available. Try again later?") if not data: raise LightningError("Failed to find that search term!") menu = InfoMenuPages(source=TinyDBPageSource(data), clear_reactions_after=True) await menu.start(ctx)
async def convert(self, ctx, argument): lowered = argument.lower() valid_commands = { c.qualified_name for c in ctx.bot.walk_commands() if c.cog_name not in ('Configuration', 'Owner', 'Jishaku', 'Git') } if lowered not in valid_commands: raise LightningError(f'Command {lowered!r} is not valid.') return lowered
async def get_page(self, specifier): query = """SELECT * FROM (SELECT * FROM infractions {where_clause} ORDER BY created_at {sort} LIMIT 5) subq ORDER BY created_at;""" args = [] if specifier.reference is None: where_clause = 'WHERE guild_id=$1' args.append(self.guild.id) elif specifier.direction is menus.PageDirection.after: where_clause = 'WHERE id > $1 AND guild_id=$2' args.append(specifier.reference[-1]['id']) args.append(self.guild.id) else: # PageDirection.before where_clause = 'WHERE id < $1 AND guild_id=$2' args.append(specifier.reference[0]['id']) args.append(self.guild.id) if self.member: last_arg = int(re.findall(r"\$\d+", where_clause)[-1].strip("$")) where_clause += f' AND user_id=${last_arg + 1}' args.append(self.member.id) if self.moderator: last_arg = int(re.findall(r"\$\d+", where_clause)[-1].strip("$")) where_clause += f' AND moderator_id=${last_arg + 1}' args.append(self.moderator.id) sort = 'ASC' if specifier.direction is menus.PageDirection.after else 'DESC' records = await self.connection.fetch( query.format(where_clause=where_clause, sort=sort), *args) if self._has_ran is False and not records: raise LightningError("No infractions matched your critiera") if self._has_ran is False: self._has_ran = True if not records: raise ValueError return records