【问题标题】:Export variable to other files discordjs将变量导出到其他文件 discordjs
【发布时间】:2021-08-16 15:39:31
【问题描述】:

我更改了我的命令处理程序,并且不再从 profileData 中导出任何内容(以前是命令处理程序文件)。该文件正在检查他们的个人资料并将数据保存在 profileData 中,或者如果他们没有,则为他们创建一个。我想导出 profileData 以在其他文件中使用,而不是在每个命令中查询。我怎样才能做到这一点。我已经尝试将 module.exports 和 exports.profileData = profileData 放在所有地方,但没有任何效果,我只是迷失了如何做到这一点。任何帮助将不胜感激。

profileData-

const profileModel = require("../../models/profileSchema");
  module.exports =  (client) => {
    client.on("message", async (message) => {
      const prefix = 's!'
  if (!message.content.startsWith(prefix) || message.author.bot) return;
  
  try {
    const profileData = await profileModel.findOne({ userID: message.author.id });
    if (!profileData) {
      let profile = await profileModel.create({
        userID: message.author.id,
        serverID: message.guild.id,
        coins: 10,
        bank: 0,
      });
      profile.save();
    }
  } catch (err) {
    console.log(err);
  }
})
}
module.exports.config = {
  displayName: 'Profile Data',
  dbName: 'PROFILEDATA',
  loadDBFirst: true
}

余额-

const { MessageEmbed } = require("discord.js");
const { profileData } = require("../../Features/client/profileData");
module.exports = {
  name: "balance",
  aliases: ["bal"],
  category: "Economy",
  cooldown: "20s",
  permissions: ["ADMINISTRATOR"],
  maxArgs: 0,
  description: "Check your wallet and bank balance!",
  execute: async ({ message, client }) => {
    let balPlaceholder = "'s balance";

    const BalEmbed = new MessageEmbed()
      .setColor("#0bbffc")
      .setAuthor("Saoul 2")
      .setTitle(`${message.author.username}${balPlaceholder}`)
      .addFields(
        { name: "???? Wallet:", value: `${profileData.coins}`, inline: true },
        { name: "???? Bank:", value: `${profileData.bank}`, inline: true }
      )
      .setTimestamp()
      .setFooter(
        `Command Requested By ${message.author.tag}`,
        client.user.displayAvatarURL()
      );
    message.channel.send(BalEmbed);
  },
};

【问题讨论】:

    标签: javascript node.js mongodb mongodb-query discord.js


    【解决方案1】:

    当通过命令处理程序发送消息时,这就是让函数运行所需的全部内容。

    module.exports = {
        getData: async (message) => {
            try {
    

    【讨论】:

      【解决方案2】:

      如果我理解正确:您想导出 profileData,如果找不到,请创建一个新的配置文件并使用它。下面的 sn-p 可能会解决你的问题。

      const profileModel = require("../../models/profileSchema");
      
      
      let data = getData(client);
      exports.data = data;
      
      async function getData(client) {
        client.on("message", async(message) => {
          const prefix = 's!'
          if (!message.content.startsWith(prefix) || message.author.bot) return;
      
          try {
            const profileData = await profileModel.findOne({
              userID: message.author.id
            });
            if (!profileData) {
              let profile = await profileModel.create({
                userID: message.author.id,
                serverID: message.guild.id,
                coins: 10,
                bank: 0,
              });
              profile.save();
              return profile;
            } else {
              return profileData;
            }
          } catch (err) {
            console.log(err);
          }
        })
      }
      
      module.exports.config = {
        displayName: 'Profile Data',
        dbName: 'PROFILEDATA',
        loadDBFirst: true
      }

      【讨论】:

      • 如果它可以与我的命令处理程序一起使用,它将起作用。我必须通过内置的命令处理程序定义客户端。 module.exports = (client) => { 也许有办法在你发送的内容中做到这一点?
      • @Saoul 您可以添加客户端:getData(client) 例如:let data = getData(client);异步函数 getData(client) { . . .
      • 我试过了,它的客户端是未定义的,我还尝试将您发送的内容放入 module.exports = (client) => { 并且运行没有问题但仍然未定义数据我的命令。
      • 也许您的 profileSchema 中没有正确定义硬币?这将是我现在的第一个猜测......现在其他一切正常吗?
      • Christoph Blüm 这曾经运行顺利,因为这段代码在我的命令处理程序中,所以当有人运行命令时,它只是内置在其中。现在我切换到 wokcommands,所以我不需要自己的。数据库没有任何问题,或者只是我不知道如何导出要在我的命令中使用的 profileData 变量。这就是为什么我不明白为什么我不能使用我拥有的解决方案来导入客户端和导出 profileData。通过命令处理程序使用 module.exports = (client) => 导入客户端。然后是您导出 profileData 的解决方案。
      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 2019-10-24
      • 2015-12-04
      • 1970-01-01
      • 2019-10-11
      • 2019-04-25
      • 1970-01-01
      • 2022-08-12
      相关资源
      最近更新 更多