Move logging to systemd, add instructions

This commit is contained in:
Will Bradley 2024-04-10 16:36:11 -07:00
parent 9bf136abf5
commit f817ad7459
Signed by: will
GPG Key ID: 1159B930701263F3
2 changed files with 59 additions and 8 deletions

View File

@ -13,13 +13,51 @@ Forked from [SparkBot](https://github.com/jkkicks/SparkBot)
## Installation ## Installation
Requires Python 3. - Get this source code:
- Run `pip install -r requirements.txt` ```
cd /opt
git clone https://git.zyphon.com/will/MineBot.git
cd MineBot
```
- It's best to run services as non-root users:
```
sudo useradd minebot
sudo mkdir /home/minebot
sudo chown minebot:minebot /home/minebot
sudo chown -R minebot:minebot /opt/MineBot
```
- Create a startup script (this is for systemd on i.e. Ubuntu):
```
[Unit]
Description=MineBot Service
After=multi-user.target
[Service]
Type=simple
User=minebot
Group=minebot
Restart=always
WorkingDirectory=/opt/MineBot
ExecStart=/usr/bin/python3 /opt/MineBot/main.py
[Install]
WantedBy=multi-user.target
```
- This bot requires Python 3, so ensure you have that on your system.
- Run `sudo -u minebot pip install -r requirements.txt` to install Python dependencies.
## Running ## Running
Run `python3 main.py` - Run `sudo service minebot start` or for testing/development you can run `python3 main.py`
- Hide sensitive data from other users:
- `sudo chmod 600 /opt/MineBot/bot_data.db /opt/MineBot/bot.log .env`
- You probably want to run MineBot automatically on system startup:
- `sudo systemctl enable minebot`
## Usage ## Usage

23
main.py
View File

@ -4,7 +4,7 @@ from discord import app_commands
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
import sqlite3 import sqlite3
import logging import logging as logginglib
from datetime import datetime, timezone from datetime import datetime, timezone
from discord import ui, Interaction from discord import ui, Interaction
import random import random
@ -21,8 +21,21 @@ load_dotenv()
TOKEN = os.getenv('BOT_TOKEN') TOKEN = os.getenv('BOT_TOKEN')
# Setup logging # Setup logging
logging.basicConfig(filename='bot.log', level=logging.INFO) logging = logginglib.getLogger(__name__)
handler = logginglib.StreamHandler()
formatter = logginglib.Formatter('[{levelname:<8}] {message}', style='{')
logging.setLevel(logginglib.INFO)
handler.setLevel(logginglib.INFO)
handler.setFormatter(formatter)
logging.addHandler(handler)
# Modify Discord logging
discLog = logginglib.getLogger('discord')
handler = logginglib.StreamHandler()
formatter = logginglib.Formatter('[{levelname:<8}] {name}: {message}', style='{')
discLog.setLevel(logginglib.INFO)
handler.setLevel(logginglib.INFO)
handler.setFormatter(formatter)
discLog.addHandler(handler)
## Function and Class Definitions ## ## Function and Class Definitions ##
@ -194,7 +207,7 @@ async def listservers(interaction: discord.Interaction):
for row in c.fetchall(): for row in c.fetchall():
o += '- **ID**: {}\n - **Status URL**: {}\n - **Join URL**: {}\n'.format(row[0], row[1], row[2]) o += '- **ID**: {}\n - **Status URL**: {}\n - **Join URL**: {}\n'.format(row[0], row[1], row[2])
if o == "": if o == "":
o = "No servers." o = "You're not added to any servers."
await interaction.response.send_message(content=o, ephemeral=True) await interaction.response.send_message(content=o, ephemeral=True)
@ -313,4 +326,4 @@ with sqlite3.connect('bot_data.db') as conn:
conn.commit() conn.commit()
# Start bot # Start bot
bot.run(TOKEN) bot.run(TOKEN, log_handler=None)