for entry in entries:
if entry['time'] + 1800 < time():
guild = self._bot.get_guild(int(entry['guild_id']))
member = guild.get_member(int(entry['user_id']))
if member is not None:
if member.activity.name is not None:
if member.activity.name.lower() == "league of legends":
await member.send("The 30 minutes has elapsed and you are still playing league, get banned.")
await member.ban(delete_message_days=0, reason='playing league')
I had a roomie that played lol. After a month of not having a job and looking very scruffy he emerged from his filthy bedroom and gave me a grocery list. We were on good terms until I told him he had an addiction.
I turned em off when my boss at the time noticed I was always playing FFXIV. I wasn't always actually playing, I used to leave it open almost all the time. Why was my boss and I in the same discord? This was early COVID and we used a discord to shoot the shit. Also almost all of us gamed, including said boss, and I probably wouldn't have really gotten in trouble anyway, but I just explained I leave it open a lot and he accepted that without qualm.
I worked with chatgpt since I'm not a python dev, and this is what I came up with
from time import time
class PlaySession:
def __init__(self, data: dict):
self.guild_id = int(data['guild_id'])
self.user_id = int(data['user_id'])
self.timestamp = data['time']
def is_longer_than_half_hour(self) -> bool:
return self.timestamp + 1800 < time()
async def resolve_member(self, bot) -> "discord.Member | None":
guild = bot.get_guild(self.guild_id)
return guild.get_member(self.user_id) if guild else None
@staticmethod
def is_playing_league(member) -> bool:
activity = getattr(member, 'activity', None)
name = getattr(activity, 'name', None)
return name and name.lower() == "league of legends"
async def ban_for_league(member):
await member.send("The 30 minutes has elapsed and you are still playing league, get banned.")
await member.ban(delete_message_days=0, reason="playing league")
async def process_entries(bot, entry_dicts):
sessions = [PlaySession(d) for d in entry_dicts if PlaySession(d).is_longer_than_half_hour()]
for session in sessions:
member = await session.resolve_member(bot)
if member and PlaySession.is_playing_league(member):
await ban_for_league(member)
Not only nested ifs. It's not even correct (doesn't check for activity existing). And it's not even pythonic (ask for forgiveness, not for permission). Just access the thing, catch the exception and be done with it.
Correct me if I'm wrong, but the script works faster with nested IFs since if it fails at the first one, it won't bother reading next ones, unless you kill/return from function.
All in all, this feels like readibility argument, not correctness or efficienty argument
It's especially spicy when you consider that one single normal League match can easily extend beyond 30 minutes. Hell, even a lighter mode (ARAM) can be 30-35+ minutes at times.