从其他文件导入变量
Importing variables from other files
我正在尝试从我的一个文件(文件 1)导入一个变量并在文件 2 中使用它。我已将文件 2 导入文件 1,但我收到错误消息。我的频道 ID 是正确的,在这种情况下,您必须选择频道,所以频道 ID 不是这里的问题。
TypeError: setr.send is not a function
文件 1
const Discord = require("discord.js");
const axios = require("axios");
let config = require("../config.json");
module.exports = {
name: "setrestart",
description: "sets the restart channel",
async execute(message, args) {
const perm = new Discord.MessageEmbed()
.setDescription(":x: You do not have permission to use this command.")
.setColor("#E74C3C");
if (!message.guild.me.hasPermission("ADMINISTRATOR"))
return message.channel.send(perm);
if (message.author.id !== "ID")
return message.channel.send(perm);
const channelx =
message.mentions.channels.first() ||
message.guild.channels.cache.find((c) => c.id === args[0]);
if (!channelx)
return message.channel.send(
`:x: Please specify the channel where server restarts will go!`
);
message.reply(`All server restart logs will now go to ${channelx}.`)
},
};
文件 2
const Discord = require("discord.js");
const axios = require("axios");
let config = require("../config.json");
let setr = require("./setrestart"); // This is importing file 1
module.exports = {
name: "restart",
description: "send a restart message in status channel",
async execute(message, args) {
const perm = new Discord.MessageEmbed()
.setDescription(":x: You do not have permission to use this command.")
.setColor("#E74C3C");
if (!message.guild.me.hasPermission("ADMINISTRATOR"))
return message.channel.send(perm);
if (message.author.id !== "ID")
return message.channel.send(perm);
const restart = new Discord.MessageEmbed()
.setTitle(" Server Restarted! ")
.setDescription(`F8 connect to ${config.SERVER_URL} `)
.setColor("RANDOM")
.setTimestamp()
.setFooter(`${config.SERVER_LOGO}`);
setr.channelx.send(restart) // This does not work.
},
};
非常感谢您的帮助。
编辑:我遗漏了关于我要导入的内容的最关键的事情。
我正在尝试导入 文件 1 中的 channelx
并且我正在尝试使用 文件 2[=44= 中的变量].
输出
用户:/setrestart #channel
Bot:所有服务器重启日志现在将转到 ${channelx}
。
用户:/重启
Bot:嵌入在 channelx
中发送
我很确定你不能那样使用 module.exports
。您应该只将 channelx
添加到导出中。
使用 this.channelx = channelx
.
这不是导入和导出的工作方式。您的 channelx
变量是在一个函数的执行中定义的,您没有 returning 它。
我不确定整个 Discord API 是如何工作的以及得到 returned 的形状是什么,但你应该可以这样做:
文件 1
module.exports = {
name: "setrestart",
description: "sets the restart channel",
async execute(message, args) {
// ... everything as per your file
message.reply(`All server restart logs will now go to ${channelx}.`);
return channelx;
},
};
文件 2
module.exports = {
name: "restart",
description: "send a restart message in status channel",
async execute(message, args) {
// ... everything the same as per your file
const channelx = await setr.execute(message, args);
channelx.send(restart);
},
};
基本上,这里发生的事情是第一个模块公开了一个函数,该函数计算出您的目标频道,然后 returns 它。
一旦你 return 它,你可以用它做任何你想做的事。
请注意,您的第一个函数可能不需要 async
,因为您没有任何 await
指令。
阅读有关范围的更多信息:https://developer.mozilla.org/en-US/docs/Glossary/Scope
变量channelx
只能在execute()
的函数范围内访问,不能导入。基本上在函数超出范围后,变量就会丢失。使用全局对象,注意对象在程序退出时被销毁。因此,如果您尝试进行某种机器人配置,则需要将对象保存到文件中。
下面是一个如何正确实施您正在尝试做的事情的示例。
文件 1 (file1.js
):
// ... Load storage from a JSON file ...
const storage = {};
module.exports = {
name: "setrestart",
description: "sets the restart channel",
async execute(message, args) {
// ... Permission checking ...
const channelx = message.mentions.channels.first() ||
message.guild.channels.cache.find((c) => c.id === args[0]);
if (!channelx) {
return message.channel.send(
`:x: Please specify the channel where server restarts will go!`
);
}
// Store restart channel id per guild
storage[message.guild.id] = channelx.id;
message.reply(`All server restart logs will now go to ${channelx}.`);
// ... Write to the storage JSON file and update it with new data ...
},
};
module.exports.storage = storage;
文件 2 (file2.js
):
const Discord = require("discord.js");
const file1 = require("./file1.js");
module.exports = {
name: "restart",
description: "send a restart message in status channel",
async execute(message, args) {
// ... Permission checking ...
const restart = new Discord.MessageEmbed()
.setTitle(" Server Restarted! ")
.setColor("RANDOM")
.setTimestamp();
const channelId = file1.storage[message.guild.id];
// If there is no restart channel set, default to the system channel
if (!channelId) channelId = message.guild.systemChannelID;
const channel = await message.client.channels.fetch(channelId);
channel.send(restart);
},
};
请注意,我已经删除了您的部分代码,以使其在我的机器上运行。
使用 discord.js ^12.5.3
.
我正在尝试从我的一个文件(文件 1)导入一个变量并在文件 2 中使用它。我已将文件 2 导入文件 1,但我收到错误消息。我的频道 ID 是正确的,在这种情况下,您必须选择频道,所以频道 ID 不是这里的问题。
TypeError: setr.send is not a function
文件 1
const Discord = require("discord.js");
const axios = require("axios");
let config = require("../config.json");
module.exports = {
name: "setrestart",
description: "sets the restart channel",
async execute(message, args) {
const perm = new Discord.MessageEmbed()
.setDescription(":x: You do not have permission to use this command.")
.setColor("#E74C3C");
if (!message.guild.me.hasPermission("ADMINISTRATOR"))
return message.channel.send(perm);
if (message.author.id !== "ID")
return message.channel.send(perm);
const channelx =
message.mentions.channels.first() ||
message.guild.channels.cache.find((c) => c.id === args[0]);
if (!channelx)
return message.channel.send(
`:x: Please specify the channel where server restarts will go!`
);
message.reply(`All server restart logs will now go to ${channelx}.`)
},
};
文件 2
const Discord = require("discord.js");
const axios = require("axios");
let config = require("../config.json");
let setr = require("./setrestart"); // This is importing file 1
module.exports = {
name: "restart",
description: "send a restart message in status channel",
async execute(message, args) {
const perm = new Discord.MessageEmbed()
.setDescription(":x: You do not have permission to use this command.")
.setColor("#E74C3C");
if (!message.guild.me.hasPermission("ADMINISTRATOR"))
return message.channel.send(perm);
if (message.author.id !== "ID")
return message.channel.send(perm);
const restart = new Discord.MessageEmbed()
.setTitle(" Server Restarted! ")
.setDescription(`F8 connect to ${config.SERVER_URL} `)
.setColor("RANDOM")
.setTimestamp()
.setFooter(`${config.SERVER_LOGO}`);
setr.channelx.send(restart) // This does not work.
},
};
非常感谢您的帮助。
编辑:我遗漏了关于我要导入的内容的最关键的事情。
我正在尝试导入 文件 1 中的 channelx
并且我正在尝试使用 文件 2[=44= 中的变量].
输出
用户:/setrestart #channel
Bot:所有服务器重启日志现在将转到 ${channelx}
。
用户:/重启
Bot:嵌入在 channelx
我很确定你不能那样使用 module.exports
。您应该只将 channelx
添加到导出中。
使用 this.channelx = channelx
.
这不是导入和导出的工作方式。您的 channelx
变量是在一个函数的执行中定义的,您没有 returning 它。
我不确定整个 Discord API 是如何工作的以及得到 returned 的形状是什么,但你应该可以这样做:
文件 1
module.exports = {
name: "setrestart",
description: "sets the restart channel",
async execute(message, args) {
// ... everything as per your file
message.reply(`All server restart logs will now go to ${channelx}.`);
return channelx;
},
};
文件 2
module.exports = {
name: "restart",
description: "send a restart message in status channel",
async execute(message, args) {
// ... everything the same as per your file
const channelx = await setr.execute(message, args);
channelx.send(restart);
},
};
基本上,这里发生的事情是第一个模块公开了一个函数,该函数计算出您的目标频道,然后 returns 它。
一旦你 return 它,你可以用它做任何你想做的事。
请注意,您的第一个函数可能不需要 async
,因为您没有任何 await
指令。
阅读有关范围的更多信息:https://developer.mozilla.org/en-US/docs/Glossary/Scope
变量channelx
只能在execute()
的函数范围内访问,不能导入。基本上在函数超出范围后,变量就会丢失。使用全局对象,注意对象在程序退出时被销毁。因此,如果您尝试进行某种机器人配置,则需要将对象保存到文件中。
下面是一个如何正确实施您正在尝试做的事情的示例。
文件 1 (file1.js
):
// ... Load storage from a JSON file ...
const storage = {};
module.exports = {
name: "setrestart",
description: "sets the restart channel",
async execute(message, args) {
// ... Permission checking ...
const channelx = message.mentions.channels.first() ||
message.guild.channels.cache.find((c) => c.id === args[0]);
if (!channelx) {
return message.channel.send(
`:x: Please specify the channel where server restarts will go!`
);
}
// Store restart channel id per guild
storage[message.guild.id] = channelx.id;
message.reply(`All server restart logs will now go to ${channelx}.`);
// ... Write to the storage JSON file and update it with new data ...
},
};
module.exports.storage = storage;
文件 2 (file2.js
):
const Discord = require("discord.js");
const file1 = require("./file1.js");
module.exports = {
name: "restart",
description: "send a restart message in status channel",
async execute(message, args) {
// ... Permission checking ...
const restart = new Discord.MessageEmbed()
.setTitle(" Server Restarted! ")
.setColor("RANDOM")
.setTimestamp();
const channelId = file1.storage[message.guild.id];
// If there is no restart channel set, default to the system channel
if (!channelId) channelId = message.guild.systemChannelID;
const channel = await message.client.channels.fetch(channelId);
channel.send(restart);
},
};
请注意,我已经删除了您的部分代码,以使其在我的机器上运行。
使用 discord.js ^12.5.3
.