Skip to content

Commit

Permalink
Merge pull request #2 from nonchris/packaging
Browse files Browse the repository at this point in the history
Packaging
  • Loading branch information
nonchris authored Sep 2, 2021
2 parents 373053d + 3213b1a commit 94960cd
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 27 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include LICENSE.txt

include setup.py
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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`
59 changes: 59 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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',
],
},
)
5 changes: 5 additions & 0 deletions src/bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .main import start_bot


def main():
start_bot()
Empty file added src/bot/cogs/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions src/cogs/help.py → src/bot/cogs/help.py
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
5 changes: 3 additions & 2 deletions src/cogs/misc.py → src/bot/cogs/misc.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/log_setup.py → src/bot/log_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 22 additions & 15 deletions src/main.py → src/bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
File renamed without changes.

0 comments on commit 94960cd

Please sign in to comment.