消息未定义

时间:2021-01-17 01:24:44

标签: javascript discord

ogloszenie.command.js 文件

const { RichEmbed, Channel } = require("discord.js")
    const Discord = require('discord.js');
    
    module.exports = {
      name: "ogloszenie",
      description: "Wysyła ogłoszenie",
    
      run(msg, args) {
    
        const { channel } = msg
        
        let announcemessage = message.content.match(/(?<=announce ).*$/)[0];
        let finalmessage = announcemessage.toUpperCase();
    
        const embed = new RichEmbed()
        .setColor("RED")
        .setTitle("? • OGŁOSZENIE", "https://media.discordapp.net/attachments/800088813774438440/800089290972463114/7921185_0.jpg")
        .setFooter("el cartel de medelline", "https://media.discordapp.net/attachments/800088813774438440/800089290972463114/7921185_0.jpg")
        .setTimestamp()
        .setDescription(finalmessage);
    
        channel.send(embed)
        
    
      }
    }

command.handler.js 文件

const { readdirSync } = require("fs")

const { prefix } = require(__dirname + "/../config/config.js")

const { Collection } = require("discord.js")

const ascii = require("ascii-table")

const table = new ascii().setHeading("Command", "Load status")

module.exports = (client) => {
  // Collections
  client.commands = new Collection()

  const commandFiles = readdirSync(__dirname + "/../commands").filter((file) =>
    file.endsWith(".command.js"),
  )

  for (const file of commandFiles) {
    const command = require(__dirname + `/../commands/${file}`)

    if (command.name) {
      client.commands.set(command.name, command)
      table.addRow(file, "✅")
    } else {
      table.addRow(file, "❌  -> missing 'name'!")
      continue
    }
  }

  console.log(table.toString())

  client.on("message", (msg) => {
    const { author, guild } = msg

    // Check if user is a bot
    if (author.bot || !guild) {
      return
    }

    // Ignore messages without prefix
    if (!msg.content.startsWith(prefix)) return

    const args = msg.content
      .slice(prefix.length)
      .trim()
      .split(/ +/g)

    const cmd = args.shift().toLowerCase()

    // Check if commands exist
    if (!client.commands.has(cmd)) return

    try {
      client.commands.get(cmd).run(msg, args)
    } catch (error) {
      console.error(error)
      msg.reply("there was an error trying to execute that command!")
    }
  })
}

控制台日志

C:\Users\profe\Desktop\DiscordBot-1.6.0\src\commands\ogloszenie.command.js:15 让announcemessage = message.content.match(/(?<=announce ).*$/)[0]; ^

ReferenceError: 消息未定义 在 Object.module.exports.run (C:\Users\profe\Desktop\DiscordBot-1.6.0\src\commands\ogloszenie.command.js:15:29) 在客户端。 (C:\Users\profe\Desktop\DiscordBot-1.6.0\src\handlers\command.handler.js:55:32) 在 Client.emit (node:events:329:20) 在 MessageCreateHandler.handle (C:\Users\profe\Desktop\DiscordBot-1.6.0\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34) 在 WebSocketPacketManager.handle (C:\Users\profe\Desktop\DiscordBot-1.6.0\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65) 在 WebSocketConnection.onPacket (C:\Users\profe\Desktop\DiscordBot-1.6.0\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35) 在 WebSocketConnection.onMessage (C:\Users\profe\Desktop\DiscordBot-1.6.0\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17) 在 WebSocket.onMessage (C:\Users\profe\Desktop\DiscordBot-1.6.0\node_modules\ws\lib\event-target.js:120:16) 在 WebSocket.emit (node:events:329:20) 在 Receiver.receiverOnMessage (C:\Users\profe\Desktop\DiscordBot-1.6.0\node_modules\ws\lib\websocket.js:789:20) PS C:\Users\profe\Desktop\DiscordBot-1.6.0>

如何解决?

1 个答案:

答案 0 :(得分:0)

您应该使用 msg 而不是 message

const { RichEmbed, Channel } = require("discord.js")
    const Discord = require('discord.js');
    
    module.exports = {
      name: "ogloszenie",
      description: "Wysyła ogłoszenie",
    
      run(msg, args) {
    
        const { channel } = msg
        
        let announcemessage = msg.content.match(/(?<=announce ).*$/)[0];
        let finalmessage = announcemessage.toUpperCase();
    
        const embed = new RichEmbed()
        .setColor("RED")
        .setTitle("? • OGŁOSZENIE", "https://media.discordapp.net/attachments/800088813774438440/800089290972463114/7921185_0.jpg")
        .setFooter("el cartel de medelline", "https://media.discordapp.net/attachments/800088813774438440/800089290972463114/7921185_0.jpg")
        .setTimestamp()
        .setDescription(finalmessage);
    
        channel.send(embed)
        
    
      }
    }