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 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` 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', + ], + }, +) 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/cogs/help.py b/src/bot/cogs/help.py similarity index 98% rename from src/cogs/help.py rename to src/bot/cogs/help.py index 00fb92f..f17f359 100644 --- a/src/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/cogs/misc.py b/src/bot/cogs/misc.py similarity index 84% rename from src/cogs/misc.py rename to src/bot/cogs/misc.py index 11889cb..38078d2 100644 --- a/src/cogs/misc.py +++ b/src/bot/cogs/misc.py @@ -1,6 +1,7 @@ from discord.ext import commands -from utils import utils as ut +from ..log_setup import logger +from ..utils import utils as ut ### @package misc @@ -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/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 81% rename from src/log_setup.py rename to src/bot/log_setup.py index 101e418..060de1d 100644 --- a/src/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 diff --git a/src/main.py b/src/bot/main.py similarity index 68% rename from src/main.py rename to src/bot/main.py index e66468d..98efa6b 100644 --- a/src/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 @@ -45,32 +45,39 @@ 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")) - -if __name__ == '__main__': # 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' + '.cogs.misc', + '.cogs.help' ] for extension in initial_extensions: - bot.load_extension(extension) + 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") 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