Guild member duplicate checking in the database

This commit is contained in:
Jeremy Zhang 2017-09-26 03:52:46 +00:00
parent 70aae0ab22
commit c42f808715

View File

@ -158,7 +158,8 @@ class DatabaseInterface(object):
with self.get_session() as session: with self.get_session() as session:
dbmember = session.query(GuildMembers) \ dbmember = session.query(GuildMembers) \
.filter(GuildMembers.guild_id == member.server.id) \ .filter(GuildMembers.guild_id == member.server.id) \
.filter(GuildMembers.user_id == member.id).first() .filter(GuildMembers.user_id == member.id) \
.order_by(GuildMembers.id).all()
if not dbmember: if not dbmember:
dbmember = GuildMembers( dbmember = GuildMembers(
member.server.id, member.server.id,
@ -173,6 +174,10 @@ class DatabaseInterface(object):
) )
session.add(dbmember) session.add(dbmember)
else: else:
if len(dbmember) > 1:
for mem in dbmember[1:]:
session.delete(mem)
dbmember = dbmember[0]
dbmember.banned = banned dbmember.banned = banned
dbmember.active = active dbmember.active = active
dbmember.username = member.name dbmember.username = member.name