不和谐机器人记录仪

时间:2020-08-04 23:24:06

标签: javascript node.js api discord discord.js

有人可以帮助我使该脚本正常工作吗?我猜它还没有更新到最新的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;
        }
    }

});

2 个答案:

答案 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)

有几个问题:

  1. guildMemberUpdate事件仅传递两个参数: oldMember newMember 。它没有传递代码中显示的行会参数。
  2. 由于未传递行会参数,因此您需要使用guild更改对newMember.guild的所有引用。
  3. guild.channels现在要求在末尾附加.cacheguild.channels.cache。有关更多信息,请阅读Discord.js migration guide
  4. Discord.Collection() .find()方法现在仅接受一个函数。
  5. .sendMessage()需要替换为.send()
  6. .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;
        }
    }

});