1

I am creating a node.js bot to track how long users have been on a game for. When I run this code sometimes the bot throws an error UnhandledPromiseRejectionWarning: MongoError: Cannot use a session that has ended

I can't see unreachable code or a session that is disconnected before everything has a chance to run. Here is my entire code for the function on a discord presence update:

const mongo = require('./mongo');
const exposeSchema = require('./schemas/expose-schema');
const { general } = require('./config.json');


module.exports = async client => {
    client.on('presenceUpdate', async (oldPresence, newPresence) => {
        const username = newPresence.user.username;
        const guild = newPresence.guild.id;
        console.log('Presence update detected:');
        console.log(`Guild ID: ${guild} \nUser ID: ${newPresence.user.id} \nUsername: ${username}`);
        await mongo().then(async mongoose => {
            try {
                await exposeSchema.where({ guildid: guild, username: username }).findOne(async function(err, sent) {
                    if (err) {
                        console.log(err);
                    }
                    if (sent == null) {
                        console.log('Writing to database');
                        await exposeSchema({
                            guildid: guild,
                            username: username,
                            sent: false,
                        }).save();
                    }
                    else if (sent.sent == false) {
                        const act = await newPresence.activities.find(activity => activity.timestamps != null);
                        if (act) {
                            const n = new Date();
                            const g = act.timestamps.start;
                            const hours = Math.abs(n - g) / 36e5;
                            if (hours >= 4) {
                                await exposeSchema.findOneAndUpdate({
                                    guildid: guild,
                                    username: username,
                                }, {
                                    guildid: guild,
                                    username: username,
                                    sent: true,
                                }, {
                                    upsert: true,
                                });
                                console.log(`${newPresence.user.username} has been playing ${act.name} for ${Math.round(hours)} hours, time to wrap it the fuck up and go outside or get some sleep.`);
                                if (newPresence.member.nickname == null) {
                                    await client.channels.cache.get(general).send(`${newPresence.user.username} has been playing ${act.name} for ${Math.round((hours) * 100) / 100} hours, time to wrap it the fuck up and go outside or get some sleep.`);
                                }
                                else {
                                    await client.channels.cache.get(general).send(`${newPresence.user.nickname} has been playing ${act.name} for ${Math.round((hours) * 100) / 100} hours, time to wrap it the fuck up and go outside or get some sleep.`);
                                }
                            }
                        }
                    }
                });
            }
            finally {
                mongoose.connection.close();
            }
        });
    });
};

0 Answers0