Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vc xp fix #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 91 additions & 48 deletions cosmos/galaxies/profile/ascension.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,66 +49,109 @@ def get_vc_members(voice_channel):
except AttributeError:
return []

@staticmethod
def valid_members(members: list) -> list:
return [m for m in members if
not m.voice.self_deaf and
not m.voice.mute and
not m.voice.deaf and
not m.voice.self_mute and
not m.voice.afk]

async def get_guild_profile(self, member_id, guild_id):
profile = await self.cache.get_profile(member_id)
return await profile.get_guild_profile(guild_id)

@Cog.listener()
async def on_voice_state_update(self, member, before, after):
if member.bot or after.afk:
if member.bot:
return

guild_profile = await self.get_guild_profile(member.id, member.guild.id)

if ((before.mute and not after.mute)
or (before.self_mute and not after.self_mute)
or (not before.channel and after.channel and not (after.mute or after.self_mute))):

# if len(self.get_vc_members(after.channel)) <= 1:
# return # Wait for someone to join vc.

guild_profile.record_voice_activity()
guild_profile.profile.record_voice_activity()

if ((not before.mute and after.mute)
or (not before.self_mute and after.self_mute)
or (before.channel and not after.channel and not (before.mute or before.self_mute))):
# user leaving a channel
if not after.channel and before.channel:
guild_profile.close_voice_activity()
guild_profile.profile.close_voice_activity()

# if after.channel and not before.channel:
# members = self.get_vc_members(before.channel)
# if not len(members) == 1:
# return
# # Someone joined vc. Check and record voice activity for member who is waiting.
# waiting_member = members[0]
# waiting_member_vs = waiting_member.voice
# if waiting_member_vs.self_mute or waiting_member_vs.mute:
# return
# waiting_profile = await self.get_guild_profile(waiting_member.id, waiting_member.guild.id)
# waiting_profile.record_voice_activity()
#
# if before.channel and not after.channel:
# members = self.get_vc_members(before.channel)
# if not len(members) == 1:
# return
# # Someone left vc and left one member alone. Stop his voice activity.
# waiting_member = members[0]
# waiting_profile = await self.get_guild_profile(waiting_member.id, waiting_member.guild.id)
# waiting_profile.close_voice_activity()

@Cog.listener()
if len(members := self.get_vc_members(before.channel)) > 0:
if len(valid_members := self.valid_members(members)) < 2:
for m in valid_members:
gp = await self.get_guild_profile(m.id, member.guild.id)
gp.close_voice_activity()
gp.profile.close_voice_activity()

# user channel hopping / mute-unmute etc.
if before.channel and after.channel:
# mute-unmute etc.
if before.channel == after.channel:
# if did mute-deaf, stop xp
if ((not before.mute and after.mute) or
(not before.deaf and after.deaf) or
(not before.self_mute and after.self_mute) or
(not before.self_deaf and after.self_mute)):
guild_profile.close_voice_activity()
guild_profile.profile.close_voice_activity()
# if old channel dont have enough members, stop their xp
if len(valid_members := self.valid_members(self.get_vc_members(before.channel))) < 2:
for m in valid_members:
gp = await self.get_guild_profile(m.id, member.guild.id)
gp.close_voice_activity()
gp.profile.close_voice_activity()

# if did unmute-undeaf
elif ((before.mute and not after.mute) or
(before.deaf and not after.deaf) or
(before.self_mute and not after.self_mute) or
(before.self_deaf and not after.self_mute)):
# if channel now has enough valid members, start their xp
if len(valid_members := self.valid_members(self.get_vc_members(after.channel))) > 1:
for m in valid_members:
gp = await self.get_guild_profile(m.id, member.guild.id)
gp.record_voice_activity()
gp.profile.record_voice_activity()
else:
pass

# if channel hop, close activity
elif before.channel != after.channel:
guild_profile.close_voice_activity()
guild_profile.profile.close_voice_activity()

# if new channel has enough members, start their xp
if len(members := self.get_vc_members(after.channel)) > 1:
if len(valid_members := self.valid_members(members)) > 1:
for m in valid_members:
gp = await self.get_guild_profile(m.id, member.guild.id)
gp.record_voice_activity()
gp.profile.record_voice_activity()

# if old channel lost enough members, close their xp
if len(members_remain := self.get_vc_members(before.channel)) < 2:
valid_members = self.valid_members(members_remain)
for m in valid_members:
gp = await self.get_guild_profile(m.id, member.guild.id)
gp.close_voice_activity()
gp.profile.close_voice_activity()
else:
pass

# user join a channel
if not before.channel and after.channel:
# if enough members, start their xp
if len(members := self.get_vc_members(after.channel)) > 1:
if len(valid_members := self.valid_members(members)) > 1:
for m in valid_members:
gp = await self.get_guild_profile(m.id, member.guild.id)
gp.record_voice_activity()
gp.profile.record_voice_activity()

@ Cog.listener()
async def on_ready(self):
for g in self.bot.guilds:
for vc in g.voice_channels:
for user_id, vs in vc.voice_states.items():
user = self.bot.get_user(user_id)
if user.bot:
continue
if vs.self_mute or vs.mute:
continue
# if len(self.get_vc_members(vs.channel)):
# continue
guild_profile = await self.get_guild_profile(user_id, g.id)
guild_profile.record_voice_activity()
guild_profile.profile.record_voice_activity()
if len(members := self.get_vc_members(vc)) > 1:
if len(valid_members := self.valid_members(members)) > 1:
for m in valid_members:
gp = await self.get_guild_profile(m.id, m.guild.id)
gp.record_voice_activity()
gp.profile.record_voice_activity()
18 changes: 13 additions & 5 deletions cosmos/galaxies/profile/models/profiles/guild/experience.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,21 @@ def voice_xp(self):
return round(raw_xp / self.VOICE_XP_CONSTRAIN)

def record_voice_activity(self):
self.__voice_activity_time = time.time()
self.is_speaking = True
self.__voice_level = self.voice_level # Save current voice level.
if not self.is_speaking:
self.__voice_activity_time = time.time()
self.is_speaking = True
self.__voice_level = self.voice_level # Save current voice level.
# print(f'opened {self.name}')
else:
pass

def close_voice_activity(self):
self.is_speaking = False
self.cache_voice_xp()
if self.is_speaking:
self.is_speaking = False
self.cache_voice_xp()
# print(f'closed {self.name}')
else:
pass

@property
def delta_xp(self):
Expand Down