async def select_bestiary(ctx, name): user_bestiaries = await ctx.bot.mdb.bestiaries.find({ "owner": str(ctx.author.id) }).to_list(None) if not user_bestiaries: raise NoActiveBrew() choices = [] for bestiary in user_bestiaries: url = bestiary['critterdb_id'] if bestiary['name'].lower() == name.lower(): choices.append((bestiary, url)) elif name.lower() in bestiary['name'].lower(): choices.append((bestiary, url)) if len(choices) > 1: choiceList = [(f"{c[0]['name']} (`{c[1]})`", c) for c in choices] result = await get_selection(ctx, choiceList, delete=True) if result is None: raise SelectionCancelled() bestiary = result[0] bestiary_url = result[1] elif len(choices) == 0: raise NoSelectionElements() else: bestiary = choices[0][0] bestiary_url = choices[0][1] return Bestiary.from_raw(bestiary_url, bestiary)
async def select(cls, ctx, name): """Searches and selects from all objects visible to a user.""" available_names = [n async for n in cls.user_visible(ctx, meta_only=True)] if not available_names: raise NoActiveBrew() result = await search_and_select(ctx, available_names, name, lambda p: p['name']) return await cls.from_id(ctx, result['_id'])
async def select_bestiary(ctx, name): user_bestiaries = [] async for b in Bestiary.user_bestiaries(ctx): user_bestiaries.append(b) if not user_bestiaries: raise NoActiveBrew() bestiary = await search_and_select(ctx, user_bestiaries, name, key=lambda b: b.name, selectkey=lambda b: f"{b.name} (`{b.upstream})`") return bestiary
async def from_ctx(cls, ctx): active_bestiary = await ctx.bot.mdb.bestiaries.find_one({ "owner": str(ctx.author.id), "active": True }) if active_bestiary is None: raise NoActiveBrew() return cls.from_raw(active_bestiary['critterdb_id'], active_bestiary)
async def select_pack(ctx, name): available_pack_names = await ctx.bot.mdb.packs.find( Pack.view_query(str(ctx.author.id)), ['name', '_id']).to_list(None) if not available_pack_names: raise NoActiveBrew() result = await search_and_select(ctx, available_pack_names, name, lambda p: p['name']) final_pack = await ctx.bot.mdb.packs.find_one({"_id": result['_id']}) return Pack.from_dict(final_pack)
async def from_id(cls, ctx, _id, meta_only=False): if not isinstance(_id, ObjectId): _id = ObjectId(_id) if meta_only: obj = await cls.data_coll(ctx).find_one({"_id": _id}, ['_id', 'name', 'owner', 'public']) else: obj = await cls.data_coll(ctx).find_one({"_id": _id}) if obj is None: raise NoActiveBrew() if not meta_only: return cls.from_dict(obj) return obj
async def from_ctx(cls, ctx): active_id = await cls.active_id(ctx) if active_id is None: raise NoActiveBrew() return await cls.from_id(ctx, active_id)
async def from_ctx(cls, ctx): active_tome = await ctx.bot.mdb.tomes.find_one({"active": str(ctx.author.id)}) if active_tome is None: raise NoActiveBrew() return cls.from_dict(active_tome)
async def from_id(cls, ctx, tome_id): tome = await ctx.bot.mdb.tomes.find_one({"_id": ObjectId(tome_id)}) if tome is None: raise NoActiveBrew() return cls.from_dict(tome)
async def from_id(cls, ctx, pack_id): pack = await ctx.bot.mdb.packs.find_one({"_id": ObjectId(pack_id)}) if pack is None: raise NoActiveBrew() return cls.from_dict(pack)
async def from_ctx(cls, ctx): active_bestiary = await ctx.bot.mdb.bestiaries.find_one( {"active": str(ctx.author.id)}, projection={"monsters": False}) if active_bestiary is None: raise NoActiveBrew() return cls.from_dict(active_bestiary)