Access the `@Discord()` class *instance* from decorator callback functions
See original GitHub issueFeature?
Problem
Currently there are some callbacks that are defined in the annotator hooks, for example @SlashOption("foo", { autocomplete })
.
However, this only passes to the callback:
- The interaction (
AutoCompleteInteraction
class instance) - The application (
DApplicationCommand
class instance)
We also have MetadataStorage.instance
.
However, none of these keeps a handle to the instance of the class that has the @Discord()
annotation. This means that we can’t access the application state stored in the class instance, when we are doing things like generating the autocomplete list. I assume each of these classes are singletons, since we only ever need to construct 1 of each, so this should be possible.
Example
import {DApplicationCommand, Slash, SlashOption} from "discordx";
import {AutocompleteInteraction} from "discord.js";
class Foo{
state: string[]
constructor() {
this.state = [
"a",
"b"
]
}
@Slash("foo")
foo(
@SlashOption("opt1", {
type: "STRING",
autocomplete: (interaction: AutocompleteInteraction, cmd: DApplicationCommand) => {
return this.state.map(item => ({name: item, value: item}));
}
})
opt1: string,
){ }
}
Of course this doesn’t actually work, because this
isn’t actually set to the Foo
instance.
Package
discordx
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Can a decorator of an instance method access the class?
The method decorator marks the method as one that is of interest by adding a "use_class" attribute - functions and methods are also...
Read more >Commands - discord.py
Commands are defined by attaching it to a regular Python function. ... The first one is by using Bot.command() decorator, as seen in...
Read more >Interactions - Discord
An Interaction is the message that your application receives when a user uses an application command or a message component. For Slash Commands,...
Read more >Decorators - Ember RFCs
Decorators provide a way to abstract functionality and improve the developer experience of working with native classes. This RFC outlines the ...
Read more >discord.ext.commands.core - Pycord v2.3 Documentation
__wrapped__ elif isinstance(function, partial): function = function.func else: ... name: :class:`str` The name of the command. callback: :ref:`coroutine ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You should name first arg of function as
this
, then it will be inherited.And yes, without it it also should work, it’s just for typings in docs, maybe? I’m not sure.
Here’s a fully working example, by the way: