Skip to content

Unexpected token error in the discord.js bot’s code

So i was making a logger that basically logs all the nick role or username changes etc using discord.js documentation. Someone helped me to get this code but when I tried running it, it gives me an error. The code:

    const Discord = require("discord.js");
    const bot = Discord.Client();
    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;
            }
        }
    
    });
    
    bot.login("token :) ");

I am really new with js so it might be a really stupid error

The error:

        const token = this.client.token ?? this.client.accessToken;
        
    SyntaxError: Unexpected token '?'                                 ^ 

Can someone help me fix it ?

Answer

You need to update node to v16.6 or later. See the discord.js v13 requirements here: https://discord.js.org/#/docs/main/stable/general/welcome

Node.js 16.6.0 or newer is required.