Retrieving Author of the Message

Some time it's useful to get the author of a message.

For those cases Discapp provides you a quick way of retrieving the author of the message that invoked the command.

In this chapter we will improve the PokeCommand from the Mention Arguments section.

Improving PokeCommand.ts#

path/to/your/app/src/Commands/PokeCommand.ts
import {
Command,
BaseCommand,
Argument,
Mention,
Author,
UserContract,
} from "discapp";
@Command()
export class PokeCommand extends BaseCommand {
@Argument()
public user: Mention;
@Author()
public author: UserContract;
public execute() {
this.user.send("๐Ÿ‘‰");
return `${this.author.username} just poked ${this.user.username}`;
}
}

For getting the author of the message, just declare a property and mark it with the @Author() decorator.

Testing PokeCommand#

When sending:

!poke @(you)

You should receive ๐Ÿ‘‰ and the bot should send (your username) just poked (your username) in the server.