async def delete( self, inter, poll_id: int = Param(description="ID of the poll to delete")): """Deletes a poll""" poll = (self.bot.s.query(Poll).filter( Poll.id == poll_id, Poll.guild == inter.guild.id).first()) if not poll: await inter.response.send_message( "No poll associated with provided ID") else: if poll == self.get_current_poll(inter): del self.polls[inter.guild.id] self.bot.s.delete(poll) self.bot.s.commit() await inter.response.send_message("Poll deleted successfully")
async def activate( self, inter, poll_id: int = Param(description="ID of the poll to activate")): """Activates a poll""" poll = (self.bot.s.query(Poll).filter( Poll.guild == inter.guild.id, Poll.id == poll_id).one_or_none()) if poll is None: return await inter.response.send_message( "No poll with the provided id") current_poll = self.get_current_poll(inter) if self.get_current_poll(inter) is not None: current_poll.active = False poll.active = True self.bot.s.commit() self.logger.info(f"Enabled poll {poll.name}") await inter.response.send_message(f"Enabled poll {poll.name}") self.polls[inter.guild.id] = poll
async def hoyo_auth_set_cookies( self, inter: Interaction, name: str = Param(desc="How you want your account to be displayed."), game: str = Param(choices=["Honkai Impact", "Genshin Impact"]), ltuid: str = Param( "", desc="If selected, please make sure to also use LTOKEN.", ), ltoken: str = Param( "", desc="If selected, please make sure to also use LTUID.", ), account_id: str = Param( "", desc="If selected, please make sure to also use COOKIE_TOKEN.", ), cookie_token: str = Param( "", desc="If selected, please make sure to also use ACCOUNT_ID.", ), ): user = disnake.utils.get(self.user_cache, discord_id=inter.author.id) inter.send if not any([ltuid, ltoken, account_id, cookie_token]): # Assume we're just adding a game if user is None: return await inter.response.send_message( f"{inter.author.mention}, you should first setup an account. " "Please run this command again and specify your login cookies.", ephemeral=True, ) account = disnake.utils.get(user.hoyolab.accounts, name=name) if account is None: return await inter.response.send_message( f"{inter.author.message}, you do not appear to have an account with " "that name. Please pick an existing option or create an account first.", ephemeral=True, ) await self._do_account_game_update(inter, account, game) return await user.commit() try: new_cookies = CookieModel( ltuid=ltuid, ltoken=ltoken, account_id=account_id, cookie_token=cookie_token ) except ValidationError as e: # TODO: Make pydantic error parser? return await inter.response.send_message(e.errors()[0]["msg"], ephemeral=True) # TODO: Validate cookies if user is None: new_user = await DiscordUserDataModel.create_new( _id=inter.author.id, hoyolab_data={ "accounts": [{"name": name, "games": [game], "cookies": new_cookies}] }, ) self.user_cache.append(new_user) return await inter.response.send_message( f"Successfully added your account with {new_cookies} and bound it to {game}!", ephemeral=True, ) for account in user.hoyolab.accounts: matching_cookies = account.match_cookies(new_cookies) if all(matching_cookies): # All match; add new game for same cookies or return error message. await self._do_account_game_update(inter, account, game) break elif any(matching_cookies): # One or more matches; assume update existing account: account.update_cookies(new_cookies) await inter.response.send_message( f"Successfully updated your cookies to {new_cookies}", ephemeral=True ) break else: # The loop didn't break, no matching account to be updated; new account: user.hoyolab.add_new_account(new_cookies, game) await inter.response.send_message( f"Successfully added your account with {new_cookies} and bound it to {game}!", ephemeral=True, ) await user.commit()
async def search( self, inter, query: str = Param(default="", description="What to search"), member_id: str = Param(default=None, description="ID of User to search"), channel_id: str = Param(default=None, description="ID of channel to search in."), before: str = Param(default="", description="Date in yyyy-mm-dd format"), after: str = Param(default="", description="Date in yyyy-mm-dd format"), during: str = Param( default="", description= "Date in yyyy-mm-dd format. Can't be used with before and after.", ), order_by: str = Param(default="DESC", description="Show old or new messages first.", choices={ "Older first": "ASC", "New first": "DESC" }), view_state: str = Param( default="", description="If public everyone can see the output file", choices={ "Public": "", "Private": "private" }), show_mod_channels: bool = Param(default=False, description="Show mod channels"), ): """Search the server logs for messages that matches the parameters given then returns them in a file""" # Discord IDs are too long to be taken as integer input from a slash command. try: if member_id: member_id = int(member_id) if channel_id: channel_id = int(channel_id) except ValueError: inter.response.send_message("Invalid input for ID") return await inter.response.defer(ephemeral=bool(view_state)) stmt = self.build_query(query, member_id, channel_id, before, after, during, order_by, show_mod_channels) if not str(stmt): await inter.edit_original_message(content="Invalid search.") with self.engine.connect() as connection: result = connection.execute(stmt).fetchall() txt = "\n".join( f"[{cname}] [{date.strftime('%m/%d/%Y %H:%M:%S')}] <{user} {content}>" for date, cname, user, content in result) if not txt: return await inter.edit_original_message( content="No messages found.") encoded = txt.encode("utf-8") if len(encoded) > inter.guild.filesize_limit: return await inter.edit_original_message( content="Result is too big!") data = io.BytesIO(encoded) file = discord.File(filename="output.txt", fp=data) await inter.edit_original_message(file=file)