megasql.db

0.3.1 • Public • Published

megasql.db - a Database for starters

Description: megasql.db is a database made by Ruben, it allows the user with a low/0 knowledge about databases to store values and add more interesting things in a database with an easy way!

Functions of megasql.db:

.fetch(userid, variable, valueIfNull(optional), newValue(optional)) - fetchs a variable and gets his value to a let, var, const function.

.get(userid, variable) - gets a variable value from given user id.

.add(userid, variable, value) - adds a value to given variable name and user id.

.subtract(userid, variable, value) - subtracts a value to given variable name and user id.

.cooldown(userid, command, time(ms)) - adds a cooldown to the command, you'll have to use a variable.

.sortData(variable) - returns all the values of a variable sortered

.set(userid, variable, value) - sets a new value for given variable name.

.condition(userid, variable, condition, number) - used to make a condition with a variable value, itll return true or false.

.removeCooldown(userid, command) - removes a cooldown from given command name and user id.

.addEvent(event, duration) - adds an event where event is the event name and duration is the duration of the event in milliseconds.

.getEvent(event, parse(optional)) - gets an event with given event name. If you set parse to true, it'll return the time in hours, minutes, seconds... If it isn't setted, it'll return the milliseconds left of that event.

.map(userid, ["var1", "var2", ...], valueIfNull(optional)) - mainly used for load multiple variables from given user id and put them in an array, if valueIfNull is setted, variables with null values will be replaced with valueIfNull.

.setVars(userid, ["var1", "var2", ...], [0 , 50, ...]) - sets multiple variables to given values of chosen userid.

.addToVars(userid, ["var1", "var2", ...], [2 , 10, ...], valueIfNull(optional)) - add to multiple variables the given values of chosen userid.

.subtractToVars(userid, ["var1", "var2", ...], [5 , 13, ...], valueIfNull(optional)) - subtract to multiple variables the given values of chosen userid.

Examples using megasql.db:

Imagine we want to add a cooldown for a command, here we go:

client.on("message", async message => {
 
if (message.content.startsWith("!test")) {
 
let cooldown = await db.cooldown(message.author.id, "test", 60000)
 
if (cooldown == 0) {
 
message.channel.send("Cooldown Expired")
 
} else {
 
let time = ms(cooldown)
 
message.channel.send(\`${message.author.username} please wait ${time.seconds} seconds.\`)
}
}})

Or maybe we want to do a leaderboard:

client.on('message', async message => {
 
if (message.content.startsWith("!leaderboard")) {
 
let content = ``
 
let position = 1
 
let array = await db.sortData("money")
 
for (let i = 0;< array.length;i++) {
 
if (client.users.get(array[i].ID.split("_")[1])) {
 
let user = client.users.get(array[i].ID.split("_")[1]).username
 
content += `${position} - ${user} - Money: ${array[i].data}\n`
 }
}
message.channel.send("Top Money:\n" + content)
}})

What about a condition?

client.on('message', async message => {
 
if (message.content.startsWith('!condition')) {
 
let item = await db.condition(message.author.id, "messages", ">=", 100)
 
if (item == true) {
 
message.channel.send(message.author + ' nice! you have sent more than 100 messages!')
 
} else {
 
message.channel.send(message.author + " you don't have more than 100 messages sent.")
}
}})

What about an event:

client.on('message', async message => {
 
if (message.content.startsWith('!addEvent')) {
 
db.addEvent("event1", 120000) //2 minutes in milliseconds
  
message.channel.send("Successfully added an event!")
}})
 
//next command would be:
 
client.on('message', async message => {
 
if (message.content.startsWith('!getEvent')) {
 
let key = await db.getEvent("event1", true)
 
if (event1 == -1) return message.channel.send("There isn't any event right now.")
 
message.channel.send("Event duration: " + key)
}})
 

Now, let's gonna get multiple variable values:

client.on("message", async message => {
  
  if (message.content.startsWith("!map")) {
    
    let vars = await db.map(message.author.id, ["money", "messages", "health"], 0)
    //vars will contain 3 values, money, messages and health
    //if we want to get the messages value, we'll do vars[1]
    message.channel.send("Money: " + vars[0] + "\nMessages sent: " +  vars[1] + "\nHealth: " + vars[2])
    
}})

Isn't it easy? :D

Remember to use async because megasql.db takes some milliseconds to load the values.

If you need more help contact us on our Discord Server.

Package Sidebar

Install

npm i megasql.db

Weekly Downloads

0

Version

0.3.1

License

MIT

Unpacked Size

40.3 kB

Total Files

15

Last publish

Collaborators

  • ruben453