def has_premium(self): now = int(time.time()) if self.negative_balance_cents: return False elif self.premium_expires_utc > now: return True elif self.coin_balance >= 1: self.coin_balance -= 1 self.premium_expires_utc = now + 60 * 60 * 24 * 7 add_role(self, "premium") g.db.add(self) return True else: if self.premium_expires_utc: delete_role(self, "premium") self.premium_expires_utc = 0 g.db.add(self) return False
def ban(self, admin=None, reason=None, days=0): if days > 0: ban_time = int(time.time()) + (days * 86400) self.unban_utc = ban_time else: # Takes care of all functions needed for account termination self.unban_utc = 0 if self.has_banner: self.del_banner() if self.has_profile: self.del_profile() add_role(self, "banned") delete_role(self, "member") self.is_banned = admin.id if admin else 1 if reason: self.ban_reason = reason try: g.db.add(self) except: pass
def unban(self): # Takes care of all functions needed for account reinstatement. self.is_banned = 0 self.unban_utc = 0 delete_role(self, "banned") g.db.add(self)
def ban(self, admin=None, reason=None, days=0): self.is_banned = admin.id if admin else 1 if reason: self.ban_reason = reason g.db.add(self) g.db.flush() if days > 0: ban_time = int(time.time()) + (days * 86400) self.unban_utc = ban_time else: # Takes care of all functions needed for account termination self.unban_utc = 0 if self.has_banner: self.del_banner() if self.has_profile: self.del_profile() add_role(self, "banned") delete_role(self, "member") #unprivate guilds if no mods remaining for b in self.boards_modded: if b.mods_count == 0: b.is_private = False b.restricted_posting = False #b.all_opt_out = False g.db.add(b) try: g.db.add(self) except: pass
def discord_redirect(v): #validate state now = int(time.time()) state = request.args.get('state', '').split('.') timestamp = state[0] state = state[1] if int(timestamp) < now - 600: abort(400) if not validate_hash(f"{timestamp}+{v.id}+discord", state): abort(400) #get discord token code = request.args.get("code", "") if not code: abort(400) data = { "client_id": CLIENT_ID, 'client_secret': CLIENT_SECRET, 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': f"https://{app.config['SERVER_NAME']}/discord_redirect", 'scope': 'identify guilds.join' } headers = {'Content-Type': 'application/x-www-form-urlencoded'} url = "https://discord.com/api/oauth2/token" x = requests.post(url, headers=headers, data=data) x = x.json() try: token = x["access_token"] except KeyError: abort(403) #get user ID url = "https://discord.com/api/users/@me" headers = {'Authorization': f"Bearer {token}"} x = requests.get(url, headers=headers) x = x.json() #add user to discord headers = { 'Authorization': f"Bot {BOT_TOKEN}", 'Content-Type': "application/json" } #remove existing user if applicable if v.discord_id and v.discord_id != x['id']: url = f"https://discord.com/api/guilds/{SERVER_ID}/members/{v.discord_id}" requests.delete(url, headers=headers) if g.db.query(User).filter(User.id != v.id, User.discord_id == x["id"]).first(): return render_template( "message.html", title="Discord account already linked.", error="That Discord account is already in use by another user.", v=v) v.discord_id = x["id"] g.db.add(v) g.db.commit() url = f"https://discord.com/api/guilds/{SERVER_ID}/members/{x['id']}" name = v.username if v.real_id: name += f" | {v.real_id}" data = { "access_token": token, "nick": name, } x = requests.put(url, headers=headers, json=data) if x.status_code in [201, 204]: add_role(v, "linked") if v.is_banned and v.unban_utc == 0: add_role(v, "banned") if v.has_premium: add_role(v, "premium") else: return jsonify(x.json()) #check on if they are already there #print(x.status_code) if x.status_code == 204: ##if user is already a member, remove old roles and update nick delete_role(v, "nick") if v.real_id: add_role(v, "realid") url = f"https://discord.com/api/guilds/{SERVER_ID}/members/{v.discord_id}" data = {"nick": name} req = requests.patch(url, headers=headers, json=data) #print(req.status_code) #print(url) return redirect( f"https://discord.com/channels/{SERVER_ID}/{WELCOME_CHANNEL}")