Javascript 那么,当请求时,如何从另一个文件运行代码
代码是一个不和谐的机器人,我想做的是当消息'!setup'被发送到discord聊天中,它从'2ndQ.js'发送代码(代码的第二部分) 它应该运行的是这个,在'!“设置”被输入到discord消息“它应该打印”菜单中,然后用户根据需要输入“1、2或3” 打印不是问题,问题是从另一个文件导入时,如何让代码运行 我正在使用repl.it对我正在使用node.js的bot进行编码Javascript 那么,当请求时,如何从另一个文件运行代码,javascript,node.js,import,discord,discord.js,Javascript,Node.js,Import,Discord,Discord.js,代码是一个不和谐的机器人,我想做的是当消息'!setup'被发送到discord聊天中,它从'2ndQ.js'发送代码(代码的第二部分) 它应该运行的是这个,在'!“设置”被输入到discord消息“它应该打印”菜单中,然后用户根据需要输入“1、2或3” 打印不是问题,问题是从另一个文件导入时,如何让代码运行 我正在使用repl.it对我正在使用node.js的bot进行编码 var menu = new Discord.MessageEmbed() .setTitle("Opti
var menu = new Discord.MessageEmbed()
.setTitle("Options: ")
.setDescription("Here are the different options for this bot!")
.addField("--> 1 = Casino", "Change prices of items and payout amount!")
.addField("--> 2 = Quotes", "Change around the quotes eg add a quote to the quote list")
.addField("--> 3 = Moderation", "Kick, Ban members and much more")
.addField("--> More to come in the future", "Leave some suggestions!")
.setImage("https://www.bolero.net/wp-content/uploads/2020/11/inr-banner-sercure.jpg")
var casino = new Discord.MessageEmbed()
.setTitle("--> Casino:")
.setDescription("Here are the different options under the Casino section!")
.addField("----> Change payout range on **slot machine**", "Type [SLR = Min(number), Max(number)]")
.addField("----> Change payout range on **blackjack**", "Type [BJR = Min(number), Max(number)]")
.addField("----> Change payout range on **cock fight**", "Type [CFR = Min(number), Max(number)]")
.addField("----> Change price of **chicken** for **cock fight**", "Type [CFP = Price]")
.addField("----> Change price of **lottery tickets**", "Type [LTP = x1(number), x5(number), x10(number), x100(number)]")
.setImage("https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f")
var casino = new Discord.MessageEmbed()
.setTitle("--> Casino:")
.setDescription("Here are the different options under the Casino section!")
.addField("----> Change payout range on **slot machine**", "Type [SLR = Min(number), Max(number)]")
.addField("----> Change payout range on **blackjack**", "Type [BJR = Min(number), Max(number)]")
.addField("----> Change payout range on **cock fight**", "Type [CFR = Min(number), Max(number)]")
.addField("----> Change price of **chicken** for **cock fight**", "Type [CFP = Price]")
.addField("----> Change price of **lottery tickets**", "Type [LTP = x1(number), x5(number), x10(number), x100(number)]")
.setImage("https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f")
var quote = new Discord.MessageEmbed()
.setTitle("--> Quotes:")
.setDescription("Here are the different options under the Casino section!")
.addField("----> Clear **quotes**", "Type [Clear quotes]")
.addField("----> Add to quotes", "Type [Add (quote....- first name, last name)]")
.addField("----> Remove a quote", "Type [Remove (quote...)]")
.setImage("https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f")
var mod = new Discord.MessageEmbed()
.setTitle("--> Casino:")
.setDescription("Here are the different options under the Casino section!")
.addField("----> **Ban** a member", "Type [Ban @member]")
.addField("----> **Warn** a member", "Type [Warn @member]")
.addField("----> Create a **self role* message", "Type [Create ]")
.setImage("https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f")
client.on("guildMemberAdd", (member) => {
const channel = member.guild.channels.cache.find(
(ch) => ch.name === "general"
);
if (!channel) return;
channel.send(
"Welcome to the server, I'm the resident bot."
);
});
channel.send(menu)
ping_char = '!'
client.on('message', msg => {
if (msg.content === ping_char+'1') {
msg.channel.send(casino);
}
if (msg.content === ping_char+'2') {
msg.channel.send(quote);
}
if (msg.content === ping_char+'3') {
msg.channel.send(mod);
}
});
您可以从
2ndQ.js
和require
将具有函数/方法的对象导出到主文件中,如下所示:
const command = require('./2ndQ.js')
现在,您可以从导出的对象调用该方法(我们将其称为execute()
),因为您需要访问其中的消息
,请将其作为参数传递
//index.js
// ...
常量ping_char='!';
client.on('message',异步(message)=>{
如果(message.content==`${ping\u char}setup`){
const命令=require('./2ndQ.js');
命令。执行(消息);
}
});
在2ndQ.js
中,您需要再次导入Discord
,以便使用嵌入
但是,检查消息内容并不是检查用户是否在之后响应的正确方法!设置
命令。虽然你说印刷不是问题;现在使用您的代码,任何人都可以发送一个号码,而无需调用setup
命令,bot将发送嵌入
Discord具有收集器,您可以使用该方法设置一个收集器。它基本上监听传入的消息,并检查发送消息的用户是否是发送当前消息的用户
当触发collect
事件时,您可以检查响应内容并发送相应的消息。查看下面的工作代码:
//2ndQ.js
const Discord=require('Discord.js');
const menu=newdiscord.MessageEmbed()
.setTitle('选项:')
.setDescription('这里是此bot的不同选项!')
.addField(“-->1=Casino”,“更改项目价格和支付金额!”)
艾德菲尔德先生(
“-->2=引号”,
'更改报价,例如将报价添加到报价列表',
)
.addField(“-->3=适度”、“踢、禁止成员和更多”)
.addField(“-->”未来还有更多内容“,”请留下建议!”)
.setImage(
'https://www.bolero.net/wp-content/uploads/2020/11/inr-banner-sercure.jpg',
);
const casino=new Discord.MessageEmbed()
.setTitle('-->赌场:')
.setDescription('这里是赌场部分下的不同选项!')
艾德菲尔德先生(
'--->更改**老虎机**上的付款范围,
“类型[SLR=最小(数量),最大(数量)]”,
)
艾德菲尔德先生(
'--->更改**21点**的付款范围,
“类型[BJR=最小(数量),最大(数量)]”,
)
艾德菲尔德先生(
'--->更改**斗鸡**的付款范围,
“类型[CFR=最小(数量),最大(数量)]”,
)
艾德菲尔德先生(
“--->将**鸡**的价格更改为**斗鸡**”,
“类型[CFP=价格]”,
)
艾德菲尔德先生(
“--->更改**彩票**的价格”,
“类型[LTP=x1(编号)、x5(编号)、x10(编号)、x100(编号)]”,
)
.setImage(
'https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f',
);
const quote=new Discord.MessageEmbed()
.setTitle('-->引号:')
.setDescription('这里是赌场部分下的不同选项!')
.addField('--->清除**引号**',键入[清除引号])
艾德菲尔德先生(
'--->添加到引号',
'键入[添加(引号…-姓名)],
)
.addField('--->删除引号),'键入[删除(引号…)')
.setImage(
'https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f',
);
const mod=new Discord.MessageEmbed()
.setTitle('-->Moderation:')
.setDescription('这里是赌场部分下的不同选项!')
.addField('--->**Ban**a成员','键入[Ban@member]')
.addField('--->**Warn**a成员','键入[Warn@member]')
.addField('--->创建**自我角色*消息,''键入[Create]')
.setImage(
'https://www.startus.cc/sites/default/files/styles/company_profile_cover_crop/public/img-casino.jpg?itok=E1XXjp5b&sc=775a4769d2582602853a3c7b3efeed6f',
);
module.exports={
异步执行(消息){
//把菜单送来就行了
等待消息。频道。发送(菜单);
//筛选器检查响应是否来自键入命令的作者
const filter=(response)=>response.author.id==message.author.id;
常量maxWait=10000;//以毫秒为单位
//设置消息收集器以检查是否有响应
const collector=message.channel.createMessageCollector(过滤器{
//设置收集器运行的最大等待时间
//不过这是可选的
时间:maxWait,
});
//在收集响应时激发
collector.on('collect',(response)=>{
如果(response.content==“1”){
返回消息.channel.send(赌场);
}
如果(response.content==“2”){
返回消息。通道。发送(报价);
}
如果(response.content==“3”){
返回消息.channel.send(mod);
}
});
//收集器完成收集后激发
收集器.on('结束',(已收集,原因)=>{
//仅在“结束”事件因超时而触发时发送消息
如果(原因!=“时间”)返回;
message.channel.send(
`好的,${message.author},我很无聊,不能再等了。如果您想再次查看菜单,请再次键入\`!setup\``,
);
});
},
};
好的,这样做意味着我得到了这个错误:
/home/runner/Java discord bot/index.js:320 command.execute(msg);^类型错误:command.execute不是一个函数
@
const command = require('./2ndQ.js')