#database not working (sequelize) ?

8 messages · Page 1 of 1 (latest)

midnight cryptBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
white isle
#

messageCreate.js

const { Client, Message } = require('discord.js');
const { getLastMessageTimeFromDatabase, updateUserXPInDatabase } = require('../models/level');

/**
 * @param {Client} client
 * @param {Message} message
 */
module.exports = {
    name: 'messageCreate',
    execute: async (client, message) => {
        if (!message.author || message.author.bot) return;

        const xpMin = 10;
        const xpMax = 20;
        const xpCooldown = 5000;

        const lastMessageTime = await getLastMessageTimeFromDatabase(message.author.id);
        if (Date.now() - lastMessageTime < xpCooldown) return;

        const xpGain = Math.floor(Math.random() * (xpMax - xpMin + 1)) + xpMin;

        await updateUserXPInDatabase(message.author.id, xpGain);
    },
};
#

level.js

const Sequelize = require('sequelize');
const sequelize = require('../utils/database');

const Level = sequelize.define('level', {
    id : {
        type: Sequelize.STRING,
        primaryKey: true
    },
    guildId: {
        type: Sequelize.STRING,
        allowNull: false
    },
    userId: {
        type: Sequelize.STRING,
        allowNull: false
    },

    xp: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    level: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    rep: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    bio: {
        type: Sequelize.TEXT,
        allowNull: true
    }
}, {
    timestamps: false
})

module.exports = Level;```
#

database.js

const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'user', 'password', {
    dialect: 'sqlite',
    host: 'localhost',

    storage: 'database.sqlite',
    logging: false,
});

sequelize.authenticate()
    .then(() => {
        console.log('Database connection has been established successfully.');
    })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });

module.exports = sequelize;```
#

syncdb.js

const Level = require('./models/level');

Level.sync({alter: true});```
i have this error each time i send a message 
```powershell
node:events:492
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read properties of undefined (reading 'author')
...```
plus my commands ( ping, avatar ect ect ) not working anymore 
i think the event block the other event from working ?
I'm desperate; it's been several days, and I've been struggling to resolve this issue.
thorn crown
#

Your database stuff isnt djs related, #1081585952654360687

#

for your commands, show the code for them