use mysql values for export const

Hey quick question. I got my values of the embed (title, color, desc, footer, etc) in my mysql database. I want them in my embed.js for all the new EmbedBuilder(). After that I want to export my Builder to my interactionCreate.js or messageCreate.js. How is that possible? Can you may send me some advice or documentation? :D
45 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Kinect3000
Kinect30003y ago
Make a function that gets the data from the db, puts it into the builder and returns it
Kilian
KilianOP3y ago
Like this:
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (err, testembed) => {
if (err) {
console.log(err);
}

let testembed_color = testembed[0].color
let testembed_title = testembed[0].title
let testembed_description = testembed[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

return test_embed;


});
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (err, testembed) => {
if (err) {
console.log(err);
}

let testembed_color = testembed[0].color
let testembed_title = testembed[0].title
let testembed_description = testembed[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

return test_embed;


});
?
Kinect3000
Kinect30003y ago
Return doesn’t work here bc ur in a nested callback
Kilian
KilianOP3y ago
oh yea I see
Kinect3000
Kinect30003y ago
You would either have to make a new Promise constructor or just use the promise wrapper of MySQL
Kilian
KilianOP3y ago
okay that's what I wanted to hear Let me try this, thanks Hey, I searched a lot and, if I use an promise I can't export const test_embed = new EmbedBuilder(), can I?
Kinect3000
Kinect30003y ago
No Well, you prob could w/ es6 import/export
Kilian
KilianOP3y ago
But how should I deal with this then? I need to export it :D tried doesn't work either
Kinect3000
Kinect30003y ago
You can’t use export in callbacks tho, it has to be top-level
Kilian
KilianOP3y ago
That sounds to heavy for me tho
Kinect3000
Kinect30003y ago
That’s why I just suggested to make a function that makes the embed
Kilian
KilianOP3y ago
Wait let me explain
Kinect3000
Kinect30003y ago
You can memoize it if you want to
Kilian
KilianOP3y ago
I have an file called embed.js there should all embed get builded for my other files like interactionCreate.js, messageCreate.js, etc. I worked for this with ES6 no problem worked fine. I worked without the ES6 module, also do the db query in my messageCreate.js (with the values of db) worked fine aswell. But to make my code much clearer I decided to move my db connection to my builder file. In this builder file I wanted to do what my problem is. :c And I guess that's not working as you said @kinect3000 One idea You know I have in the file messageCreate.js the
export const name = 'messageCreate';
export async function execute(message) {

}
export const name = 'messageCreate';
export async function execute(message) {

}
Can I export the function from the db into this? Or may loadScript the embed.js-file?
Kinect3000
Kinect30003y ago
Can’t you do the same w/ putting it on the top-level Or you said doesn’t work in non-es6
Kilian
KilianOP3y ago
Bro, be careful I'm an beginner I won't get this top level things 💀
Kinect3000
Kinect30003y ago
It’s easier to just use mysql2/promise
Kilian
KilianOP3y ago
okay yes
Kinect3000
Kinect30003y ago
Do you know what top-level means?
Kilian
KilianOP3y ago
Sadly no, I heard of that while I was researching but ignored it.
Kinect3000
Kinect30003y ago
// myFile.cjs

// top-level (of file)
console.log("I’m king of the world")

function smth() {
// inside function
}
// myFile.cjs

// top-level (of file)
console.log("I’m king of the world")

function smth() {
// inside function
}
Kilian
KilianOP3y ago
Ah does that mean it has to be on the top of the file?
Kinect3000
Kinect30003y ago
Not necessarily
Kilian
KilianOP3y ago
oh KEKW
Kinect3000
Kinect30003y ago
It has to due w/ scopes Basically any code that isn’t inside of a function
Kilian
KilianOP3y ago
Can this work: messageCreate.js
loadScript('../builders/embeds.js')

export const name = 'messageCreate';
export async function execute(message) {
if (message.content.includes("kilian")) {
message.reply({embeds: [test_embed]});
}
}
loadScript('../builders/embeds.js')

export const name = 'messageCreate';
export async function execute(message) {
if (message.content.includes("kilian")) {
message.reply({embeds: [test_embed]});
}
}
in embed.js
import { createConnection } from 'mysql2';
import { EmbedBuilder } from 'discord.js';

const dbaccess = createConnection({
host: '...', user: '...', password: '...', database: '....'
})

