【问题标题】:Can not read property "mentions" of undefined, can not read property "guild" of undefined | Discord.js slash commands无法读取未定义的属性“提及”,无法读取未定义的属性“公会”| Discord.js 斜线命令
【发布时间】:2021-07-29 15:59:51
【问题描述】:

所以我尝试为我的不和谐机器人使用斜杠命令,我已经从旧机器人复制并更改了一些警告命令,其中警告命令在没有斜杠命令的情况下工作。

我的代码是

const { MessageEmbed } = require('discord.js')
const mongo = require('../mongo')
const warnSchema = require('../schemas/warn-schema')
const Discord = require('discord.js')

module.exports = {
  slash: 'both',
  testOnly: true,
  minArgs: 2,
  expectedArgs: '<mention> <reason>',
  description: "warns mentioned user",
  callback: async ({ message, args }) => {

const [mention, reason] = args

      const target = message.mentions.users.first()

 

    const guildId = message.guild.id
    const userId = target.id
    

    const warning = {
      author: message.member.user.tag,
      timestamp: new Date().getTime(),
      reason,
    }

const yerz = new MessageEmbed()
  .setTitle('yes')

    await mongo().then(async (mongoose) => {
      try {
        await warnSchema.findOneAndUpdate(
          {
            guildId,
            userId,
          },
          {
            guildId,
            $push: {
              warnings: warning,
            },
          },
          {
            upsert: true,
          }
        )
      } finally {
        mongoose.connection.close()
      }
    })

    return yerz
  },
}

如果有人知道我如何让机器人找到 guid id 并阅读提及,我们将不胜感激。

【问题讨论】:

  • 如何调用callback函数?
  • 什么意思?
  • 我的意思是,您使用 callback 方法导出一个对象,然后在某处调用它,但您似乎没有传递正确的参数。
  • 那我该如何解决呢?
  • 我不知道。我已经问过您是如何使用 callback 的,但您没有向我们展示任何内容。

标签: discord.js


【解决方案1】:

我认为问题出在您的 JS 代码中:

callback: async ({ message, args }) => {

应该是:

callback: async ({ message, args }) {

没有=&gt;,因为在这种情况下 => 和 {} 是互斥的。

如果这不能解决问题,则可能是以下行:

const [mention, reason] = args

你能告诉我们什么是 args 吗?我猜它是一个对象,但我不确定。

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 2022-08-04
    • 2021-06-25
    • 2022-06-10
    • 2021-11-13
    • 2021-06-27
    • 2020-07-20
    • 1970-01-01
    • 2020-12-05
    相关资源
    最近更新 更多