Posting embed from JSON

Struggling to get my code to run, implementing in an existing bot setup;

const DiscordJS = require('discord.js')
const WOKCommands = require('wokcommands')
require('dotenv').config();

module.exports = {
  name: "embedjson",
      category: "info",
    description: "post embed from json data",
  minArgs: 2,
  expectedArgs: '<Channel mention> <JSON>',
  run: async ({client, message, args, level }) => {
    // get the target channel
    const targetChannel = message.mentions.channels.first()
    if (!targetChannel) {
      message.reply('Please specify a channel to send the embed in')
      return
    }

    // removes the channel mention
    args.shift()

    try {
      // get the JSON data
      const json = JSON.parse(args.join(' '))
      const { text = '' } = json

      // send the embed
      targetChannel.send(text, {
        embed: json,
      })
    } catch (error) {
      message.reply(`Invalid JSON ${error.message}`)
    }
  },
}

And throwing up this error;

TypeError: Cannot read property 'mentions' of undefined
    at Object.run (/home/runner/Manias-Bot/commands/embed/embedjson.js:11:35)
    at Client.<anonymous> (/home/runner/Manias-Bot/index.js:78:24)
    at Client.emit (events.js:387:35)
    at MessageCreateAction.handle (/home/runner/Manias-Bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/Manias-Bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:375:28)
    at Receiver.receiverOnMessage (/home/runner/Manias-Bot/node_modules/ws/lib/websocket.js:970:20)
    at Receiver.emit (events.js:375:28)
    at Receiver.dataMessage (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:517:14)
    at Receiver.getData (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:435:17)
    at Receiver.startLoop (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:143:22)
    at Receiver._write (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:78:10) Promise {
  <rejected> TypeError: Cannot read property 'mentions' of undefined
      at Object.run (/home/runner/Manias-Bot/commands/embed/embedjson.js:11:35)
      at Client.<anonymous> (/home/runner/Manias-Bot/index.js:78:24)
      at Client.emit (events.js:387:35)
      at MessageCreateAction.handle (/home/runner/Manias-Bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
      at Object.module.exports [as MESSAGE_CREATE] (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
      at WebSocketManager.handlePacket (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
      at WebSocketShard.onPacket (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
      at WebSocketShard.onMessage (/home/runner/Manias-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
      at WebSocket.onMessage (/home/runner/Manias-Bot/node_modules/ws/lib/event-target.js:132:16)
      at WebSocket.emit (events.js:375:28)
      at Receiver.receiverOnMessage (/home/runner/Manias-Bot/node_modules/ws/lib/websocket.js:970:20)
      at Receiver.emit (events.js:375:28)
      at Receiver.dataMessage (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:517:14)
      at Receiver.getData (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:435:17)
      at Receiver.startLoop (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:143:22)
      at Receiver._write (/home/runner/Manias-Bot/node_modules/ws/lib/receiver.js:78:10)
}

Any tips?

Hi @DTJW92! Welcome to the community! Can you please give us your project URL?

The problem seems here, but we need more code to investigate further :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.