Use try-except instead of checking for membership.

Basically, a lot of the checks revolving "if x in y" could be written
more efficiently by doing the task anyway and ignoring the exception
a la the EAFP guideline.
This commit is contained in:
Rapptz 2015-11-20 17:39:19 -05:00
parent 36b145aee2
commit 5ca04ea08f
2 changed files with 18 additions and 9 deletions

View File

@ -311,8 +311,11 @@ class ConnectionState(object):
if server is not None:
user_id = data['user']['id']
member = utils.find(lambda m: m.id == user_id, server.members)
if member in server.members:
try:
server.members.remove(member)
except ValueError:
return
else:
self.dispatch('member_remove', member)
def handle_guild_member_update(self, data):
@ -364,8 +367,11 @@ class ConnectionState(object):
self.dispatch('server_unavailable', server)
return
if server in self.servers:
try:
self.servers.remove(server)
except ValueError:
return
else:
self.dispatch('server_remove', server)
def handle_guild_role_create(self, data):

View File

@ -95,11 +95,14 @@ class Member(User):
if old_channel is None and self.voice_channel is not None:
# we joined a channel
self.voice_channel.voice_members.append(self)
elif old_channel is not None and self.voice_channel is None:
if self in old_channel.voice_members:
# we left a channel
elif old_channel is not None:
try:
# we either left a channel or we switched channels
old_channel.voice_members.remove(self)
elif old_channel is not None and self.voice_channel is not None:
if self in old_channel.voice_members:
old_channel.voice_members.remove(self)
self.voice_channel.voice_members.append(self)
except ValueError:
pass
finally:
# we switched channels
if self.voice_channel is not None:
self.voice_channel.voice_members.append(self)