async def set_server_active(self, ctx): """Sets the object as active for the contextual guild, with default name bindings.""" if await self.is_server_active(ctx): raise NotAllowed("This collection is already installed on this server.") if self.publish_state == PublicationState.PRIVATE and not self.is_owned_by(ctx.author): raise NotAllowed("This collection is private.") # generate default bindings alias_bindings = await self._generate_default_alias_bindings(ctx) snippet_bindings = await self._generate_default_snippet_bindings(ctx) # insert sub doc await self.sub_coll(ctx).insert_one( {"type": "server_active", "subscriber_id": ctx.guild.id, "object_id": self.id, "alias_bindings": alias_bindings, "snippet_bindings": snippet_bindings} ) # incr sub count await ctx.bot.mdb.workshop_collections.update_one( {"_id": self.id}, {"$inc": {"num_guild_subscribers": 1}} ) # log sub event await ctx.bot.mdb.analytics_alias_events.insert_one( {"type": "server_subscribe", "object_id": self.id, "timestamp": datetime.datetime.utcnow(), "user_id": ctx.author.id} )
async def subscribe(self, ctx): """Adds the contextual author as a subscriber, with default name bindings.""" if await self.is_subscribed(ctx): raise NotAllowed("You are already subscribed to this.") if self.publish_state == PublicationState.PRIVATE and not self.is_owned_by(ctx.author): raise NotAllowed("This collection is private.") # generate default bindings alias_bindings = await self._generate_default_alias_bindings(ctx) snippet_bindings = await self._generate_default_snippet_bindings(ctx) # insert subscription await self.sub_coll(ctx).insert_one( {"type": "subscribe", "subscriber_id": ctx.author.id, "object_id": self.id, "alias_bindings": alias_bindings, "snippet_bindings": snippet_bindings} ) # increase subscription count await ctx.bot.mdb.workshop_collections.update_one( {"_id": self.id}, {"$inc": {"num_subscribers": 1}} ) # log subscribe event await ctx.bot.mdb.analytics_alias_events.insert_one( {"type": "subscribe", "object_id": self.id, "timestamp": datetime.datetime.utcnow(), "user_id": ctx.author.id} )
async def _servsnippet_before_edit(ctx, name=None, delete=False): if not _can_edit_servaliases(ctx): raise NotAllowed( "You do not have permission to edit server snippets. Either __Administrator__ " "Discord permissions or a role named \"Server Aliaser\" or \"Dragonspeaker\" " "is required.") await _snippet_before_edit(ctx, name, delete)
async def toggle_server_active(self, ctx): """ Toggles whether the bestiary should be active on the contextual server. :param ctx: Context :return: Whether the bestiary is now active on the server. """ guild_id = str(ctx.guild.id) sub_doc = {"guild_id": guild_id, "subscriber_id": str(ctx.author.id)} if sub_doc in self.server_active: # I subscribed and want to unsubscribe await ctx.bot.mdb.bestiaries.update_one( {"_id": self.id}, {"$pull": { "server_active": sub_doc }}) self.server_active.remove(sub_doc) elif guild_id in map( lambda s: s['guild_id'], self.server_active ): # someone else has already served this bestiary raise NotAllowed( "Another user is already sharing this bestiary with the server!" ) else: # no one has served this bestiary and I want to await ctx.bot.mdb.bestiaries.update_one( {"_id": self.id}, {"$push": { "server_active": sub_doc }}) self.server_active.append(sub_doc) return sub_doc in self.server_active
async def unsubscribe(self, ctx): """Removes the contextual author from subscribers.""" if not await self.is_subscribed(ctx): raise NotAllowed("You are not subscribed to this.") await self.sub_coll(ctx).delete_many( {"subscriber_id": ctx.author.id, "object_id": self.id} # unsubscribe, unactive, uneditor )
async def subscribe(self, ctx): """Adds the contextual author as a subscriber.""" if await self.is_subscribed(ctx): raise NotAllowed("You are already subscribed to this.") await self.sub_coll(ctx).insert_one( {"type": "subscribe", "subscriber_id": ctx.author.id, "object_id": self.id} )
async def update_gvar(ctx, gid, value): value = str(value) gvar = await ctx.bot.mdb.gvars.find_one({"key": gid}) if gvar is None: raise InvalidArgument("Global variable not found.") elif gvar['owner'] != str(ctx.author.id) and not str(ctx.author.id) in gvar.get('editors', []): raise NotAllowed("You are not allowed to edit this variable.") elif len(value) > GVAR_SIZE_LIMIT: raise InvalidArgument(f"Gvars must be shorter than {GVAR_SIZE_LIMIT} characters.") await ctx.bot.mdb.gvars.update_one({"key": gid}, {"$set": {"value": value}})
async def set_svar(ctx, name, value): if ctx.guild is None: raise NotAllowed("You cannot set a svar in a private message.") value = str(value) if not name.isidentifier(): raise InvalidArgument("Svar names must be valid identifiers " "(only contain a-z, A-Z, 0-9, _, and not start with a number).") elif len(value) > SVAR_SIZE_LIMIT: raise InvalidArgument(f"Svars must be shorter than {SVAR_SIZE_LIMIT} characters.") await ctx.bot.mdb.svars.update_one( {"owner": ctx.guild.id, "name": name}, {"$set": {"value": value}}, True)
async def unset_server_active(self, ctx): if not await self.is_server_active(ctx): raise NotAllowed("This collection is not installed on this server.") # remove sub doc await super().unset_server_active(ctx) # decr sub count await ctx.bot.mdb.workshop_collections.update_one( {"_id": self.id}, {"$inc": {"num_guild_subscribers": -1}} ) # log unsub event await ctx.bot.mdb.analytics_alias_events.insert_one( {"type": "server_unsubscribe", "object_id": self.id, "timestamp": datetime.datetime.utcnow(), "user_id": ctx.author.id} )