From f9894cfea023a2e2a79d1abaf3fdf509cfd9d15f Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 21:18:33 +0200 Subject: [PATCH 1/8] Move code from src/ to src/bot/ --- src/{ => bot}/cogs/help.py | 0 src/{ => bot}/cogs/misc.py | 0 src/{ => bot}/environment.py | 0 src/{ => bot}/log_setup.py | 0 src/{ => bot}/main.py | 0 src/{ => bot}/utils/utils.py | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename src/{ => bot}/cogs/help.py (100%) rename src/{ => bot}/cogs/misc.py (100%) rename src/{ => bot}/environment.py (100%) rename src/{ => bot}/log_setup.py (100%) rename src/{ => bot}/main.py (100%) rename src/{ => bot}/utils/utils.py (100%) diff --git a/src/cogs/help.py b/src/bot/cogs/help.py similarity index 100% rename from src/cogs/help.py rename to src/bot/cogs/help.py diff --git a/src/cogs/misc.py b/src/bot/cogs/misc.py similarity index 100% rename from src/cogs/misc.py rename to src/bot/cogs/misc.py diff --git a/src/environment.py b/src/bot/environment.py similarity index 100% rename from src/environment.py rename to src/bot/environment.py diff --git a/src/log_setup.py b/src/bot/log_setup.py similarity index 100% rename from src/log_setup.py rename to src/bot/log_setup.py diff --git a/src/main.py b/src/bot/main.py similarity index 100% rename from src/main.py rename to src/bot/main.py diff --git a/src/utils/utils.py b/src/bot/utils/utils.py similarity index 100% rename from src/utils/utils.py rename to src/bot/utils/utils.py From 265d117805202f07348847e6fdec1f3a76ed1293 Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 23:17:38 +0200 Subject: [PATCH 2/8] Change packaging and imports --- src/bot/__init__.py | 5 +++++ src/bot/cogs/__init__.py | 0 src/bot/cogs/help.py | 4 ++-- src/bot/cogs/misc.py | 2 +- src/bot/main.py | 11 ++++++----- 5 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 src/bot/__init__.py create mode 100644 src/bot/cogs/__init__.py diff --git a/src/bot/__init__.py b/src/bot/__init__.py new file mode 100644 index 0000000..af58fea --- /dev/null +++ b/src/bot/__init__.py @@ -0,0 +1,5 @@ +from .main import start_bot + + +def main(): + start_bot() diff --git a/src/bot/cogs/__init__.py b/src/bot/cogs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/bot/cogs/help.py b/src/bot/cogs/help.py index 00fb92f..f17f359 100644 --- a/src/bot/cogs/help.py +++ b/src/bot/cogs/help.py @@ -1,8 +1,8 @@ import discord from discord.ext import commands -from utils import utils as utl -from environment import OWNER_NAME, OWNER_ID, VERSION, PREFIX +from ..utils import utils as utl +from ..environment import OWNER_NAME, OWNER_ID, VERSION, PREFIX ### @package help # diff --git a/src/bot/cogs/misc.py b/src/bot/cogs/misc.py index 11889cb..079a0e2 100644 --- a/src/bot/cogs/misc.py +++ b/src/bot/cogs/misc.py @@ -1,6 +1,6 @@ from discord.ext import commands -from utils import utils as ut +from ..utils import utils as ut ### @package misc diff --git a/src/bot/main.py b/src/bot/main.py index e66468d..53f7fb1 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -6,8 +6,8 @@ # setup of logging and env-vars # logging must be initialized before environment, to enable logging in environment -from log_setup import logger -from environment import PREFIX, TOKEN +from .log_setup import logger +from .environment import PREFIX, TOKEN """ This bot is based on a template by nonchris @@ -66,11 +66,12 @@ async def on_ready(): # LOADING Extensions bot.remove_command('help') # unload default help message initial_extensions = [ - 'cogs.misc', - 'cogs.help' + '.cogs.misc', + '.cogs.help' ] for extension in initial_extensions: - bot.load_extension(extension) + bot.load_extension(extension, package=__package__) + bot.run(TOKEN) From 188637ada7ee5290ceaede836441cd80d9c69046 Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 23:18:04 +0200 Subject: [PATCH 3/8] Add MANIFEST.in --- MANIFEST.in | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..3812355 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,3 @@ +include LICENSE.txt + +include setup.py From 3954b6de1c8e6c6b2f3cdffc0c44a5ee48cef5c1 Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 23:18:15 +0200 Subject: [PATCH 4/8] Add setup.py --- setup.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 setup.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e8a16ec --- /dev/null +++ b/setup.py @@ -0,0 +1,59 @@ +from setuptools import setup, find_packages +import pathlib + +here = pathlib.Path(__file__).parent.resolve() + +# Get the long description from the README file +long_description = (here / 'README.md').read_text(encoding='utf-8') + +setup( + # TODO: Adjust your project information here + name='discord-bot', + version='2.0.0', + description='A discord bot template', + long_description=long_description, + long_description_content_type='text/markdown', + url='https://github.com/nonchris/discord-bot', + author='nonchris', + author_email='info@nonchris.eu', + + project_urls={ + 'Bug Reports': 'https://github.com/nonchris/discord-bot/issues', + 'Source': 'https://github.com/https://github.com/nonchris/discord-bot', + }, + + keywords='discord-bot', + + python_requires='>=3.8, <4', + + install_requires='discord.py ~= 1.7.2', + + classifiers=[ + + 'Development Status :: 5 - Production/Stable', + + 'Environment :: Console', + + 'Intended Audience :: Other Audience', + 'Topic :: Communications :: Chat', + + 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', + + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3 :: Only', + + 'Typing :: Typed', + ], + + package_dir={'': 'src/'}, + + packages=find_packages(where='src/'), + + entry_points={ + 'console_scripts': [ + 'discord-bot=bot:main', + ], + }, +) From efbd6ff519582f7a6b9b0c87958ed54225d59dc9 Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 23:29:00 +0200 Subject: [PATCH 5/8] Update startup function for bot --- src/bot/main.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/bot/main.py b/src/bot/main.py index 53f7fb1..493f433 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -61,8 +61,6 @@ async def on_ready(): await bot.change_presence( activity=discord.Activity(type=discord.ActivityType.watching, name=f"{PREFIX}help")) - -if __name__ == '__main__': # LOADING Extensions bot.remove_command('help') # unload default help message initial_extensions = [ @@ -74,4 +72,11 @@ async def on_ready(): bot.load_extension(extension, package=__package__) - bot.run(TOKEN) +def start_bot(token=None): + """ Start the bot, takes token, uses token from env if none is given """ + if token is not None: + bot.run(token) + if TOKEN is not None: + bot.run(TOKEN) + else: + logger.error("No token was given! - Exiting") From 60a1b4f90be00ac548d0d16e106dcddfd93de51d Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 23:31:43 +0200 Subject: [PATCH 6/8] Update readme --- README.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0ccf720..c30533d 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ Generic, functional bot based on discord.py Including a custom help command and ping command, utils for easy embed creation, logging configuration, and a general bot setup ## setup -`pip install -r requirements.txt` +` python3 -m pip install -e .` `export TOKEN="your-key"` -`python3 main.py` +`discord-bot` #### optional env variables | parameter | description | @@ -40,10 +40,7 @@ https://github.com/nonchris/discord-fury https://github.com/nonchris/quiz-bot https://github.com/Info-Bonn/poll-bot -I collected the most useful and generic functions to save me some time when starting the next bot-project. - -### dependencies -This project is based on `discord.py V1.x` minimum required: V1.5.1 +I collected the most useful and generic functions to save me some time when starting the next bot-project. ### documentation In order to render this documentation, just call `doxygen` From 95ab2be89f07d61eda1497b07572aff345e6d3d1 Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 23:45:33 +0200 Subject: [PATCH 7/8] Set console logging to info level --- src/bot/log_setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bot/log_setup.py b/src/bot/log_setup.py index 101e418..060de1d 100644 --- a/src/bot/log_setup.py +++ b/src/bot/log_setup.py @@ -15,12 +15,12 @@ # logger for writing to file file_logger = logging.FileHandler('data/events.log') -file_logger.setLevel(logging.INFO) # everything into the logging file +file_logger.setLevel(logging.INFO) file_logger.setFormatter(formatter) # logger for console prints console_logger = logging.StreamHandler() -console_logger.setLevel(logging.WARNING) # only important stuff to the terminal +console_logger.setLevel(logging.INFO) console_logger.setFormatter(formatter) # get new logger From 3213b1a66fed8cd6342facc0ba99e59e269bfa24 Mon Sep 17 00:00:00 2001 From: nonchris Date: Thu, 2 Sep 2021 23:46:02 +0200 Subject: [PATCH 8/8] Change prints to logging --- src/bot/cogs/misc.py | 3 ++- src/bot/main.py | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/bot/cogs/misc.py b/src/bot/cogs/misc.py index 079a0e2..38078d2 100644 --- a/src/bot/cogs/misc.py +++ b/src/bot/cogs/misc.py @@ -1,5 +1,6 @@ from discord.ext import commands +from ..log_setup import logger from ..utils import utils as ut @@ -23,7 +24,7 @@ async def ping(self, ctx): @param ctx Context of the message """ - print(f"ping: {round(self.bot.latency * 1000)}") + logger.info(f"ping: {round(self.bot.latency * 1000)}") await ctx.send( embed=ut.make_embed( diff --git a/src/bot/main.py b/src/bot/main.py index 493f433..98efa6b 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -45,24 +45,25 @@ def _prefix_callable(_bot: commands.Bot, msg: discord.Message): @bot.event async def on_ready(): """! - function called when the bot is ready. emits the '[Bot] has connected' message + function called when the bot is ready. Emits the '[Bot] has connected' message """ - print(f'{bot.user.name} has connected') - logger.info(f"Bot has connected, active on {len(bot.guilds)} guilds") - - print(f'Bot is connected to the following guilds:') print() member_count = 0 + guild_string = "" for g in bot.guilds: - print(f"{g.name} - {g.id} - Members: {g.member_count}") + guild_string += f"{g.name} - {g.id} - Members: {g.member_count}\n" member_count += g.member_count - print() + + logger.info(f"Bot '{bot.user.name}' has connected, active on {len(bot.guilds)} guilds:\n{guild_string}") + await bot.change_presence( activity=discord.Activity(type=discord.ActivityType.watching, name=f"{PREFIX}help")) # LOADING Extensions + # this is done in on_ready() so that cogs can fetch data from discord when they're loaded bot.remove_command('help') # unload default help message + # TODO: Register your extensions here initial_extensions = [ '.cogs.misc', '.cogs.help'