Fix admin voting page crash

This commit is contained in:
Jeremy Zhang 2018-07-23 05:02:26 +00:00
parent 1e80512ca7
commit 0203f24ce7
4 changed files with 25 additions and 20 deletions

View File

@ -208,3 +208,17 @@ class RedisQueue:
await self.delete_guild(guild) await self.delete_guild(guild)
await self.on_get_guild(key, {"guild_id": guild.id}) await self.on_get_guild(key, {"guild_id": guild.id})
await self.enforce_expiring_key(key) await self.enforce_expiring_key(key)
async def on_get_user(self, key, params):
user = self.bot.get_user(int(params["user_id"]))
if not user:
await self.connection.set(key, "")
return
user_formatted = {
"id": user.id,
"username": user.name,
"discriminator": user.discriminator,
"avatar": user.avatar,
"bot": user.bot
}
await self.connection.set(key, json.dumps(user_formatted, separators=(',', ':')))

View File

@ -435,19 +435,9 @@ def voting_get():
users = db.session.query(DiscordBotsOrgTransactions).filter(DiscordBotsOrgTransactions.timestamp >= start, DiscordBotsOrgTransactions.timestamp <= end).order_by(DiscordBotsOrgTransactions.timestamp) users = db.session.query(DiscordBotsOrgTransactions).filter(DiscordBotsOrgTransactions.timestamp >= start, DiscordBotsOrgTransactions.timestamp <= end).order_by(DiscordBotsOrgTransactions.timestamp)
all_users = [] all_users = []
for u in users: for u in users:
uid = u.user_id # Let's fix this OBO error
gmember = db.session.query(GuildMembers).filter(GuildMembers.user_id == uid).first()
count = 0
if not gmember:
uid = uid - 10
while uid < u.user_id + 10:
gmember = db.session.query(GuildMembers).filter(GuildMembers.user_id == uid).first()
if gmember:
break
uid = uid + 1
all_users.append({ all_users.append({
"id": u.id, "id": u.id,
"user_id": uid, "user_id": u.user_id,
"timestamp": u.timestamp, "timestamp": u.timestamp,
"action": u.action, "action": u.action,
"referrer": u.referrer "referrer": u.referrer
@ -458,8 +448,6 @@ def voting_get():
action = u["action"] action = u["action"]
if uid not in overall_votes: if uid not in overall_votes:
overall_votes[uid] = 0 overall_votes[uid] = 0
if action == "none":
overall_votes[uid] = overall_votes[uid] - 1
if action == "upvote": if action == "upvote":
overall_votes[uid] = overall_votes[uid] + 1 overall_votes[uid] = overall_votes[uid] + 1
sorted_overall_votes = [] sorted_overall_votes = []
@ -467,13 +455,13 @@ def voting_get():
sorted_overall_votes.append(uid) sorted_overall_votes.append(uid)
overall = [] overall = []
for uid in sorted_overall_votes: for uid in sorted_overall_votes:
gmember = db.session.query(GuildMembers).filter(GuildMembers.user_id == uid).first() gmember = redisqueue.get_user(uid)
u = { u = {
"user_id": uid, "user_id": uid,
"votes": overall_votes[uid] "votes": overall_votes[uid]
} }
if gmember: if gmember:
u["discord"] = gmember.username + "#" + str(gmember.discriminator) u["discord"] = gmember["username"] + "#" + str(gmember["discriminator"])
overall.append(u) overall.append(u)
referrer = {} referrer = {}
for u in all_users: for u in all_users:
@ -488,12 +476,12 @@ def voting_get():
sorted_referrers.append(uid) sorted_referrers.append(uid)
referrals = [] referrals = []
for uid in sorted_referrers: for uid in sorted_referrers:
gmember = db.session.query(GuildMembers).filter(GuildMembers.user_id == uid).first() gmember = redisqueue.get_user(uid)
u = { u = {
"user_id": uid, "user_id": uid,
"votes": referrer[uid] "votes": referrer[uid]
} }
if gmember: if gmember:
u["discord"] = gmember.username + "#" + str(gmember.discriminator) u["discord"] = gmember["username"] + "#" + str(gmember["discriminator"])
referrals.append(u) referrals.append(u)
return render_template("admin_voting.html.j2", overall=overall, referrals=referrals, datestart=datestart, timestart=timestart, dateend=dateend, timeend=timeend) return render_template("admin_voting.html.j2", overall=overall, referrals=referrals, datestart=datestart, timestart=timestart, dateend=dateend, timeend=timeend)

View File

@ -540,8 +540,6 @@ def webhook_discordbotsorg_vote():
params = dict(parse_qsl(urlsplit(incoming.get("query", "")).query)) params = dict(parse_qsl(urlsplit(incoming.get("query", "")).query))
if vote_type == "upvote": if vote_type == "upvote":
redis_store.set("DiscordBotsOrgVoted/" + user_id, "voted", 86400) redis_store.set("DiscordBotsOrgVoted/" + user_id, "voted", 86400)
else:
redis_store.delete("DiscordBotsOrgVoted/" + user_id)
referrer = None referrer = None
if "referrer" in params: if "referrer" in params:
try: try:

View File

@ -114,3 +114,8 @@ class RedisQueue:
key = "/guilds/{}".format(guild_id) key = "/guilds/{}".format(guild_id)
q = self.get(key, "get_guild", {"guild_id": guild_id}) q = self.get(key, "get_guild", {"guild_id": guild_id})
return q return q
def get_user(self, user_id):
key = "/users/{}".format(user_id)
q = self.get(key, "get_user", {"user_id": user_id})
return q