async def test_button(app: CommandHandler, ctx: InvokeContext): """ A button that sends a test POST request to the webhook with a test message. If The request fails the error handler takes care of the responses otherwise it responds as a sucess. """ try: channel_id = ctx["channel_id"] webhook_url = ctx["webhook_url"] except KeyError: return Response(content=( f"Oops! Something has gone awfully wrong in the (events:test_button:KeyError) handler, " f"please contact the developer @ {SUPPORT_SERVER_URL}")) await app.http.request( "POST", f"{webhook_url}?wait=true", pass_token=False, json={"content": "🚀 Testing, testing, I think we're ready!"}, ) return Response( content=f"I've sent a test message! If it appears in <#{channel_id}>", flags=ResponseFlags.EPHEMERAL, )
async def add_release_channel( app: CommandHandler, interaction: Interaction, channel: Channel = Option(channel_types=[ChannelType.GUILD_TEXT]), ) -> Response: url = await get_webhook_url( app=app, channel=channel, sub_type=EventType.Releases, ) await submit_webhook( app=app, sub_type=EventType.Releases, guild_id=interaction.guild_id, webhook_url=url, ) return Response( content=(f"<:exitment:717784139641651211> All done! I'll send to " f"<#{channel.id}> when a new Anime episode is out!"), flags=ResponseFlags.EPHEMERAL, components=[[test_button]], component_context={ "channel_id": channel.id, "webhook_url": url }, )
async def select_other_results(ctx: InvokeContext, index: str): """ Allows the user to select a list of embeds and rendering that result. Args: ctx: The invoke context to pass the embeds and select_options from the parent. index: The index of the embed to render. This comes as a string but is converted to a int. """ index = int(index) embeds: List[EntityAndEmbed] = ctx["embeds"] select_options = ctx["select_options"] option = select_options.pop(index) select_options.insert(0, option) return Response( embed=embeds[index][1], components=[ select_other_results.with_options(select_options), ], component_context={ "ttl": timedelta(minutes=2), }, )
async def search_anime_from_message( app: CommandHandler, interaction: Interaction, message: PartialMessage, ): embeds = await get_best_manga_results(app=app, interaction=interaction, query=message.content) select_options = [ SelectOption(label=title, value=str(i), default=i == 0) for i, (title, _) in enumerate(embeds) ] return Response( embed=embeds[0][1], components=[ select_other_results.with_options(select_options), ], component_context={ "embeds": embeds, "select_options": select_options, "ttl": timedelta(minutes=2), }, )
async def on_button_error(_: Interaction, e: Exception): if isinstance(e, NotFound): return Response( content= ("<:HimeSad:676087829557936149> Weird!? This webhook seems to have " "been delete between me creating and you clicking the test button!" ), flags=ResponseFlags.EPHEMERAL, ) if isinstance(e, DiscordServerError): return Response( content= ("<:HimeSad:676087829557936149> Oh no! Discord seem to be having some " "issues right now, this doesn't mean your event feed hasn't been " "added it just means we aren't able to test it right now."), flags=ResponseFlags.EPHEMERAL, )
async def on_command_error(_: Interaction, e: Exception): if isinstance(e, Forbidden): return Response( content= ("<:HimeSad:676087829557936149> Oops! Looks like I'm missing permissions " "to do this. Make sure I have the permission `MANAGE_WEBHOOKS` and try again." ), flags=ResponseFlags.EPHEMERAL, ) raise e
async def search_manga( app: CommandHandler, interaction: Interaction, query: str = Option( description="Search for the Manga you want here.", autocomplete=True, ), ): try: data = await app.client.request( "GET", f"/data/manga/{query}", ) except CrunchyApiHTTPException as e: if e.status_code == 404: return Response( content="Oops! I couldn't find anything for that query!") raise e _, embed = make_manga_embed(interaction, data['data']) return Response(embed=embed)
async def get_tracking( app: CommandHandler, interaction: Interaction, group: str = Option( description="Start typing the name of the list.", autocomplete=True, ), ): if group == "__NO_OP": return Response( content=("Oops! You dont have any tracking lists, use the " "'/create-group' command to get started."), flags=ResponseFlags.EPHEMERAL, ) if interaction.member is not None: user_id = interaction.member.user.id else: user_id = interaction.user.id response = await app.client.request("GET", f"/tracking/{user_id}/{group}") data = response["data"]