Home > Software design >  I want to read the value of a specific table after user_id
I want to read the value of a specific table after user_id

Time:08-31

I want to read the value of a specific table after user_id.

(I use discord.js v14) Please see Database picture so that you understand what I mean.

Databse picture

Again, I want to get the invite_link value by entering only user_id. Is it even possible?

This is my actually code:

const { SlashCommandBuilder } = require("@discordjs/builders")
const Discord = require("discord.js")
const { QuickDB } = require("quick.db");
const db2 = new QuickDB();
const mongodb = require("mongodb");

const username = encodeURIComponent('censored');
const password = encodeURIComponent("censored")
const dbname = 'censored';
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const clusterUrl = "censored";
const authMechanism = "DEFAULT";
const url = `mongodb://${username}:${password}@${clusterUrl}/${username}`;
const client = new mongodb.MongoClient(url);    
const db = client.db(dbname)
client.connect().then(() => {
    console.log("Data: Connected")
}).catch(err => console.error(err)) 


module.exports = { 
    data: new SlashCommandBuilder()
        .setName("invite")
        .setDescription("Create invite to download server"),

    execute: async function(interaction, guilds, client, invites) {
        if(interaction instanceof Discord.CommandInteraction) {


const guild = interaction.client.guilds.cache.get(`1013773663910232184`)
if(await db.collection('blacklist').findOne({
    user_id: `${interaction.member.id}`
 })){
    return interaction.reply({ content: `You cant acces to this command! (Error code: 502)`, ephemeral: true})
 }

 const query = { user_id: `${interaction.member.id}` };

const options = {
   projection: { invite_link: 1 } //only return invite link
};

const inviteLink2 = await db.collection('invites').findOne(query, options);

 if(await db.collection('invites').findOne({
    user_id: `${interaction.member.id}`
 })) {
    return interaction.reply({ content: `You always have invite code! This is your invite code: ${inviteLink2}`})
 }

 




        const invitecode = await guild.invites.create('1013793499189100574', { maxAge: 18000, maxUses: 1} );
        db.collection('invites').insertOne({
            user_id: `${interaction.member.id}`,
            invite_link: `${invitecode.url}`
     })
        interaction.reply({ content: `This is your invite: ${invitecode.url} , DO NOT SEND THIS LINK TO ANYONE. Invite expires in 3 minutes.`, ephemeral: true})
        
        


console.log(`${invitecodegetindatabase}`)

        }   

        }
    }

THIS IS EDITED CODE

CodePudding user response:

Assuming the document is added to the database correctly

const query = { user_id: `${interaction.member.id}` };

const options = {
   projection: { invite_link: 1 } //only return invite link
};

const inviteLink = await db.collection('invites').findOne(query, options);
console.log(inviteLink);

Please read the mongoDB docs thoroughly, it will save lots of time.

Also, since you're adding the document to the database after you make the request it won't exist. Move the query execution to a line after the insertOne call.

  • Related