bot.exts.info.code_snippets#

Get snippets of code.

Module Contents#

Classes#

CodeSnippets

Cog that parses and sends code snippets to Discord.

Functions#

setup(→ None)

Load the CodeSnippets cog.

Attributes#

bot.exts.info.code_snippets.log[source]#
bot.exts.info.code_snippets.GITHUB_RE[source]#
bot.exts.info.code_snippets.GITHUB_GIST_RE[source]#
bot.exts.info.code_snippets.GITHUB_HEADERS[source]#
bot.exts.info.code_snippets.GITLAB_RE[source]#
bot.exts.info.code_snippets.BITBUCKET_RE[source]#
class bot.exts.info.code_snippets.CodeSnippets(bot: bot.bot.Bot)[source]#

Bases: discord.ext.commands.Cog

Cog that parses and sends code snippets to Discord.

Matches each message against a regex and prints the contents of all matched snippets.

async _fetch_response(url: str, response_format: str, **kwargs: dict) str | dict | None[source]#

Make http requests using aiohttp.

_find_ref(path: str, refs: tuple) tuple[source]#

Loop through all branches and tags to find the required ref.

async _fetch_github_snippet(repo: str, path: str, start_line: str, end_line: str) str[source]#

Fetch a snippet from a GitHub repo.

async _fetch_github_gist_snippet(gist_id: str, revision: str, file_path: str, start_line: str, end_line: str) str[source]#

Fetch a snippet from a GitHub gist.

async _fetch_gitlab_snippet(repo: str, path: str, start_line: str, end_line: str) str[source]#

Fetch a snippet from a GitLab repo.

async _fetch_bitbucket_snippet(repo: str, ref: str, file_path: str, start_line: str, end_line: str) str[source]#

Fetch a snippet from a BitBucket repo.

_snippet_to_codeblock(file_contents: str, file_path: str, start_line: str, end_line: str) str[source]#

Given the entire file contents and target lines, creates a code block.

First, we split the file contents into a list of lines and then keep and join only the required ones together.

We then dedent the lines to look nice, and replace all ` characters with `​ to prevent markdown injection.

Finally, we surround the code with ``` characters.

async _parse_snippets(content: str) str[source]#

Parse message content and return a string with a code block for each URL found.

async on_message(message: discord.Message) None[source]#

Check if the message has a snippet link, removes the embed, then sends the snippet contents.

async bot.exts.info.code_snippets.setup(bot: bot.bot.Bot) None[source]#

Load the CodeSnippets cog.