Skip to content

rework for api v2#76

Open
gregdan3 wants to merge 19 commits into
lipu-linku:mainfrom
Theooolone:main
Open

rework for api v2#76
gregdan3 wants to merge 19 commits into
lipu-linku:mainfrom
Theooolone:main

Conversation

@gregdan3

Copy link
Copy Markdown
Member

No description provided.

Theooolone added 2 commits May 9, 2026 19:18
Words are stored and accessed differently, in order to make future
changes in the API easier to manage:
- Now using the V2 API instead of the deprecated V1 API.
- Words now use the "Words" class instead of a typed dictionary.
- Individual translations are now only downloaded from the API when
  needed, as the V2 API doesn't support requesting every translation at
  once.
- The etymology section of `/nimi` has been modified slightly due to
  API changes.

Only surface level bug testing has been done, there may still be some
bugs introduced by these changes that I haven't yet found
Comment thread src/ilo/cogs/nimi/cog.py Outdated
Comment thread src/ilo/cogs/nimi/cog.py Outdated
Comment thread src/ilo/__main__.py Outdated
Comment thread src/ilo/data.py Outdated
Comment thread src/ilo/data.py
_FETCHED_LANGS: list[str] = []

async def fetch_lang_and_defer(lang: str, ctx: discord.Interaction, ephemeral = False):
"""Does the same thing as fetch_lang(), but defers an interaction if the translation has not already been downloaded. Use this in commands"""

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why leave this choice up to the user?
you can have fetch_lang alone, and always defer if necessary
is there a circumstance in which the dev would want to fetch lang and not defer if necessary?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defer requires running inside of an async function, I didn't want fetch_lang to be async when async behaviour was not needed in many instances.
fetch_lang is used in get_word, get_non_sandbox_words, get_sandbox_words, get_words, word.get_commentary, word.get_definition, and word.get_etymology. I'd rather have these functions synchronous, it doesn't make sense to have them be async because of a feature of a function that these functions never use. So I separated out the async behaviour into a separate function.
Deferring every time fetch_lang is called would mean commands defer many times, and the interaction context would need to be passed down through many functions. It is simpler to just call fetch_lang_and_defer at the start of a command.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • deferring is completely free, since discord will consume the deferral as soon as the command returns (which may be immediate)
  • async is also completely free
  • all of these commands potentially fetch data via an API anyway, so them being asynchronous is beneficial since other things can be done while waiting out the network fetch. and, well, most of Linku is async anyway because all operations on the discord api are async

Comment thread src/ilo/data.py
"""Unsafe to call without previously having fetched the language using fetch_lang() or fetch_lang_and_defer()"""
return word if (word := get_word(word_str, lang)) and word.usage_category == "sandbox" else None

def get_word(word_str: str, lang: str = "en") -> Optional[Word]:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than have a safe/unsafe distinction, you can have a function like ensure_lang(lang) which is always run, and it fetches the lang + defers if necessary within itself

@Theooolone Theooolone May 15, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments are outdated, get_word fetches the lang using fetch_lang, so it is safe. the ensure_lang function you're describing is the behaviour of fetch_lang_and_defer.

Comment thread src/ilo/word.py Outdated
@@ -0,0 +1,58 @@
from typing import Required, Optional, TypedDict

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of duplicating this work, use this tool to regenerate the sona data for v2, then use that as a reference

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants