Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Yumatsu/0dbe5b537d308c8900a3a59b456f9997 to your computer and use it in GitHub Desktop.
Save Yumatsu/0dbe5b537d308c8900a3a59b456f9997 to your computer and use it in GitHub Desktop.
Código do tutorial: https://youtu.be/qaArXO_W8kQ - Banco de dados LowDB
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('banco.json')
const db = low(adapter)
//criar
db.set('servidor2', [])
//postar
db.get('servidor1').push({
id: "0000000",
nick: "pedro",
avatar: "link.com/avatar.png"
})
//editar
db.get('servidor1').find({id: "1111111"}).assign({nick: "paulo novo"})
//buscar
let valor = db.get('servidor1').find({id: "1111111"}).value()
console.log(valor)
//apagar
db.get('servidor1').remove({id: "1111111"}).write()
const Discord = require("discord.js"); //baixar a lib discord.js
const client = new Discord.Client();
const config = require("./config.json");
const low = require('lowdb') //banco de dados
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('banco.json')
const db = low(adapter)
client.on("ready", () => {
console.log('Olá Mundo')
})
client.on("guildCreate", () => {
db.set(guild.id, []).write()
})
client.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
if(!message.content.startsWith(config.prefix)) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const comando = args.shift().toLowerCase();
if(comando === "criar") {
db.get(message.guild.id)
.push({
id: message.author.id,
nick: message.author.username,
avatar: message.author.displayAvatarURL
}).write()
message.channel.send('Perfil criado com sucesso!')
}
if(comando === "editar"){
if(!args[0])return message.channel.send('Você esqeceu do argumento ')
let [novonome] = args
db.get(message.guild.id)
.find({id: message.author.id}).assign({nick: novonome}).write()
message.channel.send('Perfil editado com sucesso!')
}
if(comando === "apagar"){
db.get(message.guild.id).remove({id: message.author.id}).write()
}
});
client.login(config.token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment