Skip to content

Command Basics

paige edited this page Aug 28, 2023 · 4 revisions

Commands are stored inside the WC instance and they're a lot simpler to set up compared to Discord.JS and have a variety of features like cooldowns, arguments, aliases, etc

Creating Prefix Commands

Prefix commands have a pretty basic setup only requiring a name to be created although they have optional aliases and cooldowns.

In this example let's make a prefix command called "ping" then tell it what to do when it's run
Let's say that you made your prefix "!" if someone were to say "!ping" in chat the bot would reply with "pong!"

wc.command("ping", async (ctx) => {
	ctx.reply("pong!");
});

Now let's compare that to Discord.JS prefix commands:

// discord.js
client.on("messageCreate", async (ctx) => {
	if (!ctx.content.startsWith(prefix) || ctx.content.endsWith(prefix) && ctx.content.startsWith(prefix)) return;

	if (ctx.content.split(" ")[0].toLowerCase() == `${prefix}ping`) {
		ctx.reply("pong!");
	}
});

(If you ever need it the way to access commands is through wc.commandList)

Command Returns

When a command is run it returns ctx (message context) and cmd (command info)

  • ctx is the context of the command's message/interaction including things like the author, guild, channel, etc
  • cmd is the command's info such as name, aliases, arguments, cooldown, etc

Using Arguments

Arguments are an array of things given after the command
For this example let's make a !say command, if the message is "!say arguments are a thing!" you can use that in your command like this:

wc.command("say", async (ctx, cmd) => {
	console.log(cmd.args[0]); // "arguments"
	console.log(cmd.args[1]); // "are"
	console.log(cmd.args[cmd.args.length-1]); // "thing!"

	ctx.channel.send(cmd.args.join(" ")); // sends "arguments are a thing!"
});

Adding Aliases (prefix only)

Aliases are alternate names for the commands
For this example let's make an avatar command with the aliases as ["av"] when someone says "!av" in chat it should run the command

wc.command( {name: "avatar", aliases: ["av"]}, async (ctx, cmd) => {
    // if there is no argument it defaults to the message author
    let user = await wc.fetchUser(cmd.args[0]);

    return ctx.reply(wc.user.avatar(user));
});

Adding Cooldowns

Cooldowns are the time that it takes for a user to use a command again mostly used for reducing spamming
Let's take the last command and add a 1 second cooldown

wc.command( {name: "avatar", aliases: ["av"], cooldown: "1s"}, async (ctx, cmd) => {
    if (cmd.onCooldown) return wc.reply("Command is on cooldown!", {deleteAfter: "1s"});
    
    // if there is no argument it defaults to the message author
    let user = await wc.fetchUser(cmd.args[0]);

    return ctx.reply(wc.user.avatar(user));
});

for more info on things like time you can check out the page on time here

Clone this wiki locally