Skip to content

Commit b8100f1

Browse files
committed
bump dependencies; use [project.scripts] instead of __main__.py
1 parent 431c697 commit b8100f1

File tree

8 files changed

+1071
-899
lines changed

8 files changed

+1071
-899
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ chmod -R 777 ./
2626

2727
USER nonroot
2828

29-
CMD ["/app/.venv/bin/python", "-m", "my_discord_bot"]
29+
CMD ["/app/.venv/bin/my-discord-bot"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This Discord bot is developed with Python 3.11. Use the same Python version to e
1414

1515
3. Create a virtual python environment, activate it, then install dependencies with `pip install -e .`.
1616

17-
4. Run `python -m my_discord_bot` in your console to start up the bot.
17+
4. Run `my-discord-bot` in your console to start up the bot.
1818

1919
5. To use the music player, you need to run a [Lavalink](https://github.com/freyacodes/Lavalink) instance alongside the bot. Set `LAVALINK_URL` and `LAVALINK_PASSWORD` in `.env`.
2020

compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
LAVALINK_SERVER_PASSWORD: youshallnotpass
3535
LAVALINK_SERVER_SOURCES_YOUTUBE: "false"
3636
# See https://github.com/lavalink-devs/youtube-source for the latest version of the plugin
37-
LAVALINK_PLUGINS_0_DEPENDENCY: dev.lavalink.youtube:youtube-plugin:1.11.4
37+
LAVALINK_PLUGINS_0_DEPENDENCY: dev.lavalink.youtube:youtube-plugin:1.13.2
3838
PLUGINS_YOUTUBE_ENABLED: "true"
3939
PLUGINS_YOUTUBE_CLIENTS_0: MUSIC
4040
PLUGINS_YOUTUBE_CLIENTS_1: WEB

pyproject.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ dependencies = [
1717
"wavelink==3.*",
1818
]
1919

20+
[project.scripts]
21+
my-discord-bot = "my_discord_bot:main"
22+
23+
[build-system]
24+
requires = ["hatchling"]
25+
build-backend = "hatchling.build"
26+
2027
[dependency-groups]
2128
dev = ["ipykernel", "ruff"]
2229

2330
[tool.pyright]
2431
reportCallInDefaultInitializer = true
25-
26-
[build-system]
27-
requires = ["hatchling"]
28-
build-backend = "hatchling.build"

src/my_discord_bot/__init__.py

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,87 @@
1-
from . import cogs, models
1+
import asyncio
2+
import logging
3+
import os
4+
from logging.handlers import TimedRotatingFileHandler
5+
from pathlib import Path
6+
7+
import discord
8+
from alembic import command
9+
from alembic.config import Config
10+
from dotenv import load_dotenv
11+
from sqlalchemy import event
12+
from sqlalchemy.engine.interfaces import DBAPIConnection
13+
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
14+
from sqlalchemy.pool import ConnectionPoolEntry
15+
216
from .bot import DiscordBot
317

4-
__all__ = ["DiscordBot", "models", "cogs"]
18+
# Initialization
19+
Path("./volume/logs").mkdir(parents=True, exist_ok=True)
20+
Path("./volume/gallery-dl").mkdir(parents=True, exist_ok=True)
21+
22+
# Logger
23+
logger = logging.getLogger()
24+
logger.setLevel("INFO")
25+
logger.addHandler(logging.StreamHandler())
26+
# Time Rotating File Handler
27+
logHandler = TimedRotatingFileHandler(
28+
Path("./volume/logs/discord.log"), when="D", backupCount=10, encoding="utf-8"
29+
)
30+
logHandler.setFormatter(
31+
logging.Formatter(
32+
"%(asctime)s %(levelname)-8s %(name)-15s %(message)s",
33+
datefmt="%H:%M:%S",
34+
)
35+
)
36+
logger.addHandler(logHandler)
37+
38+
39+
# Load .env and basic sanity checking
40+
load_dotenv(dotenv_path="./.env")
41+
for env in ["DISCORD_TOKEN", "PREFIX"]:
42+
if not os.getenv(env):
43+
errMsg = f"{env} is not set in both .env and the OS environment. Exiting..."
44+
logger.error(errMsg)
45+
raise Exception(errMsg)
46+
47+
logging.info("Running migrations...")
48+
alembic_cfg = Config("./alembic.ini")
49+
alembic_cfg.attributes["no_alembic_loggers"] = True
50+
command.upgrade(alembic_cfg, "head")
51+
52+
53+
async def main() -> None:
54+
"""Entrypoint of the bot."""
55+
56+
logging.info("\nSetting up the bot...")
57+
58+
# Database initialization
59+
engine = create_async_engine("sqlite+aiosqlite:///volume/db.sqlite3", echo=True)
60+
61+
@event.listens_for(engine.sync_engine, "connect")
62+
def set_sqlite_pragma(
63+
dbapi_connection: DBAPIConnection, connection_record: ConnectionPoolEntry
64+
) -> None:
65+
"""If using SQLite, enable foreign key constraints."""
66+
cursor = dbapi_connection.cursor()
67+
cursor.execute("PRAGMA foreign_keys=ON")
68+
cursor.close()
69+
70+
async_session = async_sessionmaker(engine, expire_on_commit=False)
71+
72+
intents = discord.Intents.default()
73+
intents.members = True
74+
intents.message_content = True
75+
76+
activity = discord.Game(name="/help")
77+
78+
description = "Discord bot my own use. \nCreated with [discord.py](https://github.com/Rapptz/discord.py)."
79+
80+
bot = DiscordBot(
81+
os.getenv("PREFIX", ">>"), intents, activity, description, async_session
82+
)
83+
84+
await bot.start(os.getenv("DISCORD_TOKEN", ""))
85+
86+
87+
asyncio.run(main())

src/my_discord_bot/__main__.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/my_discord_bot/cogs/music.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ async def connect_music(
474474
self,
475475
ia: discord.Interaction,
476476
) -> None:
477-
"""(OWNER ONLY) Use this if the music player is not working.
477+
"""(OWNER ONLY) Use this to reconnect to a Lavalink node.
478478
479479
Do NOT use this when the bot is playing music."""
480480

0 commit comments

Comments
 (0)