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

@@ -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)