async def update_activity_stats(msg: discord.Message): """Update activity statistics for a user""" if ChannelUtil.is_private(msg.channel): return member = msg.author # Ignore if user doesnt have rain role has_rain_role = False rain_roles = config.Config.instance().get_rain_roles() if len(rain_roles) > 0: for role in member.roles: if role.id in rain_roles: has_rain_role = True break if not has_rain_role: return content_adjusted = Utils.emoji_strip(msg.content) if len(content_adjusted) == 0: return # Get user OBJ from redis if it exists, else create one user_key = f"activity:{msg.guild.id}:{msg.author.id}" active_stats = await RedisDB.instance().get(user_key) if active_stats is None: # Create stats and save active_stats = { 'user_id': msg.author.id, 'last_msg': datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M:%S'), 'msg_count': 1 } await RedisDB.instance().set(user_key, json.dumps(active_stats), expires=1800) return else: active_stats = json.loads(active_stats) # Ignore em if they've messaged too recently last_msg_dt = datetime.datetime.strptime(active_stats['last_msg'], '%m/%d/%Y %H:%M:%S') delta_s = (datetime.datetime.utcnow() - last_msg_dt).total_seconds() if 90 > delta_s: return elif delta_s > 1200: # Deduct a point if active_stats['msg_count'] > 1: active_stats['msg_count'] -= 1 active_stats['last_msg'] = datetime.datetime.utcnow().strftime( '%m/%d/%Y %H:%M:%S') await RedisDB.instance().set(user_key, json.dumps(active_stats), expires=1800) else: # add a point if active_stats['msg_count'] <= Constants.RAIN_MSG_REQUIREMENT * 2: active_stats['msg_count'] += 1 active_stats['last_msg'] = datetime.datetime.utcnow().strftime( '%m/%d/%Y %H:%M:%S') await RedisDB.instance().set(user_key, json.dumps(active_stats), expires=1800) else: # Reset key expiry active_stats['last_msg'] = datetime.datetime.utcnow().strftime( '%m/%d/%Y %H:%M:%S') await RedisDB.instance().set(user_key, json.dumps(active_stats), expires=1800)
def test_emoji_strip(self): self.assertEqual(Utils.emoji_strip("å—æ¼¢å—Hello😊myfriend\u2709"), "å—æ¼¢å—Hellomyfriend")