Scope of Command Classes?

Hi just curious, what's the scope of command classes? Is a class created each time you run the command, or
export class ConfigCommand extends Command {
supportChannel: Channel | null = null;
settingsEmbed: EmbedBuilder | null = null;
permittedRoles: string[] = [];
export class ConfigCommand extends Command {
supportChannel: Channel | null = null;
settingsEmbed: EmbedBuilder | null = null;
permittedRoles: string[] = [];
when a guild user runs this command, is the support channel per-command instance or per bot?
Solution:
An instance is created before the bot logs in and afterwards that same instance is always referenced. Thank god because otherwise perf would be dogshit
Jump to solution
44 Replies
Solution
Favna
Favna3mo ago
An instance is created before the bot logs in and afterwards that same instance is always referenced. Thank god because otherwise perf would be dogshit
Favna
Favna3mo ago
The method on a command is called on every run
ZachHandley
ZachHandley3mo ago
I figured yeah, in that case, what's the best way to deal with having data between command and interactions? e.g. in my config command they set up which channel is for support, roles, etc. and I could keep it in my DB the whole time and just update it with interactions if I need to I could create a class with the embed and data maybe and just make a new instance per command keyed by the user's ID or guild's ID
Favna
Favna3mo ago
If you can fit it down to 80 or so string characters then customId field. Otherwise some temporary storage like an in memory Map or a Redis database with keys being a combination of commandId-guildId-interactionId or such and values whatever you want to pass.
ZachHandley
ZachHandley3mo ago
ah damn okay, have you guys considered adding any kind of storage or state management to SapphireJs? Nanostores probably works with it I'd imagine?
Favna
Favna3mo ago
Regarding the former I do this for @Dragonite https://github.com/favware/dragonite/blob/main/src/lib/util/utils.ts
GitHub
dragonite/src/lib/util/utils.ts at main · favware/dragonite
A Pokémon information Discord bot built around Discord Interactions - favware/dragonite
Favna
Favna3mo ago
Funny you mention it because @kyra 🩵🩷🤍🩷🩵 has abandoned it twice 1. https://github.com/sapphiredev/utilities/pull/118 2. https://github.com/sapphiredev/utilities/pull/663
GitHub
feat: added string-store by kyranet · Pull Request #118 · sapphired...
TODO Dynamically increase size on size-unknown payloads. Add tests, lots of tests Does this thing even work? Endianness is a pain.
GitHub
feat(bit-array): initial commit by kyranet · Pull Request #663 · sa...
This is a dependency for @sapphire/string-store's unaligned (and more compact) data storage, based on TypedArray's API, but using bits instead of bytes. Pending to add the missing code, and...
ZachHandley
ZachHandley3mo ago
I like that, makes sense, yeah I just think like I was using DiscordX
Favna
Favna3mo ago
I don't know nanostores so I cant answer that but it's probably unnecessary overhead. Just so export myMap = new Map() somewhere.
ZachHandley
ZachHandley3mo ago
and a large part of why it was useful was because even though the commands were static, the interactions and stuff were in the same class, though I hated their docs and everything else about it so the separation of concerns is dope, but also it makes it harder to pass info because everything is so separated, so a storage seems like a natural way to pass things together plus in theory wouldn't stores with Nanostores or something reactive be the best idea so you can just subscribe to the value?
Favna
Favna3mo ago
Overhead if you ask me like I said Just get and set map entries
ZachHandley
ZachHandley3mo ago
oh also are there better docs for the decorators?
Favna
Favna3mo ago
Never overcomplicate what can be done easily is my take
ZachHandley
ZachHandley3mo ago
yeah fair enough I like great systems 😄 hence why I like Sapphire
Favna
Favna3mo ago
Better than what exactly?
ZachHandley
ZachHandley3mo ago
better than the API ref docs I found cause I use the CLI to generate the classes but the generated don't match the docs
Favna
Favna3mo ago
Not really no but there isn't much to them either
ZachHandley
ZachHandley3mo ago
as far as I can tell the ApplyOptions == constructor basically
Favna
Favna3mo ago
https://sapphirejs.dev/docs/Documentation/api-utilities/modules/sapphire_decorators#applyoptions There are examples. We can add more TSDoc comments if needed but I would think this suffices
Sapphire Framework
Module: @sapphire/decorators | Sapphire
Enumerations
Favna
Favna3mo ago
Yes (If your constructor only sets options)
ZachHandley
ZachHandley3mo ago
Adding a function parameter to the applyOptions could remove even more and allow customizing the constructor easily, but also aren't there different options for commands vs listener vs events etc.? oh I see it's typed in the options obvs either way, my comments are not indicative of my overall opinion, I really appreciate the framework guys ❤️
Favna
Favna3mo ago
Adding a function parameter to the applyOptions could remove even more and allow customizing the constructor easily
Not possible with decorators. Especially not with ES spec ones in the future
but also aren't there different options for commands vs listener vs events etc.?
That's what the generic type is for
ZachHandley
ZachHandley3mo ago
first Discord TypeScript framework I actually like
Favna
Favna3mo ago
Not possible as in, not possible in JavaScript
ZachHandley
ZachHandley3mo ago
makes sense, you could have a boolean enable it and then check for an overridden function? but at that point I guess why not just have the users make their own
Favna
Favna3mo ago
Constructor? Also not possible. Decorators are very restrictive.
Favna
Favna3mo ago
https://github.com/sapphiredev/utilities/blob/main/packages/decorators/src/piece-decorators.ts#L52-L62 this is all that you can do and nothing more. As in, create a new instance and pass some options to it.
GitHub
utilities/packages/decorators/src/piece-decorators.ts at main · sap...
Common JavaScript utilities for Sapphire Projects. Contribute to sapphiredev/utilities development by creating an account on GitHub.
ZachHandley
ZachHandley3mo ago
so something like this is irrelevant?
No description
ZachHandley
ZachHandley3mo ago
genuinely asking, haven't messed with decorators beyond this
Favna
Favna3mo ago
Maybe the ES decorator spec will get more features in the future but that'll be many years
ZachHandley
ZachHandley3mo ago
yeah fair I did really like what DiscordX did with their decorators for buttons and stuff
Favna
Favna3mo ago
Those are method decorators not class decorators. They're completely separate spec. Sapphire exports a few of those too.
ZachHandley
ZachHandley3mo ago
they did @ButtonInteraction({ id: "someCustomId" }) with your function handler below it
Favna
Favna3mo ago
The reason we don't have many is because sapphire aims to be both JS and TS which is also why the decorators are in a separate package
ZachHandley
ZachHandley3mo ago
ah understood makes sense, I guess I feel that with Discord.js your real target is TS but I understand
Favna
Favna3mo ago
Once decorators are in Node LTS spec so TS doesn't need to output tslib.__decorate code then we can move them to the core. But God knows when that happens
ZachHandley
ZachHandley3mo ago
copy that, makes sense
Favna
Favna3mo ago
Probably not before Node 26... Or something like that
ZachHandley
ZachHandley3mo ago
I am working on a discord bot that has privacy permissions and stuff and indexes questions for AI support to provide first round support for common questions my job is in AI, so I can do that part incredibly well, do you think it's something I should try to just give to answer overflow? cause I'm basically doing the same thing just for a different reason it worked perfectly in python but single threaded killed me on it so had to switch the bot to TS
Favna
Favna3mo ago
I can't really vouch for or against @Answer Overflow. It's great for marking answers but I'm not sure how much functionality we get out of it beyond that. I have no idea how many,if anyone, uses the dashboard to search for answers before asking their question. Or search other forum posts for that matter. I know @Rhys intends to add more features, for example he mentioned using AI to give possible solutions to people once, however he also mentioned that wouldn't be a free feature and I don't know if we can afford paying for premium bot features because we get basically no funding.
ZachHandley
ZachHandley3mo ago
I mean, so I have a few open source communities I plan on funding
Favna
Favna3mo ago
FYI JavaScript is ultimately still single threaded, but the event loop (and existence of worker threads) make it seem multi threaded.
ZachHandley
ZachHandley3mo ago
for data and for my own thing I mean it's less about that and more about the fact that it's real async, and not some bullshit thing made up to make it work cause Python kept running sub-event-loops and then they wouldn't quit and everything would take 2-3 minutes for basic things
ZachHandley
ZachHandley3mo ago
thank you