Kord-Extensions/kord-extensions

Suggestion: Event cache

GreemDev opened this issue · 3 comments

Summary

The idea here is to provide a data object to an arbitrary event KordEx receives when dispatched.

Current Behaviour

None, potential workaround could entail some sort of weird map cache that holds the objects via an ID? but that ID wouldn't be very intuitive. Haven't ever tried it, just thought it might be possible when writing this.

Preferred Behaviour

New class, EventCache, for example.
EventCache is an object added to the relevant Context objects as val cache: EventCache

override suspend fun setup() {
  event<MessageReceivedEvent> {
    check {
      cache["guildSettings"] = someGettingMethod(event.guild?.id ?: 0)
      if (cache["guildSettings"] == null)
          fail("404")
      else pass()
    }
    action {
      //we can safely assume that guild settings are not null.
      if (event.message.content.startsWith(cache.get<GuildSettings>("guildSettings").commandPrefix)) {
        //now the prefix has been checked and we only called the DB once
        //yes i know you'd probably put this logic in the check but this isn't the point, this example is to show
      }
    }
  }
}

Hello, and thanks for opening an issue! As this is the first time you've created an issue on this repository, we'd just like to offer you a warm welcome to the project, and the following pointers:

  • Most importantly, all issues must adhere to our Code of Conduct. Please give it a quick read if you haven't already.

  • While our team is passionate about the projects we've created here, we're all volunteers. Please don't be offended if it takes time for us to get to your issue - we'll be here as soonas we can be!

  • Please provide as much information as possible when asking a question, reporting a problem, or submitting a feature request. This will help us to address your issue quickly and efficiently. If you forgot to add some information, no worries - feel free to edit the issue and add anything you missed!

    Thanks for contacting us! If you have any further questions, please feel free to join us on Discord in the #dev-kotdis channel (or #kordex-discussion for Kord Extensions projects), or to contact a staff member directly.

Pushed in a8eeca5

  • Checks and action blocks for commands, components and events will have a cache MutableMap with string keys and non-nullable values
  • All maps with non-nullable values now have extra .getOf, .getOfOrNull and .getOfOrDefault convenience functions for casting

See what you think!

Looks good to me. Thanks!