该代码用于不和谐机器人,我想要做的是当消息“!setup”发送到不和谐聊天时,它会发送来自“2ndQ.js”的代码(代码的第二部分)
client.on('message', msg => {
if (msg.content === ping_char+'setup') {
msg.channel.send(install 2ndQ);
}
它应该运行的是这样的,在不和谐消息中输入“!setup”后,它应该打印菜单,然后用户根据他们想要执行的操作输入“1、2或3”!
打印不是问题,问题是如何在从另一个文件导入时运行此代码。
我正在使用 repl.it 来编写我正在使用 node.js 的机器人
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
// ...
const ping_char = '!';
client.on('message', async (message) => {
if (message.content === `${ping_char}setup`) {
const command = require('./2ndQ.js');
command.execute(message);
}
});
在 2ndQ.js
中,您需要再次导入 Discord
,以便可以使用嵌入。
但是,检查消息内容并不是检查用户在 !setup
命令后是否得到响应的正确方法。尽管您说打印不是问题;目前使用您的代码,任何人都可以发送号码,甚至无需调用 setup
命令,机器人就会发送嵌入内容。
Discord 有收集器,您可以使用createMessageCollector
设置一个收集器。方法。它基本上监听传入的消息并检查发送消息的用户是否是发送当前消息的用户。
当 collect
事件触发时,您可以检查响应内容并发送相应的嵌入内容。查看下面的工作代码:
// 2ndQ.js
const Discord = require('discord.js');
const 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 come suggetions!')
.setImage(
'https://www.bolero.net/wp-content/uploads/2020/11/inr-banner-sercure.jpg',
);
const 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',
);
const 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',
);
const mod = new Discord.MessageEmbed()
.setTitle('--> Moderation:')
.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',
);
module.exports = {
async execute(message) {
// just send that menu
await message.channel.send(menu);
// filter checks if the response is from the author who typed the command
const filter = (response) => response.author.id === message.author.id;
const maxWait = 10000; // in ms
// set up a message collector to check if there are any responses
const collector = message.channel.createMessageCollector(filter, {
// set up the max wait time the collector runs
// it's optional though
time: maxWait,
});
// fires when a response is collected
collector.on('collect', (response) => {
if (response.content === '1') {
return message.channel.send(casino);
}
if (response.content === '2') {
return message.channel.send(quote);
}
if (response.content === '3') {
return message.channel.send(mod);
}
});
// fires when the collector is finished collecting
collector.on('end', (collected, reason) => {
// only send a message when the "end" event fires because of timeout
if (reason !== 'time') return;
message.channel.send(
`Okay, ${message.author}, I'm bored and I can't wait any longer. If you want to see the menu again, type \`!setup\` again`,
);
});
},
};
关于javascript - 那么如何在请求时从另一个文件运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67649235/