debug mode var and ingame_channel shorthand getter
All checks were successful
Build and Push Docker Image / build (push) Successful in 23s
All checks were successful
Build and Push Docker Image / build (push) Successful in 23s
This commit is contained in:
parent
3e622fcecb
commit
77cb214276
25
bot.py
25
bot.py
@ -11,7 +11,7 @@ import logging.config
|
|||||||
from os import getenv
|
from os import getenv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from discord import Intents
|
from discord import Intents, TextChannel
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from tortoise import Tortoise
|
from tortoise import Tortoise
|
||||||
@ -28,12 +28,9 @@ class DiscordBot(commands.Bot):
|
|||||||
Represents a Discord bot.
|
Represents a Discord bot.
|
||||||
Contains controls to interact with the bot via the Discord API.
|
Contains controls to interact with the bot via the Discord API.
|
||||||
"""
|
"""
|
||||||
in_game_channel_id: int
|
def __init__(self, debug_mode: bool):
|
||||||
steam_api_key: str
|
|
||||||
rcon_details: dict
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__(command_prefix="-", intents=Intents.all())
|
super().__init__(command_prefix="-", intents=Intents.all())
|
||||||
|
self.debug_mode = debug_mode
|
||||||
self.in_game_channel_id = int(getenv("SPIFFO__DISCORD_CHANNEL_ID"))
|
self.in_game_channel_id = int(getenv("SPIFFO__DISCORD_CHANNEL_ID"))
|
||||||
self.steam_api_key = getenv("SPIFFO__STEAM_API_KEY")
|
self.steam_api_key = getenv("SPIFFO__STEAM_API_KEY")
|
||||||
self.rcon_details = {
|
self.rcon_details = {
|
||||||
@ -75,6 +72,12 @@ class DiscordBot(commands.Bot):
|
|||||||
if path.suffix == ".py":
|
if path.suffix == ".py":
|
||||||
await self.load_extension(f"cogs.{path.stem}")
|
await self.load_extension(f"cogs.{path.stem}")
|
||||||
|
|
||||||
|
async def get_ingame_channel(self) -> TextChannel:
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
channel = self.bot.get_channel(self.bot.in_game_channel_id)
|
||||||
|
return channel or await self.bot.fetch_channel(self.bot.in_game_channel_id)
|
||||||
|
|
||||||
|
|
||||||
def get_bot_token() -> str:
|
def get_bot_token() -> str:
|
||||||
"""
|
"""
|
||||||
@ -87,7 +90,7 @@ def get_bot_token() -> str:
|
|||||||
|
|
||||||
return bot_token
|
return bot_token
|
||||||
|
|
||||||
def setup_logging_config():
|
def setup_logging_config(debug_mode: bool):
|
||||||
"""
|
"""
|
||||||
Loads the logging configuration and creates an asynchronous queue handler.
|
Loads the logging configuration and creates an asynchronous queue handler.
|
||||||
"""
|
"""
|
||||||
@ -101,6 +104,9 @@ def setup_logging_config():
|
|||||||
# Ensure the logging directory exists
|
# Ensure the logging directory exists
|
||||||
(BASE_DIR / "logs").mkdir(exist_ok=True)
|
(BASE_DIR / "logs").mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
# update the log level dependent on debug mode
|
||||||
|
log_config["loggers"]["root"]["level"] = "DEBUG" if debug_mode else "INFO"
|
||||||
|
|
||||||
# Load the config
|
# Load the config
|
||||||
logging.config.dictConfig(log_config) # This 'logging.config' path is jank.
|
logging.config.dictConfig(log_config) # This 'logging.config' path is jank.
|
||||||
|
|
||||||
@ -114,10 +120,11 @@ async def main():
|
|||||||
"""
|
"""
|
||||||
The entrypoint function, initialises the application.
|
The entrypoint function, initialises the application.
|
||||||
"""
|
"""
|
||||||
setup_logging_config()
|
debug_mode = getenv("SPIFFO__DEBUG").lower() == "true"
|
||||||
|
setup_logging_config(debug_mode)
|
||||||
bot_token = get_bot_token()
|
bot_token = get_bot_token()
|
||||||
|
|
||||||
async with DiscordBot() as bot:
|
async with DiscordBot(debug_mode) as bot:
|
||||||
await bot.load_cogs()
|
await bot.load_cogs()
|
||||||
await bot.start(bot_token, reconnect=True)
|
await bot.start(bot_token, reconnect=True)
|
||||||
|
|
||||||
|
@ -16,11 +16,7 @@ from utils.models import Player
|
|||||||
|
|
||||||
ZOMBOID_FOLDER_PATH = Path(getenv("SPIFFO__ZOMBOID_FOLDER_PATH"))
|
ZOMBOID_FOLDER_PATH = Path(getenv("SPIFFO__ZOMBOID_FOLDER_PATH"))
|
||||||
LOGS_FOLDER_PATH = ZOMBOID_FOLDER_PATH / "Logs"
|
LOGS_FOLDER_PATH = ZOMBOID_FOLDER_PATH / "Logs"
|
||||||
USER_LOG_FILE_PATH = None
|
USER_LOG_FILE_PATH = next(LOGS_FOLDER_PATH.glob("*_user.txt"), None)
|
||||||
for path in LOGS_FOLDER_PATH.iterdir():
|
|
||||||
if path.name.endswith("_user.txt"):
|
|
||||||
USER_LOG_FILE_PATH = path
|
|
||||||
break
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -38,6 +34,8 @@ class PlayersCog(commands.Cog):
|
|||||||
|
|
||||||
@tasks.loop(seconds=3)
|
@tasks.loop(seconds=3)
|
||||||
async def listen_for_changes(self):
|
async def listen_for_changes(self):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
log.debug("listening for changes")
|
log.debug("listening for changes")
|
||||||
for line in await self.file_handler.read():
|
for line in await self.file_handler.read():
|
||||||
await self.process_log_line(line)
|
await self.process_log_line(line)
|
||||||
@ -52,7 +50,7 @@ class PlayersCog(commands.Cog):
|
|||||||
elif "disconnected player" in line:
|
elif "disconnected player" in line:
|
||||||
await self.process_disconnected_player(line)
|
await self.process_disconnected_player(line)
|
||||||
|
|
||||||
async def process_player_death(self, line: str):
|
async def process_player_death(self, line: str, alert: bool = False):
|
||||||
log.debug("processing player death")
|
log.debug("processing player death")
|
||||||
re_pattern = r"\[(?P<timestamp>[\d\- :\.]+)\] user (?P<username>.+?) died at \((?P<x>\d+),(?P<y>\d+),(?P<z>\d+)\) \((?P<cause>.+?)\)"
|
re_pattern = r"\[(?P<timestamp>[\d\- :\.]+)\] user (?P<username>.+?) died at \((?P<x>\d+),(?P<y>\d+),(?P<z>\d+)\) \((?P<cause>.+?)\)"
|
||||||
re_match = re.search(re_pattern, line)
|
re_match = re.search(re_pattern, line)
|
||||||
@ -78,19 +76,19 @@ class PlayersCog(commands.Cog):
|
|||||||
)
|
)
|
||||||
await player.save()
|
await player.save()
|
||||||
|
|
||||||
channel = self.bot.get_channel(self.bot.in_game_channel_id)
|
log.debug("successfully registered player death to %s", re_match.group("username"))
|
||||||
channel = channel or await self.bot.fetch_channel(self.bot.in_game_channel_id)
|
|
||||||
|
|
||||||
|
if not alert:
|
||||||
|
return
|
||||||
|
|
||||||
|
channel = await self.bot.get_ingame_channel()
|
||||||
embed = await player.get_embed()
|
embed = await player.get_embed()
|
||||||
embed.title = "Player Has Died"
|
embed.title = "Player Has Died"
|
||||||
embed.colour = Colour.dark_orange()
|
embed.colour = Colour.dark_orange()
|
||||||
|
|
||||||
await channel.send(embed=embed)
|
await channel.send(embed=embed)
|
||||||
|
|
||||||
log.debug("successfully registered player death to %s", re_match.group("username"))
|
async def process_connected_player(self, line: str, alert: bool = False):
|
||||||
|
|
||||||
|
|
||||||
async def process_connected_player(self, line: str):
|
|
||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
log.debug("processing connected player")
|
log.debug("processing connected player")
|
||||||
@ -115,16 +113,17 @@ class PlayersCog(commands.Cog):
|
|||||||
await player.save()
|
await player.save()
|
||||||
return
|
return
|
||||||
|
|
||||||
channel = self.bot.get_channel(self.bot.in_game_channel_id)
|
if not alert:
|
||||||
channel = channel or await self.bot.fetch_channel(self.bot.in_game_channel_id)
|
return
|
||||||
|
|
||||||
|
channel = await self.bot.get_ingame_channel()
|
||||||
embed = await player.get_embed()
|
embed = await player.get_embed()
|
||||||
embed.title = "Player Has Connected"
|
embed.title = "Player Has Connected"
|
||||||
embed.colour = Colour.brand_green()
|
embed.colour = Colour.brand_green()
|
||||||
|
|
||||||
await channel.send(embed=embed)
|
await channel.send(embed=embed)
|
||||||
|
|
||||||
async def process_disconnected_player(self, line: str):
|
async def process_disconnected_player(self, line: str, alert: bool = False):
|
||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
log.debug("processing disconnected player")
|
log.debug("processing disconnected player")
|
||||||
@ -143,12 +142,10 @@ class PlayersCog(commands.Cog):
|
|||||||
await player.update_steam_summary(re_match.group("steam_id"), self.bot.steam_api_key)
|
await player.update_steam_summary(re_match.group("steam_id"), self.bot.steam_api_key)
|
||||||
await player.save()
|
await player.save()
|
||||||
|
|
||||||
if player.is_dead:
|
if player.is_dead or not alert:
|
||||||
return
|
return
|
||||||
|
|
||||||
channel = self.bot.get_channel(self.bot.in_game_channel_id)
|
channel = await self.bot.get_ingame_channel()
|
||||||
channel = channel or await self.bot.fetch_channel(self.bot.in_game_channel_id)
|
|
||||||
|
|
||||||
embed = await player.get_embed()
|
embed = await player.get_embed()
|
||||||
embed.title = "Player Has Disconnected"
|
embed.title = "Player Has Disconnected"
|
||||||
embed.colour = Colour.brand_red()
|
embed.colour = Colour.brand_red()
|
||||||
|
Reference in New Issue
Block a user