如果在打开机器人程序时向服务器创建新的快速邀请,则会发生错误。在其他情况下,它会起作用
const invites = {};
const wait = require('util').promisify(setTimeout);
client.on('ready', () => {
wait(1000);
g.fetchInvites().then(guildInvites => {
invites[g.id] = guildInvites;
});
});
});
client.on('guildMemberAdd', member => {
member.guild.fetchInvites().then(guildInvites => {
const ei = invites[member.guild.id];
invites[member.guild.id] = guildInvites;
const invite = guildInvites.find(i => ei.get(i.code).uses < i.uses);
const inviter = client.users.get(invite.inviter.id);
const logChannel = member.guild.channels.find(channel => channel.name === "join-logs");
logChannel.send(`${member.user.tag} joined using invite code ${invite.code} from ${inviter.tag}. Invite was used ${invite.uses} times since its creation.`);
});
});
错误:
2019-07-07T09:49:20.363359+00:00 app[worker.1]: Unhandled Rejection:
2019-07-07T09:49:20.363377+00:00 app[worker.1]: TypeError: Cannot read property 'uses' of undefined
2019-07-07T09:49:20.363378+00:00 app[worker.1]: at guildInvites.find.i (./bot.js:398:57)
2019-07-07T09:49:20.363380+00:00 app[worker.1]: at Map.find (./node_modules/discord.js/src/util/Collection.js:160:13)
2019-07-07T09:49:20.363381+00:00 app[worker.1]: at member.guild.fetchInvites.then.guildInvites (./bot.js:398:33)
2019-07-07T09:49:20.363382+00:00 app[worker.1]: at process._tickCallback (internal/process/next_tick.js:68:7)
398截止日期
const invite = guildInvites.find(i => ei.get(i.code).uses < i.uses);
答案 0 :(得分:0)
使用的邀请是新邀请,尚未在缓存中。但是,ei.get(i.code).uses
假定它为真,并尝试在它不存在时使用它的属性。
此修订的谓词功能将返回未缓存的邀请,或使用量增加的邀请。
const invite = guildInvites.find(i => !ei.get(i.code) || ei.get(i.code).uses < i.uses);