Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions sanic.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from discord.ext import commands

from sanicbot.core.cogs import HelpCog, GitCog
from sanicbot.core.config import config

bot = commands.Bot(help_command=None, command_prefix=None)
git_cog = GitCog(bot)

EXTENSIONS = (
'sanicbot.extensions.git',
'sanicbot.extensions.help',
)


def main():
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.


if __name__ == '__main__':
bot.command_prefix = '!'
bot.add_cog(git_cog)
bot.add_cog(HelpCog(bot))
bot.run(config['SANIC']['token'])


if __name__ == '__main__':
main()
Empty file added sanicbot/extensions/__init__.py
Empty file.
13 changes: 4 additions & 9 deletions sanicbot/core/cogs.py → sanicbot/extensions/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from sanicbot.core.utils import failure_message, success_message


class GitCog(commands.Cog):
class Git(commands.Cog):
issue_pattern = re.compile(r"#(?P<issue_id>[1,2]\d{3})")

def __init__(self, bot):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

async def lookup(self, ctx: Context, number: int, repo: str):
Expand Down Expand Up @@ -51,12 +51,7 @@ async def github_issue_message_listener(self, message: Message):
await self.lookup(
message.channel, int(match.group("issue_id")), "sanic"
)
else:
await self.bot.process_commands(message)


class HelpCog(commands.Cog):
@commands.command()
async def help(self, ctx):
with open("./resources/help.txt") as f:
await ctx.send(f.read())
def setup(bot: commands.Bot):
bot.add_cog(Git(bot))
13 changes: 13 additions & 0 deletions sanicbot/extensions/help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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.

@commands.command()
async def help(self, ctx):
with open("./resources/help.txt") as f:
await ctx.send(f.read())


def setup(bot: commands.Bot):
bot.add_cog(Help())