#Converting argument `interaction` to `this`

53 messages · Page 1 of 1 (latest)

dense bridge
#

This sounds weird, but i have tried couple things to convert

run(interaction: ChatInputCommandInteraction) to already making it so the class inherits run() and then i can perform actions like this.reply() instead of interaction.reply()

Node: v16.15.1
D.js: v14.3.0

pine turtleBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

stable nacelle
#

but why

#

You can use function.bind to alter the this context

dense bridge
#

What do you mean by function.bind

#

Any code example by any chance?

#

or like links to?

pine turtleBOT
#

mdn Function.prototype.bind()
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

dense bridge
#

I did as followed, but now inside the command class typescript shows this when accessing properties of this but it console logs a different Class

#

to do .bind({ ...interaction, client: this.client}) for example?

#

Ok it worked but the intellisense doesn't pick it up

#

This is how the code looks like so far

desert nova
# dense bridge Ok it worked but the intellisense doesn't pick it up

Typescript hates this kind of things, messing with bind and prototype directly is not smtg that will make typescript happy.
I would highly recommend following othet approach or if you rly want to do it like this use some type casting or change your types completly, and you will have to narrow down the types to make sure you have what you want.
I would strongly recommend following another approach

dense bridge
desert nova
#

And what was the issue?

dense bridge
#

When initializing the class within the command handling

desert nova
#

Does djs even expose the raw api data to the user?

dense bridge
#

I do not know, i even tried going through the source code of djs to maybe reverse engineer it and somehow do it, but it doesn't work

desert nova
#

I dont remember raw api data being exposed to the user

#

If you want to create a class based of another one there are ways of doing it that do not include bind but they are a little messy to setup properly in a way that you get intellisense

dense bridge
desert nova
#

Implementing it wont solve the issue because it means you need to implement the methods yourself

dense bridge
desert nova
#

Exactly

dense bridge
#

If i console log this everything is there, it's just typescript thinks something is wrong

desert nova
#

An interface is a ts thing

dense bridge
#

so if i try SlashCommand extends Command implements ISlashCommand

#

it can work

desert nova
#

If it doesnt recognize they are there it will ask you to define them or else it wont compile

dense bridge
desert nova
#

You can try but im pretty sure it wont compile and will scream at you

#

Again, using bind is not smtg that should be done

dense bridge
#

I will try it and let you know the update, thank you for suggestions tho

dense bridge
desert nova
dense bridge
#

im ded 💀

#

Idk what to do at this point

#

I've seen people do it

desert nova
#

First, why do you want to merge the interaction methods on your command class?

#

Thats just a memory hog and not efficient at all, + is way less mantainable and a pain to read

dense bridge
desert nova
#

You are introducing a lot of problems to deal with just for no reason

dense bridge
#

Why am i introducing a lot of problems besides this situation?

desert nova
#

First, if you have 2 methods with the same name one will be deleted.
Second, you are adding more processing to do on every command call
Third, linked with the second, you are consuming more memory doing this for no apparent reason
Fourth, is just against best practices

#

There is more to it but i cant be bothered to go through everything. Just read about bind problems and you will understand

#

But its your code, if you rly want to do it there are ways if doing it and having intellisense

dense bridge
#

I didn’t look at it that way

#

Fair points