Skip to content

Commit

Permalink
my inatyear: Suffix URL with id to bust the Discord unfurl cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
synrg committed Dec 8, 2024
1 parent 0f522ba commit 08ffb50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions inatcog/commands/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ..embeds.inat import INatEmbeds
from ..interfaces import MixinMeta
from ..projects import UserProject
from ..utils import get_valid_user_config
from ..utils import cache_busting_id, get_valid_user_config


class CommandsUser(INatEmbeds, MixinMeta):
Expand Down Expand Up @@ -1061,7 +1061,7 @@ async def user_inatyear(self, ctx, user: str = "me", year: int = None):
return

await ctx.send(
f"https://www.inaturalist.org/stats/{stats_year}/{inat_user.login}"
f"https://www.inaturalist.org/stats/{stats_year}/{inat_user.login}?{cache_busting_id()}"
)

@commands.command()
Expand Down
17 changes: 17 additions & 0 deletions inatcog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
from contextlib import asynccontextmanager
import functools
from time import time
from typing import Optional, Union

import discord
Expand All @@ -19,6 +20,22 @@
COG_HAS_USER_DEFAULTS = ["home"]


def cache_busting_id():
"""A unique id suitable to bust Discord's cache of a URL unfurl.
Append this string to any URL that Discord previews as an embed (an "unfurl" in
Discord parlance) to bust the cache.
The Discord URL unfurl is reported here to be cached "up to 30 minutes":
- https://github.com/discord/discord-api-docs/issues/1663#issuecomment-632970964
Since that would be 4 digits, we're generous and generate IDs from 0 through 9999,
so should work to bust their cache for roughly 2.78 hours.
"""
return str(int(time() % 10000)).zfill(4)


def use_client(coro_or_command):
is_command = isinstance(coro_or_command, commands.Command)
if not is_command and not asyncio.iscoroutinefunction(coro_or_command):
Expand Down

0 comments on commit 08ffb50

Please sign in to comment.