Files
discordbot/cogs/statchannel.py
T
esefandClaude Opus 5 93e22a8364 statchannel: ignore steam's bogus zero player counts
Steam's GetNumberOfCurrentPlayers occasionally answers 200 with
player_count: 0 for a valid appid. Both Strinova stat channels were
renamed to "0" on the same 09:48 tick on 2026-10-20 (the only zero in
~300 renames over 3 days), and the name sat wrong until the next
10-minute tick.

Skip the rename on a non-positive count so the channel keeps its last
good value, and log it so the next one is visible. !strinova had the
same bug and would reply "0 players online right now".

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-10-20 13:12:57 +03:00

74 lines
2.6 KiB
Python

import aiohttp
from discord.ext import commands, tasks
import os
class StatChannel(commands.Cog):
def __init__(self, bot):
self.bot = bot
def _parse_id(key: str) -> int | None:
val = os.getenv(key, "")
return int(val) if val.isdigit() else None
self.channel_id = _parse_id('STRINOVA_STAT_CHANNEL_ID')
self.channel_id_mao = _parse_id('STRINOVA_STAT_CHANNEL_ID_MAO')
self.member_channel_id = _parse_id('MEMBER_COUNT_CHANNEL_ID')
self.update_stat.start()
if self.channel_id_mao is not None:
self.update_stat_mao.start()
if self.member_channel_id is not None:
self.update_members.start()
def cog_unload(self):
self.update_stat.cancel()
self.update_stat_mao.cancel()
self.update_members.cancel()
async def _fetch_and_update(self, channel_id):
try:
async with aiohttp.ClientSession() as session:
async with session.get('https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid=1282270') as r:
data = await r.json()
count = data['response']['player_count']
if not isinstance(count, int) or count <= 0:
print(f'stat channel update skipped (channel {channel_id}): steam returned {count!r}')
return
channel = self.bot.get_channel(channel_id)
if channel:
await channel.edit(name=f'Strinova Players From Steam: {count:,}')
except Exception as e:
print(f'stat channel update failed (channel {channel_id}): {e}')
@tasks.loop(minutes=10)
async def update_stat(self):
await self._fetch_and_update(self.channel_id)
@tasks.loop(minutes=10)
async def update_stat_mao(self):
await self._fetch_and_update(self.channel_id_mao)
@tasks.loop(minutes=10)
async def update_members(self):
try:
channel = self.bot.get_channel(self.member_channel_id)
if channel:
await channel.edit(name=f'Member Count: {channel.guild.member_count:,}')
except Exception as e:
print(f'member count update failed (channel {self.member_channel_id}): {e}')
@update_stat.before_loop
async def before_update_stat(self):
await self.bot.wait_until_ready()
@update_stat_mao.before_loop
async def before_update_stat_mao(self):
await self.bot.wait_until_ready()
@update_members.before_loop
async def before_update_members(self):
await self.bot.wait_until_ready()
async def setup(bot):
await bot.add_cog(StatChannel(bot))