function execute(message) {
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (err, testembed) => {
if (err) {
console.log(err);
}

let testembed_color = testembed[0].color
let testembed_title = testembed[0].title
let testembed_description = testembed[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

return test_embed;


});
}
import { createConnection } from 'mysql2';
import { EmbedBuilder } from 'discord.js';

const dbaccess = createConnection({
host: '...', user: '...', password: '...', database: '....'
})

function execute(message) {
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (err, testembed) => {
if (err) {
console.log(err);
}

let testembed_color = testembed[0].color
let testembed_title = testembed[0].title
let testembed_description = testembed[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

return test_embed;


});
}
I guess with some changes but do you get my idea? Or is this shit xd Or could this work in an promise?
Kinect3000
Kinect30003y ago
GitHub
node-mysql2/Promise-Wrapper.md at master · sidorares/node-mysql2
:zap: fast mysqljs/mysql compatible mysql driver for node.js - node-mysql2/Promise-Wrapper.md at master · sidorares/node-mysql2
Kinect3000
Kinect30003y ago
It’s much easier to just use async/await Otherwise, you have to make ur own Promise
d.js docs
d.js docs3y ago
mdn Promise The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
Kilian
KilianOP3y ago
Bruh that's an level to high for me
Kinect3000
Kinect30003y ago
Async/await is pretty easy imo
Kilian
KilianOP3y ago
I guess I know what I do now: learn more about java script
Kinect3000
Kinect30003y ago
const data = await myPromise Where data is the result of myPromise
Kilian
KilianOP3y ago
sure, it is, but it's not for my case
Kinect3000
Kinect30003y ago
You have to use the promise wrapper for MySQL
Kilian
KilianOP3y ago
oh, ok wait that explain something whats what I never did read lmao can I use this aswell: export const data = await myPromise?
Kinect3000
Kinect30003y ago
Yea Only w/ esm tho
Kilian
KilianOP3y ago
esm is activated for sure kinda dumb question, how do I define my promise ? with mysql2?
Kinect3000
Kinect30003y ago
Are you not using mysql2?
Kilian
KilianOP3y ago
I do
import { createConnection } from 'mysql2';
import { EmbedBuilder } from 'discord.js';

const dbaccess = createConnection({
host: '...', user: '...', password: '...', database: '....'
})

export const test = await testPromise(() => {
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (err, testembed) => {
if (err) {
console.log(err);
}

let testembed_color = testembed[0].color
let testembed_title = testembed[0].title
let testembed_description = testembed[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

return test_embed;

});
})
import { createConnection } from 'mysql2';
import { EmbedBuilder } from 'discord.js';

const dbaccess = createConnection({
host: '...', user: '...', password: '...', database: '....'
})

export const test = await testPromise(() => {
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (err, testembed) => {
if (err) {
console.log(err);
}

let testembed_color = testembed[0].color
let testembed_title = testembed[0].title
let testembed_description = testembed[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

return test_embed;

});
})
Kinect3000
Kinect30003y ago
You use the code I linked to use promises and async/await instead of callback-style promises
Kilian
KilianOP3y ago
I did it wrong? Didn't you say that I should use async/await ?
Kinect3000
Kinect30003y ago
Not sure where you got testPromise from Ur db.query is still using callback-style promise
Kilian
KilianOP3y ago
oh I though it could be an string, my bad Okay, my plan did change. I do not need to export anymore because, what if I need any interactionvalue like ${interaction.id}. So I moved query into the messageCreate.js. It works fine now. But maybe you can tell me how I get like the message.author.id into my query? Code:
if (message.content.includes("kilian")) {
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (error, success) => {
if (error) {
console.log(error);
}

let testembed_color = success[0].color
let testembed_title = success[0].title
let testembed_description = success[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

message.reply({embeds: [test_embed]})

});

}
if (message.content.includes("kilian")) {
dbaccess.query(`SELECT * FROM embeds WHERE name = 'test_embed'`, (error, success) => {
if (error) {
console.log(error);
}

let testembed_color = success[0].color
let testembed_title = success[0].title
let testembed_description = success[0].description

const test_embed = new EmbedBuilder()
.setColor(testembed_color)
.setTitle(testembed_title)
.setDescription(testembed_description);

message.reply({embeds: [test_embed]})

});

}
I guess I've to work with promise aswell right?
Want results from more Discord servers?
Add your server