【发布时间】:2021-05-09 18:37:18
【问题描述】:
我正在尝试在 Discord 中制作关卡机器人。基本上,我想做的是为服务器中的每个人分配一个“pts”变量。我的机器人中有一个事件处理程序和命令处理程序。
这是我的 event_handler.js:
const fs = require('fs');
module.exports = (client, Discord) =>{
const load_dir = (dirs) =>{
const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'));
for (const file of event_files) {
const event = require(`../events/${dirs}/${file}`);
const event_name = file.split('.')[0];
client.on(event_name, event.bind(null, Discord, client));
}
}
['client', 'guild'].forEach(e => load_dir(e));
}
这是我的 command_handler.js:
const fs = require('fs');
module.exports = (client, Discord) =>{
const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of command_files){
const command = require(`../commands/${file}`);
if(command.name){
client.commands.set(command.name, command);
} else {
continue;
}
}
}
我还有一个 guildMemberAdd 事件,它有一个 guildMember 变量,向 guildMember 添加一个成员角色,并说一条欢迎消息。在这里(它被称为 guildMemberAdd.js):
module.exports = (Discord, client, guildMember) =>{
let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'Member');
guildMember.roles.add(welcomeRole);
guildMember.guild.channels.cache.get(MYCHANNELID).send(`Welcome, <@${guildMember.user.id}>, to our server! ... Check out the rules-cmd channel!`);
}
我在想也许当有人加入服务器时,我可以给他们一个变量。例如:
module.exports = (Discord, client, guildMember) =>{
var pts = new guildMember;
let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'Member');
guildMember.roles.add(welcomeRole);
guildMember.guild.channels.cache.get(MYCHANNELID).send(`Welcome, <@${guildMember.user.id}>, to our server! ... Check out the rules-cmd channel!`);
}
...但我对 javaScript 真的很不好,而且我知道,这肯定不会起作用。
有没有办法做到这一点?如果有,我该如何根据命令更改它? (我的命令存储在另一个文件夹中。)
如果你们需要,这是我的……嗯……文件和文件夹的排序。我不太确定那叫什么。
非常感谢您的帮助!!
我认为你们不需要节点模块文件,对吧?如果你这样做,请发表评论。
我的编辑 1(由 TYRCNEX 撰写):有些人看到这个问题可能会说:“应该还有其他类似的 Stack Overflow 问题,对吧?”就在这里。但是,如上所述,我是 JS 的 COMPLETE 初学者。我可以花一年的时间来学习 js 的基础知识……
...但我手头没有那么多时间。我的SPECIFIC问题需要帮助,但我无法真正理解其他答案。这就是我问这个问题的原因。谢谢。
【问题讨论】:
标签: javascript node.js variables discord.js