This repository has been archived on 2025-02-16. You can view files and clone it, but cannot push or open issues or pull requests.
Spiffo/cogs/activity.py
Corban-Lee Jones cffe8546c1
All checks were successful
Build and Push Docker Image / build (push) Successful in 13s
move activity task starter into 'on_ready' event
it was being started before the bot websocket was established
2024-12-06 17:17:30 +00:00

53 lines
1.5 KiB
Python

"""
Handles the activity displayed on the bot's Discord profile.
The current intent is to display the server's player count as the activity.
"""
import re
import logging
from discord import Game, Status
from discord.ext import commands, tasks
from rcon.source import rcon
log = logging.getLogger(__name__)
class ActivityCog(commands.Cog):
"""
Handles the bot's profile activity.
"""
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
if not self.update_activity.is_running():
self.update_activity.start()
@tasks.loop(seconds=30)
async def update_activity(self):
"""
Update the bot's profile activity on an interval of 30 seconds.
"""
log.debug("updating activity")
players_message = await rcon("players", **self.bot.rcon_details)
re_match = re.search(r"Players connected \((\d+)\):\s*", players_message)
if not re_match:
log.error("Failed to parse player count from rcon response: %s", players_message)
return
players_count = int(re_match.group(1))
activity = Game(name=f"with {players_count} survivor{'s' if players_count != 1 else ''}!")
await self.bot.change_presence(activity=activity, status=Status.online)
log.debug("player count in activity updated to: %s", players_count)
async def setup(bot: commands.Bot):
cog = ActivityCog(bot)
await bot.add_cog(cog)
log.info("Added %s cog", cog.__class__.__name__)