def update_guild_properties(self, settings, changes, timestamp): """Updates Discord guild properties Parameters ---------- settings : [google.cloud.firestore_v1.document.DocumentSnapshot] complete document snapshot changes : [google.cloud.firestore_v1.watch.DocumentChange] database changes in the sent snapshot timestamp : int timestamp indicating time of change in the database """ try: for change in changes: guildId = change.document.id properties = change.document.to_dict() if "addons" not in properties: database.document("discord/properties/guilds/{}".format(guildId)).set(Utils.create_guild_settings(properties)) continue if properties["addons"]["satellites"].get("count") is not None and not properties["addons"]["satellites"]["enabled"]: properties["addons"]["satellites"].pop("count", None) database.document("discord/properties/guilds/{}".format(guildId)).set(properties) continue if change.type.name in ["ADDED", "MODIFIED"]: self.guildProperties[guildId] = properties else: self.guildProperties.pop(guildId) self.serviceStatus[2] = True except Exception: print(traceback.format_exc()) if os.environ["PRODUCTION_MODE"]: self.logging.report_exception(user=guildId)
def guild_validation(self, guildId, properties): try: if "addons" not in properties or "settings" not in properties: database.document( "discord/properties/guilds/{}".format(guildId)).set( Utils.create_guild_settings(properties)) return True if "stale" in properties: if properties["stale"].get("count", 0) >= 96: database.document("discord/properties/guilds/{}".format( guildId)).delete() return True elif properties["stale"].get("timestamp", time()) <= time() - 86400: database.document( "discord/properties/guilds/{}".format(guildId)).set( {"stale": DELETE_FIELD}, merge=True) return True if properties["addons"]["satellites"]["enabled"]: addedSatellites = properties["addons"]["satellites"].get( "added", []) satelliteCount = len(addedSatellites) if satelliteCount > properties["addons"]["satellites"].get( "count", 0): accountProperties = self.accountProperties.get( properties["addons"]["satellites"].get("connection")) if accountProperties["customer"][ "personalSubscription"].get( "subscription") is not None: if environ["PRODUCTION_MODE"]: subscription = stripe.Subscription.retrieve( accountProperties["customer"] ["personalSubscription"]["subscription"]) cycleRatio = ( subscription["current_period_end"] - time() ) / (subscription["current_period_end"] - subscription["current_period_start"]) quantity = int( ceil((satelliteCount - properties["addons"]["satellites"].get( "count", 0)) * 20 * cycleRatio)) stripe.SubscriptionItem.create_usage_record( subscription["items"]["data"][0]["id"], quantity=quantity, timestamp=int(time()), action="increment") database.document( "discord/properties/guilds/{}".format( guildId)).set( { "addons": { "satellites": { "enabled": True, "count": satelliteCount } } }, merge=True) return True else: print("{}: {} satellites".format( guildId, satelliteCount)) elif properties["addons"]["satellites"].get("count") is not None: properties["addons"]["satellites"].pop("count", None) properties["addons"]["satellites"].pop("added", None) database.document("discord/properties/guilds/{}".format( guildId)).set(properties) return True except: print(format_exc()) if environ["PRODUCTION_MODE"]: self.logging.report_exception(user=guildId) return False