0

I recently made a Discord bot for my Discord Server which basically counts up a number when typing in a specific message into a chat. For this I also made a leaderboard to see who has the highest score. Until recently the bot worked fine but now it always crashes when trying to get the leaderboard and in the console I get the error bellow.

TypeError: Cannot read property 'tag' of undefined
    at Client.<anonymous> (C:\Users\***\Desktop\DiscordBot\bot.js:71:61)
    at Client.emit (events.js:219:5)
    at MessageCreateAction.handle (C:\Users\***\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\***\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\***\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\***\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\***\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\***\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:219:5)
    at Receiver.receiverOnMessage (C:\Users\***\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:797:20)

I already made some research and found out that "tag" is used to print the name of the discord user on the leaderboard instead of his ID. The problem now is that I can't just remove "tag" because then you only see the ID in the leaderboard and no one knows his ID.

Here is my code if somebody needs it.

    if (!table['count(*)']) {
        // If the table isn't there, create it and setup the database correctly.
        sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER);").run();
        // Ensure that the "id" row is always unique and indexed.
        sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
        sql.pragma("synchronous = 1");
        sql.pragma("journal_mode = wal");
    }
      // And then we have two prepared statements to get and set the score data.
    client.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
    client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points) VALUES (@id, @user, @guild, @points);");
    console.log('Ready!');
});

client.on('message', message => {
    if (message.author.bot) return;
    if (message.guild) {
        if (message.content === '!aulaner') {
            let score = client.getScore.get(message.author.id, message.guild.id);
            if (!score) {
                score = {
                    id: `${message.guild.id}-${message.author.id}`,
                    user: message.author.id,
                    guild: message.guild.id,
                    points: 0,
            }
        }
            score.points++;
            
            
            //const user_rank = sql.prepare("SELECT * FROM (SELECT *, RANK() OVER(ORDER BY points DESC) AS rank FROM scores) WHERE user = ?")
            
            //const user_rank = sql.prepare("SELECT * FROM scores ORDER BY points WHERE user = ?")
            
            client.setScore.run(score);
            //var x = score.points % 24
            //if (x != 0){
                message.reply('Sie haben ihr ' +score.points+ '. Aulaner geöffnet <:aulanergrab:739469308165619802>');
            //} else if (x == 0) {
            //  score.kisten++;
            //  message.reply('Glückwunsch du hast deinen ' +score.kisten+ '. Kasten Aulaner Ezi geöffnet! :partying_face:');
            //}
            console.log(score);
            //console.log(score.rowid);
            //console.log(rank);
            //console.log(user_rank.rank);
            
        }
        if(message.content === "!aulanerboard") {
            const top10 = sql.prepare("SELECT * FROM scores WHERE guild = ? ORDER BY points DESC LIMIT 10;").all(message.guild.id);

            // Now shake it and show it! (as a nice embed, too!)
            const embed = new Discord.MessageEmbed()
                .setTitle("Aulanerboard")
                .setAuthor(client.user.username, client.user.avatarURL())
                .setDescription("Unsere Top 10 Aulaner säufer")
                .setColor(0x00AE86);

            for(const data of top10) {
            console.log(data);
            embed.addFields({ name: client.users.cache.get(data.user).tag, value: `${data.points} Aulaner geöffnet` });
            }   
            return message.channel.send({embed});
        }
        
        if(message.content === "!aulaneropened") {
            let score = client.getScore.get(message.author.id, message.guild.id);
            if(score == null){
                message.reply('Sie haben bis jetzt noch kein Aulaner geöffnet');
            } else {
                message.reply('Sie haben bis jetzt ' +score.points+ ' Aulaner geöffnet.');
            }
        }
    }
});

I hope somebody can help me

D3ltafl3x
  • 11
  • 3

0 Answers0