Skip to content

How to Create a New Command

Evan Ugarte edited this page Dec 22, 2023 · 4 revisions

Before starting this tutorial

  • This project is cloned and set up on your computer (see the "How to use" section in the readme
  • You have an API key for the discord bot (ask dev lead or Evan for this)
  • You are a member of the "SCE-dev" Discord server (ask dev lead or Evan for this)

Organization of bot commands

All of the commands for the discord bot live inside of the src/commands/ folder, which looks like this: image

Let's create a new command inside of util/ called square. Create the file src/commands/square.js

const Command = require('../Command');

module.exports = new Command({
  name: 'square',
  description: 'Check if the bot is up',
  aliases: [],
  example: 's!square',
  permissions: 'general',
  category: 'information',
  // eslint-disable-next-line
  execute: async (message, args) => {},
});

Looking at the above file, let's go step by step:

  • Command is a helper class used to organize each command under a name and other information
  • each Command has an execute function, which is automatically called when someone runs a command (i.e. s!square)
  • the prefix of the command, most commonly s! is defined in config.json, specifically in config.example.json:4
    • you can change it to be whatever you want, for example x! and you would run the bot with <prefix><command>, i.e. x!square

lets fill out the execute function with some code to square a value from the user:

it works!

image

Challenge Time

can you try adding the below functionality:

  1. ensuring a user sent a value to square
  2. ensuring the value sent is a number

image

Clone this wiki locally