Skip to content

Commit 94960cd

Browse files
authored
Merge pull request #2 from nonchris/packaging
Packaging
2 parents 373053d + 3213b1a commit 94960cd

File tree

11 files changed

+99
-27
lines changed

11 files changed

+99
-27
lines changed

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include LICENSE.txt
2+
3+
include setup.py

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Generic, functional bot based on discord.py
33
Including a custom help command and ping command, utils for easy embed creation, logging configuration, and a general bot setup
44

55
## setup
6-
`pip install -r requirements.txt`
6+
` python3 -m pip install -e .`
77
`export TOKEN="your-key"`
8-
`python3 main.py`
8+
`discord-bot`
99

1010
#### optional env variables
1111
| parameter | description |
@@ -40,10 +40,7 @@ https://github.com/nonchris/discord-fury
4040
https://github.com/nonchris/quiz-bot
4141
https://github.com/Info-Bonn/poll-bot
4242

43-
I collected the most useful and generic functions to save me some time when starting the next bot-project.
44-
45-
### dependencies
46-
This project is based on `discord.py V1.x` minimum required: V1.5.1
43+
I collected the most useful and generic functions to save me some time when starting the next bot-project.
4744

4845
### documentation
4946
In order to render this documentation, just call `doxygen`

setup.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from setuptools import setup, find_packages
2+
import pathlib
3+
4+
here = pathlib.Path(__file__).parent.resolve()
5+
6+
# Get the long description from the README file
7+
long_description = (here / 'README.md').read_text(encoding='utf-8')
8+
9+
setup(
10+
# TODO: Adjust your project information here
11+
name='discord-bot',
12+
version='2.0.0',
13+
description='A discord bot template',
14+
long_description=long_description,
15+
long_description_content_type='text/markdown',
16+
url='https://github.com/nonchris/discord-bot',
17+
author='nonchris',
18+
author_email='[email protected]',
19+
20+
project_urls={
21+
'Bug Reports': 'https://github.com/nonchris/discord-bot/issues',
22+
'Source': 'https://github.com/https://github.com/nonchris/discord-bot',
23+
},
24+
25+
keywords='discord-bot',
26+
27+
python_requires='>=3.8, <4',
28+
29+
install_requires='discord.py ~= 1.7.2',
30+
31+
classifiers=[
32+
33+
'Development Status :: 5 - Production/Stable',
34+
35+
'Environment :: Console',
36+
37+
'Intended Audience :: Other Audience',
38+
'Topic :: Communications :: Chat',
39+
40+
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
41+
42+
'Programming Language :: Python :: 3',
43+
'Programming Language :: Python :: 3.8',
44+
'Programming Language :: Python :: 3.9',
45+
'Programming Language :: Python :: 3 :: Only',
46+
47+
'Typing :: Typed',
48+
],
49+
50+
package_dir={'': 'src/'},
51+
52+
packages=find_packages(where='src/'),
53+
54+
entry_points={
55+
'console_scripts': [
56+
'discord-bot=bot:main',
57+
],
58+
},
59+
)

src/bot/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .main import start_bot
2+
3+
4+
def main():
5+
start_bot()

src/bot/cogs/__init__.py

Whitespace-only changes.

src/cogs/help.py renamed to src/bot/cogs/help.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import discord
22
from discord.ext import commands
33

4-
from utils import utils as utl
5-
from environment import OWNER_NAME, OWNER_ID, VERSION, PREFIX
4+
from ..utils import utils as utl
5+
from ..environment import OWNER_NAME, OWNER_ID, VERSION, PREFIX
66

77
### @package help
88
#

src/cogs/misc.py renamed to src/bot/cogs/misc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from discord.ext import commands
22

3-
from utils import utils as ut
3+
from ..log_setup import logger
4+
from ..utils import utils as ut
45

56

67
### @package misc
@@ -23,7 +24,7 @@ async def ping(self, ctx):
2324
2425
@param ctx Context of the message
2526
"""
26-
print(f"ping: {round(self.bot.latency * 1000)}")
27+
logger.info(f"ping: {round(self.bot.latency * 1000)}")
2728

2829
await ctx.send(
2930
embed=ut.make_embed(
File renamed without changes.

src/log_setup.py renamed to src/bot/log_setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
# logger for writing to file
1717
file_logger = logging.FileHandler('data/events.log')
18-
file_logger.setLevel(logging.INFO) # everything into the logging file
18+
file_logger.setLevel(logging.INFO)
1919
file_logger.setFormatter(formatter)
2020

2121
# logger for console prints
2222
console_logger = logging.StreamHandler()
23-
console_logger.setLevel(logging.WARNING) # only important stuff to the terminal
23+
console_logger.setLevel(logging.INFO)
2424
console_logger.setFormatter(formatter)
2525

2626
# get new logger

src/main.py renamed to src/bot/main.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
# setup of logging and env-vars
88
# logging must be initialized before environment, to enable logging in environment
9-
from log_setup import logger
10-
from environment import PREFIX, TOKEN
9+
from .log_setup import logger
10+
from .environment import PREFIX, TOKEN
1111

1212
"""
1313
This bot is based on a template by nonchris
@@ -45,32 +45,39 @@ def _prefix_callable(_bot: commands.Bot, msg: discord.Message):
4545
@bot.event
4646
async def on_ready():
4747
"""!
48-
function called when the bot is ready. emits the '[Bot] has connected' message
48+
function called when the bot is ready. Emits the '[Bot] has connected' message
4949
"""
50-
print(f'{bot.user.name} has connected')
5150

52-
logger.info(f"Bot has connected, active on {len(bot.guilds)} guilds")
53-
54-
print(f'Bot is connected to the following guilds:')
5551
print()
5652
member_count = 0
53+
guild_string = ""
5754
for g in bot.guilds:
58-
print(f"{g.name} - {g.id} - Members: {g.member_count}")
55+
guild_string += f"{g.name} - {g.id} - Members: {g.member_count}\n"
5956
member_count += g.member_count
60-
print()
57+
58+
logger.info(f"Bot '{bot.user.name}' has connected, active on {len(bot.guilds)} guilds:\n{guild_string}")
59+
6160
await bot.change_presence(
6261
activity=discord.Activity(type=discord.ActivityType.watching, name=f"{PREFIX}help"))
6362

64-
65-
if __name__ == '__main__':
6663
# LOADING Extensions
64+
# this is done in on_ready() so that cogs can fetch data from discord when they're loaded
6765
bot.remove_command('help') # unload default help message
66+
# TODO: Register your extensions here
6867
initial_extensions = [
69-
'cogs.misc',
70-
'cogs.help'
68+
'.cogs.misc',
69+
'.cogs.help'
7170
]
7271

7372
for extension in initial_extensions:
74-
bot.load_extension(extension)
73+
bot.load_extension(extension, package=__package__)
74+
7575

76-
bot.run(TOKEN)
76+
def start_bot(token=None):
77+
""" Start the bot, takes token, uses token from env if none is given """
78+
if token is not None:
79+
bot.run(token)
80+
if TOKEN is not None:
81+
bot.run(TOKEN)
82+
else:
83+
logger.error("No token was given! - Exiting")

0 commit comments

Comments
 (0)