Retrieving the Guild of the Message

Sometimes it's usefull to have the guild of where the message was sent.

For demonstrating how you can do this using Discapp we will code the AboutCommand that will display information about your server.

The AboutCommand#

path/to/your/app/src/Commands/AboutCommand.ts
import { BaseCommand, Command, Guild, GuildContract } from "discapp";
import { MessageEmbed } from "discord.js";
@Command({
isGuildOnly: true,
})
export class AboutCommand extends BaseCommand {
@Guild()
public guild: GuildContract;
public execute() {
return new MessageEmbed()
.setTitle(this.guild.name)
.setDescription(
this.guild.description || "This server has no description "
)
.addField("Created at", this.guild.createdAt.toLocaleDateString())
.setThumbnail(this.guild.iconURL());
}
}

Couple of things to notice:

  • The Guild will only be avaible if the command was executed in a guild context, otherwise it'll be null.
  • If your command really needs the guild, then mark then set isGuildOnly to true.

Testing the AboutCommand#

You can test the AboutCommand by sending:

!about

Your bot should respond with a message with the title, description, icon and creation date of your server.