Search code examples
discord.jsbotsembed

How to embed a message in a command base


I am using WOK's advanced command handler and I just want the final message that will be returned to be an embed.

I am working on a specific test command where I type the prefix (for example !) when I run !add 5 10 I want the bot to ping me so it says @Ping , 5 + 10 = 15.

I want to solve 2 things:

  1. I don't want the comma after the ping
  2. I want the message that appears on discord to be an embed.

Here is the code for the command base:

const Discord = require('discord.js')
const { prefix } = require('../config.json')

const validatePermissions = (permissions) => {
    const validPermissions = [
        'ADMINISTRATOR',
        'CREATE_INSTANT_INVITE',
        'KICK_MEMBERS',
        'BAN_MEMBERS',
        'MANAGE_CHANNELS',
        'MANAGE_GUILD',
        'ADD_REACTIONS',
        'VIEW_AUDIT_LOG',
        'PRIORITY_SPEAKER',
        'STREAM',
        'VIEW_CHANNEL',
        'SEND_MESSAGES',
        'SEND_TTS_MESSAGES',
        'MANAGE_MESSAGES',
        'EMBED_LINKS',
        'ATTACH_FILES',
        'READ_MESSAGE_HISTORY',
        'MENTION_EVERYONE',
        'USE_EXTERNAL_EMOJIS',
        'VIEW_GUILD_INSIGHTS',
        'CONNECT',
        'SPEAK',
        'MUTE_MEMBERS',
        'DEAFEN_MEMBERS',
        'MOVE_MEMBERS',
        'USE_VAD',
        'CHANGE_NICKNAME',
        'MANAGE_NICKNAMES',
        'MANAGE_ROLES',
        'MANAGE_WEBHOOKS',
        'MANAGE_EMOJIS_AND_STICKERS',
        'USE_SLASH_COMMANDS',
        'REQUEST_TO_SPEAK',
        'MANAGE_THREADS',
        'USE_PUBLIC_THREADS',
        'USE_PRIVATE_THREADS',
        'USE_EXTERNAL_STICKERS',
    ]

    for (const permission of permissions) {
        if (!validPermissions.includes(permission)) {
            throw new Error(`Unknown permission node "${permission}"`)
        }
    }
}

module.exports = (client, commandOptions) => {
    let {
        commands,
        expectedArgs = '',
        permissionError = 'You do not have the permission to run this command.',
        minArgs = 0,
        maxArgs = null,
        permissions = [],
        requiredRoles = [],
        callback
    } = commandOptions

    if (typeof commands === 'string') {
        commands = [commands]
    }

    console.log(`Registering the "${commands[0]}" command!`)

    if (permissions.length) {
        if (typeof permissions === 'string') {
            permissions = [permissions]
        }

        validatePermissions(permissions)
    }

    client.on('message', (message) => {
        const { member, content, guild } = message

        for (const alias of commands) {
            const command = `${prefix}${alias.toLowerCase()}`

      if (
        content.toLowerCase().startsWith(`${command} `) ||
        content.toLowerCase() === command
      ) {

        for (const permission of permissions) {
          if (!member.hasPermission(permission)) {
            message.reply(permissionError)
            return
          }
        }

                for (const requiredRole of requiredRoles) {
                    const role = guild.roles.cache.find((role) => role.name === requiredRole)

                    if (!role || !member.roles.cache.has(role.id)) {
                        message.reply(`You must have the "${requiredRole}" role to use this command.`)
                        return
                    }
                }

                const arguments = content.split(/[ ]+/)

                arguments.shift()

                if (arguments.length < minArgs || (
                    maxArgs !== null && arguments.length > maxArgs
                )) {
                    const embed = new Discord.MessageEmbed()
                    embed.setTitle(`Incorrect syntax! Use ${prefix}${alias} ${expectedArgs}`)
                    message.reply(embed)
                    return
                }

                callback(message, arguments, arguments.join(' '), client) 

                return
            }
        }
    })
}

And here is the code for the specific add command:

module.exports = {
    commands: ['add', 'addition'],
    expectedArgs: '<num1> <num2>',
    permissionError: 'You do not have the permission to run this command.',
    minArgs: 2,
    maxArgs: 2,
    callback: (message, arguments, text) => {
        const num1 = +arguments[0]
        const num2 = +arguments[1]

        message.reply(`${num1} + ${num2} = ${num1 + num2}`)
    },
    permissions: ['SEND_MESSAGES'],
    requiredRoles: ['.𝐄𝐗𝐄 ₪ | 𝐕𝐞𝐫𝐢𝐟𝐢𝐞𝐝'],
}

Please advise.


Solution

  • require discord.js in your function and use Discord.MessageEmbed() to create the embed.

    To ping the user who ran the command use <@${message.author.id}>

    module.exports = {
        commands: ['add', 'addition'],
        expectedArgs: '<num1> <num2>',
        permissionError: 'You do not have the permission to run this command.',
        minArgs: 2,
        maxArgs: 2,
        callback: (message, arguments, text) => {
            const Discord = require("discord.js")
            const num1 = +arguments[0]
            const num2 = +arguments[1]
    
            const addEmbed = new Discord.MessageEmbed()
              .setTitle(`<@${message.author.id}> ${num1} + ${num2} = ${num1 + num2}`)
    
            message.reply(addEmbed)
        },
        permissions: ['SEND_MESSAGES'],
        requiredRoles: ['.𝐄𝐗𝐄 ₪ | 𝐕𝐞𝐫𝐢𝐟𝐢𝐞𝐝'],
    }