Add logging and environment variable loading
This commit is contained in:
parent
a6f868fb8a
commit
660207ff31
3 changed files with 57 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,4 @@
|
|||
venv
|
||||
frogbot.log
|
||||
# environment vars for development
|
||||
.env
|
||||
|
|
53
frogbot.py
Normal file
53
frogbot.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
import os
|
||||
import logging
|
||||
import logging.handlers
|
||||
import discord
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
||||
# Set discord library logging level to INFO
|
||||
# Configure logging to disk and stderr
|
||||
logger = logging.getLogger('discord')
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
stderr_loghandler = logging.StreamHandler()
|
||||
loghandler = logging.handlers.RotatingFileHandler(
|
||||
filename='frogbot.log',
|
||||
encoding='utf-8',
|
||||
maxBytes=128 * 1024 * 1024, # 128 MiB
|
||||
backupCount=10, # Rotate through 10 files
|
||||
)
|
||||
|
||||
logger_dt_fmt = '%Y-%m-%d %H:%M:%S'
|
||||
logger_formatter = logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', logger_dt_fmt, style='{')
|
||||
|
||||
loghandler.setFormatter(logger_formatter)
|
||||
stderr_loghandler.setFormatter(logger_formatter)
|
||||
|
||||
logger.addHandler(stderr_loghandler)
|
||||
logger.addHandler(loghandler)
|
||||
|
||||
# Set up discord intents
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
|
||||
client = discord.Client(intents=intents)
|
||||
|
||||
# Grab bot tokens and other data from environment variables
|
||||
load_dotenv()
|
||||
bot_token = os.getenv("FROGTOKEN", None)
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
logging.info(f'We have logged in as {client.user}')
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
if message.author == client.user:
|
||||
return
|
||||
|
||||
if message.content.startswith('$hello'):
|
||||
await message.channel.send('Hello!')
|
||||
|
||||
# Suppress the default configuration since we have our own
|
||||
client.run(token=bot_token, log_handler=None)
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
python-dotenv==1.0.1
|
Loading…
Add table
Reference in a new issue