Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to extension based Cogs #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Switch to extension based Cogs #7

wants to merge 3 commits into from

Conversation

tinoythomas
Copy link
Contributor

@tinoythomas tinoythomas commented Oct 3, 2021

This lets us reload the modules with ease during development.
The main entry point was also modified to accomodate this.
The Git Cog was also calling bot.process_commands(), causing duplicate command invocations, which was removed.

!issue command added.

thetimtoy and others added 3 commits October 3, 2021 14:17
This lets us reload the modules with ease during development.
The main entry point was also modified to accomodate this.
The Git Cog was also calling bot.process_commands(), causing duplicate command invocations, which was removed.
Create an issue, with information about the author.
Comment on lines +61 to +66
async with httpx.AsyncClient() as client:
response = await client.post(url, data=data, headers=headers)
if response.status_code == 200:
await success_message(ctx, "Issue created\n" + response.json()["url"])
else:
await failure_message(ctx, "An error occured: `" + response.json()["message"] + "`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be premature for now, but it would be nice to abstract some of this into a GH client.

Copy link

@airsho airsho Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be set up like so: /gh create issue whereas issue lookup could be set up like: /gh issue 2559 and PRs /gh pr 352, could display lists of issues/PRs with pagination: /gh issues 1 and /gh prs 1

from discord.ext import commands


class Help(commands.Cog):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By switching to application (slash) commands, this cog becomes obsolete. A user simply needs to start typing / to get a list of commands and their descriptions.

bot = commands.Bot(help_command=None, command_prefix='!')

for ext in EXTENSIONS:
bot.load_extension(ext)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By adding Cog classes using bot.add_cog we can avoid adding setup() functions to the cog files. (see PR #8 for implementation). By extending the commands.Bot class we have more control over the bot scope and can handle adding cogs more elegantly.

"Accept": "application/vnd.github.v3+json",
"Authorization": "token " + config["SANIC"]["git_token"]
}
async with httpx.AsyncClient() as client:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by extending the commands.Bot class (see PR #8 for implementation) we can attach a bot-level httpclient so we're not creating a new client each time a command is called.

@@ -44,6 +45,26 @@ async def retrieve_github_issue(
repo = f"sanic-{repo}"
await self.lookup(ctx, number, repo)

@commands.command()
async def issue(self, ctx: Context, repo: str, title: str, *, content: str) -> None:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a worthwhile command, however it would be better implemented using slash sub-commands. Considering what @ahopkins mentioned about a GH client, sub-commands would make things easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants