【问题标题】:Problem with sending text from yaml Discord.js从 yaml Discord.js 发送文本的问题
【发布时间】:2021-06-22 04:31:03
【问题描述】:

我从 yaml 发送文本时遇到问题。保存文本有效,但发送无效。代码如下:

const { Message, MessageEmbed } = require("discord.js")
const { channel } = require('./kanal')
const db = require('quick.db')
module.exports = {
  name: "reklama",
  guildOnly: true,
  description:
    "Change guild prefix. If no arguments are passed it will display actuall guild prefix.",
  usage: "[prefix]",


   run(msg, args, guild) {
    
    if (!guild) {
      const guld = new MessageEmbed()
        .setTitle("Błąd!")
        .setColor("RED")
        .setDescription("Tą komende można tylko wykonać na serwerze!")
        .setThumbnail('https://emoji.gg/assets/emoji/3485-cancel.gif')
        .setTimestamp()
        .setFooter(`${msg.author.tag} (${msg.author.id})`, `${msg.author.displayAvatarURL({dynamic: true})}`)
     
      msg.channel.send(guild)
    }

    const { settings } = client
    const prefixArg = args[0]

    if (!settings.get(guild.id)) {
      settings.set(guild.id, { prefix: null })
    }

    if (!prefixArg) {
      let Reklama = client.settings.get(guild.id).Reklama
      let Kanal = client.settings.get(guild.id).Kanał


      const embed = new MessageEmbed()
        .setTitle(`Informacje o serwerze: ${msg.guild.name}`)
        .addField("Treść reklamy:", Reklama)
        .addField("Kanał do wysyłania reklam:", Kanal)
      msg.channel.send(embed)
      
    }
    
    setInterval(() => {
      Kanal.send(`${Reklama}`)
    }, 1000)
    
    


  },catch(e){
    console.log(e)
  }
}

这是命令处理程序的一部分:


    const args = msg.content.slice(length).trim().split(" ")

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

    const cmd =
      client.commands.get(cmdName) ||
      client.commands.find(
        (cmd) => cmd.aliases && cmd.aliases.includes(cmdName),
      )
    
    try {
      cmd.run(msg, args)
    } catch (error) {
      console.error(error)
    }
  })
}

问题是当我启动机器人时,它显示了这样一个错误:

让 Reklama = client.settings.get(guild.id).Reklama ^

TypeError: 无法读取未定义的属性“id”

【问题讨论】:

  • 如何调用run 方法?你传递msgargsguild的顺序是否正确?
  • 是的,顺序正确
  • 您也可以添加这部分代码吗?您可以edit your question 并将其添加到那里。
  • 我添加了部分命令处理程序

标签: discord discord.js bots


【解决方案1】:

问题是您没有将guild 变量传递给您的run 方法。你调用cmd.run(msg, args)run 接受三个参数。

您可以像这样传递guild 或从msg 获取它:

module.exports = {
  name: 'reklama',
  guildOnly: true,
  description:
    'Change guild prefix. If no arguments are passed it will display actuall guild prefix.',
  usage: '[prefix]',

  run(msg, args) {
    // destructure the guild the message was sent in
    const { guild } = msg;

    if (!guild) {
      const embed = new MessageEmbed()
        .setTitle('Błąd!')
        .setColor('RED')
        .setDescription('Tą komende można tylko wykonać na serwerze!')
        .setThumbnail('https://emoji.gg/assets/emoji/3485-cancel.gif')
        .setTimestamp()
        .setFooter(
          `${msg.author.tag} (${msg.author.id})`,
          `${msg.author.displayAvatarURL({ dynamic: true })}`,
        );

      return msg.channel.send(embed);
    }

    const { settings } = client;
    const prefixArg = args[0];

    if (!settings.get(guild.id)) {
      settings.set(guild.id, { prefix: null });
    }

    if (!prefixArg) {
      let Reklama = client.settings.get(guild.id).Reklama;
      let Kanal = client.settings.get(guild.id).Kanał;

      const embed = new MessageEmbed()
        .setTitle(`Informacje o serwerze: ${msg.guild.name}`)
        .addField('Treść reklamy:', Reklama)
        .addField('Kanał do wysyłania reklam:', Kanal);
      msg.channel.send(embed);
    }

    setInterval(() => {
      Kanal.send(`${Reklama}`);
    }, 1000);
  },
  catch(e) {
    console.log(e);
  },
};

【讨论】:

  • 什么问题?
  • 同样的错误:让 Reklama = client.settings.get(guild.id).Reklama ^ TypeError: Cannot read property 'id' of undefined
  • 哦,我错过了return msg.channel.send(embed); 的返回。如果没有公会,我们发送消息并且我们不想运行其余代码。我刚刚更新了我的答案。
  • 仍然无法正常工作,你可以在 dc 上加我吗?我将发送完整的 src:Ghoste#3295
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 2021-02-17
  • 2017-10-30
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
相关资源
最近更新 更多