question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access the `@Discord()` class *instance* from decorator callback functions

See original GitHub issue

Feature?

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:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Satontcommented, Jan 3, 2022

Thanks for the quick reply. However I don’t think this is right. The type for the autocomplete option is defined here, and none of the arguments is the parent object:

https://github.com/oceanroleplay/discord.ts/blob/7238b63eed9dcf300939f47099cdf45cdaae0269/src/decorators/params/SlashOptionParams.ts#L54-L60

Also when I try to debug their values, I get:

    @Slash("foo")
    foo(
        @SlashOption("opt1", {
            type: "STRING",
            // @ts-ignore
            autocomplete: function(a, b, c) {
                console.log(a);
                console.log(b);
                console.log(c);
            }
        })
        opt1: string,
    ){ }
AutocompleteInteraction { ... }
DApplicationCommand { ... }
undefined

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.

0reactions
multimericcommented, Jan 3, 2022

Here’s a fully working example, by the way:

import {DApplicationCommand, Slash, SlashOption, Discord} from "discordx";
import {AutocompleteInteraction} from "discord.js";

@Discord()
class Foo {
    state: string[]

    constructor() {
        this.state = [
            "a",
            "b"
        ]
    }

    @Slash("foo")
    foo(
        @SlashOption("opt1", {
            type: "STRING",
            autocomplete: async function(this: Foo, interaction: AutocompleteInteraction, cmd: DApplicationCommand) {
                await interaction.respond(this.state.map(item => ({name: item, value: item})));
            }
        })
        opt1: string,
    ){ }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found