From a30b4a5412a0e88ac37ae5f524b03886a997dd7c Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Tue, 2 Oct 2018 20:41:07 +0200 Subject: Implemented flake8 and a Discord Bot base. --- bot/bot.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 bot/bot.py (limited to 'bot/bot.py') diff --git a/bot/bot.py b/bot/bot.py new file mode 100644 index 00000000..0813eb54 --- /dev/null +++ b/bot/bot.py @@ -0,0 +1,21 @@ +from pathlib import Path +from sys import stderr +from traceback import print_exc + + +from bot import constants +from discord.ext import commands + + +bot = commands.Bot(command_prefix=commands.when_mentioned_or('!')) + +if __name__ == '__main__': + cogs = [x.stem for x in Path('cogs').glob('*.py')] + for extension in cogs: + try: + bot.load_extension(f'cogs.{extension}') + except Exception as e: + print(f'Failed to load extension {extension}.', file=stderr) + print_exc() + +bot.run(constants.token) -- cgit v1.2.3 From c0436c8a1e6240bae819b82f1237529b3170a74c Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Tue, 2 Oct 2018 22:08:29 +0200 Subject: Add some comments and docstrings documenting the code and replacing minor issues. --- bot/bot.py | 8 ++++---- bot/cogs/template.py | 27 ++++++++++++++++++++++----- bot/constants.py | 1 - requirements-dev.txt | 5 ----- 4 files changed, 26 insertions(+), 15 deletions(-) delete mode 100644 requirements-dev.txt (limited to 'bot/bot.py') diff --git a/bot/bot.py b/bot/bot.py index 0813eb54..177fe1dc 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -1,16 +1,16 @@ from pathlib import Path from sys import stderr from traceback import print_exc +from os import environ -from bot import constants from discord.ext import commands - bot = commands.Bot(command_prefix=commands.when_mentioned_or('!')) if __name__ == '__main__': - cogs = [x.stem for x in Path('cogs').glob('*.py')] + # Scan for files in the /cogs/ directory and make a list of the file names. + cogs = [file.stem for file in Path('cogs').glob('*.py')] for extension in cogs: try: bot.load_extension(f'cogs.{extension}') @@ -18,4 +18,4 @@ if __name__ == '__main__': print(f'Failed to load extension {extension}.', file=stderr) print_exc() -bot.run(constants.token) +bot.run(environ.get('TOKEN')) diff --git a/bot/cogs/template.py b/bot/cogs/template.py index 18b3f21c..da92f6b5 100644 --- a/bot/cogs/template.py +++ b/bot/cogs/template.py @@ -1,14 +1,31 @@ +from os import system + + from discord.ext import commands +"""A template cog that contains examples of commands and command groups.""" + + class Template: def __init__(self, bot): self.bot = bot - - @commands.command() - async def template(self, ctx): - await ctx.send('It indeed is a template cog!') - + @commands.command(name='repo', aliases=['repository', 'project'], brief='A link to the repository of this bot.') + async def repository(self, ctx): + await ctx.send('https://github.com/discord-python/hacktoberbot') + + # A command group with the name git. You can now create sub-commands such as git commit. + @commands.group(name='git', invoke_without_command=True) + async def github(self, ctx): + await ctx.send('Resources to learn **Git**: https://try.github.io/.') + + # A command that belongs to the git command group. Invoked using git commit. + @github.command() + async def commit(self, ctx): + system('git commit -m "A huge commit adding many revolutionary features!"') + + +# Required in order to load the cog, use the class name in the add_cog function. def setup(bot): bot.add_cog(Template(bot)) diff --git a/bot/constants.py b/bot/constants.py index f2376c4d..e69de29b 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -1 +0,0 @@ -token = '' diff --git a/requirements-dev.txt b/requirements-dev.txt deleted file mode 100644 index bdc6b391..00000000 --- a/requirements-dev.txt +++ /dev/null @@ -1,5 +0,0 @@ -flake8 -flake8-bugbear -flake8-bandit -flake8-import-order -flake8-tidy-imports -- cgit v1.2.3 From a135873590b426c566b9d329daf0066fd5426dc3 Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Wed, 3 Oct 2018 19:36:12 +0200 Subject: Fix improper docstrings and comments. --- bot/bot.py | 5 +++-- bot/cogs/template.py | 15 +++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'bot/bot.py') diff --git a/bot/bot.py b/bot/bot.py index 177fe1dc..a67fcdab 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -3,9 +3,10 @@ from sys import stderr from traceback import print_exc from os import environ - from discord.ext import commands + +HACKTOBERBOT_TOKEN = environ.get('HACKTOBERBOT_TOKEN') bot = commands.Bot(command_prefix=commands.when_mentioned_or('!')) if __name__ == '__main__': @@ -18,4 +19,4 @@ if __name__ == '__main__': print(f'Failed to load extension {extension}.', file=stderr) print_exc() -bot.run(environ.get('TOKEN')) +bot.run(HACKTOBERBOT_TOKEN) diff --git a/bot/cogs/template.py b/bot/cogs/template.py index da92f6b5..caac3412 100644 --- a/bot/cogs/template.py +++ b/bot/cogs/template.py @@ -1,13 +1,12 @@ -from os import system - - from discord.ext import commands -"""A template cog that contains examples of commands and command groups.""" +class Template: + """ + A template cog that contains examples of commands and command groups. + """ -class Template: def __init__(self, bot): self.bot = bot @@ -15,15 +14,15 @@ class Template: async def repository(self, ctx): await ctx.send('https://github.com/discord-python/hacktoberbot') - # A command group with the name git. You can now create sub-commands such as git commit. @commands.group(name='git', invoke_without_command=True) async def github(self, ctx): + # A command group with the name git. You can now create sub-commands such as git commit. await ctx.send('Resources to learn **Git**: https://try.github.io/.') - # A command that belongs to the git command group. Invoked using git commit. @github.command() async def commit(self, ctx): - system('git commit -m "A huge commit adding many revolutionary features!"') + # A command that belongs to the git command group. Invoked using git commit. + await ctx.send('`git commit -m "First commit"` commits tracked changes.') # Required in order to load the cog, use the class name in the add_cog function. -- cgit v1.2.3