有人可以帮助我使该脚本正常工作吗?我猜它还没有更新到最新的Discord API。
我只需要三件事:更改昵称,更改用户名和头像的记录器。
当新人刚进来时,已经使我自己成为工作记录员,并且换了角色,并收到欢迎消息,但是这三个人似乎被打破了。
bot.on('guildMemberUpdate', function(guild, oldMember, newMember) {
//declare changes
var Changes = {
unknown: 0,
addedRole: 1,
removedRole: 2,
username: 3,
nickname: 4,
avatar: 5
};
var change = Changes.unknown;
//check if username changed
if (newMember.user.username != oldMember.user.username)
change = Changes.username;
//check if nickname changed
if (newMember.nickname != oldMember.nickname)
change = Changes.nickname;
//check if avatar changed
if (newMember.user.avatarURL != oldMember.user.avatarURL)
change = Changes.avatar;
//log to console
switch (change) {
case Changes.unknown:
console.log('[' + guild.name + '][UPDUSR] ' + newMember.user.username + '#' + newMember.user.discriminator);
break;
case Changes.username:
console.log('[' + guild.name + '][UPDUSRNM] ' + oldMember.user.username + '#' + oldMember.user.discriminator +
' is now ' + newMember.user.username + '#' + newMember.user.discriminator);
break;
case Changes.nickname:
console.log('[' + guild.name + '][UPDUSRNK] ' + newMember.user.username + '#' + newMember.user.discriminator +
(oldMember.nickname != null ? ' (' + oldMember.nickname + ')' : '') +
(newMember.nickname != null ? ' is now ' + newMember.nickname : ' no longer has a nickname.'));
break;
case Changes.avatar:
console.log('[' + guild.name + '][UPDAVT] ' + newMember.user.username + '#' + newMember.user.discriminator);
break;
}
//post in the guild's log channel
var log = guild.channels.find('name', CHANNEL);
if (log != null) {
switch (change) {
case Changes.unknown:
log.sendMessage('**[User Update]** ' + newMember);
break;
case Changes.username:
log.sendMessage('**[User Username Changed]** ' + newMember + ': Username changed from ' +
oldMember.user.username + '#' + oldMember.user.discriminator + ' to ' +
newMember.user.username + '#' + newMember.user.discriminator);
break;
case Changes.nickname:
log.sendMessage('**[User Nickname Changed]** ' + newMember + ': ' +
(oldMember.nickname != null ? 'Changed nickname from ' + oldMember.nickname +
+newMember.nickname : 'Set nickname') + ' to ' +
(newMember.nickname != null ? newMember.nickname + '.' : 'original username.'));
break;
case Changes.avatar:
log.sendMessage('**[User Avatar Changed]** ' + newMember);
break;
}
}
});
答案 0 :(得分:2)
以下行导致您的程序出错:
var log = guild.channels.find('name', CHANNEL);
在discord.js v12中,您需要使用一个函数并使用.cache.
这是更正的版本:
var log = guild.channels.cache.find(c => c.name == CHANNEL);
答案 1 :(得分:0)
有几个问题:
guildMemberUpdate
事件仅传递两个参数: oldMember 和 newMember 。它没有传递代码中显示的行会参数。guild
更改对newMember.guild
的所有引用。guild.channels
现在要求在末尾附加.cache
:guild.channels.cache
。有关更多信息,请阅读Discord.js migration guide。Discord.Collection()
.find()
方法现在仅接受一个函数。.sendMessage()
需要替换为.send()
。.avatarURL
需要替换为.displayAvatarURL()
。 侧面说明:为什么不使用newMember.user.tag
而不是newMember.user.username + '#' + newMember.user.discriminator
?
这是修改后的代码:
bot.on('guildMemberUpdate', function(oldMember, newMember) {
//declare changes
var Changes = {
unknown: 0,
addedRole: 1,
removedRole: 2,
username: 3,
nickname: 4,
avatar: 5
};
var change = Changes.unknown;
//check if username changed
if (newMember.user.username !== oldMember.user.username)
change = Changes.username;
//check if nickname changed
if (newMember.nickname !== oldMember.nickname)
change = Changes.nickname;
//check if avatar changed
if (newMember.user.displayAvatarURL() !== oldMember.user.displayAvatarURL())
change = Changes.avatar;
//log to console
switch (change) {
case Changes.unknown:
console.log('[' + newMember.guild.name + '][UPDUSR] ' + newMember.user.username + '#' + newMember.user.discriminator);
break;
case Changes.username:
console.log('[' + newMember.guild.name + '][UPDUSRNM] ' + oldMember.user.username + '#' + oldMember.user.discriminator +
' is now ' + newMember.user.username + '#' + newMember.user.discriminator);
break;
case Changes.nickname:
console.log('[' + newMember.guild.name + '][UPDUSRNK] ' + newMember.user.username + '#' + newMember.user.discriminator +
(oldMember.nickname != null ? ' (' + oldMember.nickname + ')' : '') +
(newMember.nickname != null ? ' is now ' + newMember.nickname : ' no longer has a nickname.'));
break;
case Changes.avatar:
console.log('[' + newMember.guild.name + '][UPDAVT] ' + newMember.user.username + '#' + newMember.user.discriminator);
break;
}
//post in the guild's log channel
var log = newMember.guild.channels.cache.find(channel => channel.name === CHANNEL);
if (log) {
switch (change) {
case Changes.unknown:
log.send('**[User Update]** ' + newMember);
break;
case Changes.username:
log.send('**[User Username Changed]** ' + newMember + ': Username changed from ' +
oldMember.user.username + '#' + oldMember.user.discriminator + ' to ' +
newMember.user.username + '#' + newMember.user.discriminator);
break;
case Changes.nickname:
log.send('**[User Nickname Changed]** ' + newMember + ': ' +
(oldMember.nickname != null ? 'Changed nickname from ' + oldMember.nickname +
+newMember.nickname : 'Set nickname') + ' to ' +
(newMember.nickname != null ? newMember.nickname + '.' : 'original username.'));
break;
case Changes.avatar:
log.send('**[User Avatar Changed]** ' + newMember);
break;
}
}
});