不和谐机器人记录器

Posted

技术标签:

【中文标题】不和谐机器人记录器【英文标题】:Discord Bot Logger 【发布时间】:2020-11-25 02:37:19 【问题描述】:

有人可以帮我让那个脚本工作吗?我猜它没有更新到最新的 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;
        
    

);

【问题讨论】:

【参考方案1】:

以下行导致您的程序出错:

    var log = guild.channels.find('name', CHANNEL);

在discord.js v12中,你需要使用一个函数并使用.cache.

这里是更正的版本:

    var log = guild.channels.cache.find(c => c.name == CHANNEL);

【讨论】:

【参考方案2】:

有几个问题:

    guildMemberUpdate 事件仅传递两个参数:oldMembernewMember。如您的代码所示,它不会传递 guild 参数。 由于没有传递guild参数,您需要将所有对guild的引用更改为newMember.guildguild.channels 现在需要在末尾附加 .cacheguild.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;
        
    

);

【讨论】:

现在我得到了这个:TypeError: log.sendMessage is not a function 刚刚将其更改为 client.channels.cache.get('MY CHANNEL ID HERE').send 并且似乎工作正常。 但是现在我遇到了这两个问题:newMember.user.displayAvatarURL != oldMember.user.displayAvatarURLnewMember.user != oldMember.user 检查头像更改和登录更改似乎不起作用。昵称更改工作正常。这是最新的吗? 谢谢,log.send 运行良好。但是我仍然对头像和用户名更改公告有问题。这是一张照片:ibb.co/71GfTjZ 第一条消息运行良好,但接下来的两条消息的头像和用户名发生了变化。我猜它不被识别并且作为“未知= 0”工作 @FOX 我被难住了。

以上是关于不和谐机器人记录器的主要内容,如果未能解决你的问题,请参考以下文章

试图用嵌入制作一个不和谐的日志记录机器人

即使在不和谐中明确检查历史记录后仍重复消息

我的不和谐机器人没有上线 - 不和谐 v13

让您的不和谐机器人保存您发送的消息

不和谐.JS |如何使用 **channel.client** 功能而不显示机器人客户端信息?

不和谐.js | TypeError:无法读取未定义的属性“0”