Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-Santana-j committed Apr 26, 2024
1 parent 2653fdd commit 81df4eb
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 65 deletions.
10 changes: 10 additions & 0 deletions Discord/discordIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ module.exports = (Discord, client) => {
try {
var DiscordServer = await client.guilds.cache.get(interaction.guildId);
var DiscordChannel = await DiscordServer.channels.cache.get(interaction.channelId)
let verifyPerms = await functions.verifyPermissions(interaction.user.id,interaction.guildId,Discord,client)
if (verifyPerms.error == true) {
return
}
if (verifyPerms.perms.owner == false && verifyPerms.perms.command == false) {
interaction.reply({content:'Você não tem permissão para executar comandos',ephemeral: true})
return
}


// interacao do botao de compra de um produto
if (interaction.customId.includes('comprar')) {
let server = await db.findOne({colecao:"servers",doc:interaction.guildId})
Expand Down
9 changes: 0 additions & 9 deletions Discord/updateProductMassage.js

This file was deleted.

2 changes: 1 addition & 1 deletion config/web-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
},
port: process.env.PORT || 3000,
// serviceAccount: JSON.parse(process.env.SERVICEACCOUNT),
serviceAccount: require('./fireabse.json'),
serviceAccount: require('./firebase.json'),
secret:process.env.CLIENTSECRET,
clientId:process.env.CLIENTID,
redirect:process.env.REDIRECTURL,
Expand Down
102 changes: 101 additions & 1 deletion functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,115 @@ const axios = require('axios')
const sharp = require('sharp');
const fs = require('fs');
const stripe = require('stripe')(require('./config/web-config').stripe);
const db = require('./Firebase/models')
const db = require('./Firebase/models');

module.exports = {
verifyPermissions: async (user, server, Discord, client) => {
try {
let serverDB = await db.findOne({ colecao: "servers", doc: server })
if (!serverDB) {
return { error: true, err: "server not found" };
}
const guild = await client.guilds.cache.get(server);
if (!guild) {
return { error: true, err: "server not found" };
}

const member = await guild.members.cache.get(user);

if (!member) {
return { error: true, err: "member not found" };
}

if (guild.ownerId == member.user.id) {
return {
error: false, perms: {
owner: true,
botEdit: true,
paymentEdit: true,
commands: true,
commandsAllChannel: true,
}
}
}
if (serverDB.permissions) {


const memberRoles = await member.roles.cache;

if (memberRoles.size > 1) {
const UserRolesPerms = await serverDB.permissions.filter(role => memberRoles.has(role.id));
if (UserRolesPerms.length > 0) {
let totalPerms = {}
await UserRolesPerms.forEach((element) => {
let perms = element.perms
if (perms.botEdit == true) {
totalPerms.botEdit = true
}
if (perms.paymentEdit == true) {
totalPerms.paymentEdit = true
}
if (perms.commands == true) {
totalPerms.commands = true
}
if (perms.commandsAllChannel == true) {
totalPerms.commandsAllChannel = true
}
})
if (!('botEdit' in totalPerms)) {
totalPerms.botEdit = false
}
if (!('paymentEdit' in totalPerms)) {
totalPerms.paymentEdit = false
}
if (!('commands' in totalPerms)) {
totalPerms.commands = false
}
if (!('commandsAllChannel' in totalPerms)) {
totalPerms.commandsAllChannel = false
}
totalPerms.owner = false
return { error: false, perms: totalPerms }
} else {
return {
error: false, perms: {
botEdit: true,
paymentEdit: false,
commands: true,
commandsAllChannel: false,
owner: false
}
}
}
} else {
console.log(`${member.user.username} não possui cargos.`);
return { error: true, err: "user not roles" };
}
} else {
return {
error: false, perms: {
botEdit: true,
paymentEdit: false,
commands: true,
commandsAllChannel: false,
owner: false
}
}
}
} catch (error) {
console.log(error);
return { error: true, err: error }
}
},
subscriptionStatus: async (req, res, next) => {
if (!req.params.id || !req.session.uid) {
res.redirect('/')
return
}
let server = await db.findOne({ colecao: "servers", doc: req.params.id })
if ("vitalicio" in server && server.vitalicio == true) {
next()
}
if (server) {
try {
const assinatura = await stripe.subscriptions.retrieve(server.subscription);
Expand Down
137 changes: 129 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ app.get('/auth/callback', async (req, res) => {
...headers
}
}).then((res) => { return res.data }).catch((err) => console.error(err));

await db.create('users', userResponse.id, {
id: userResponse.id,
username: userResponse.username,
Expand Down Expand Up @@ -259,6 +258,16 @@ app.get('/server/:id', functions.subscriptionStatus, async (req, res) => {
return
}

let verifyPerms = await functions.verifyPermissions(user.id, server.id, Discord, client)
if (verifyPerms.error == true) {
res.redirect('/dashboard')
return
}
if ('botConfig' in verifyPerms.perms && verifyPerms.perms.botEdit == false) {
res.redirect('/dashboard?botedit=false')
return
}

let analytics = await db.findOne({ colecao: "analytics", doc: req.params.id })

let comprasConcluidas = JSON.stringify(await functions.getDatesLast7Days(analytics["vendas completas"], functions.formatDate))
Expand All @@ -268,9 +277,6 @@ app.get('/server/:id', functions.subscriptionStatus, async (req, res) => {
})





app.get('/server/sales/:id', functions.subscriptionStatus, async (req, res) => {
let serverID = req.params.id
let user = await db.findOne({ colecao: 'users', doc: req.session.uid })
Expand All @@ -282,6 +288,16 @@ app.get('/server/sales/:id', functions.subscriptionStatus, async (req, res) => {
res.redirect('/dashboard')
return
}
let verifyPerms = await functions.verifyPermissions(user.id, server.id, Discord, client)
if (verifyPerms.error == true) {
res.redirect('/dashboard')
return
}

if ('botConfig' in verifyPerms.perms && verifyPerms.perms.botConfig == false) {
res.redirect(`/server/${serverID}`)
return
}
let bankData = server.bankData ? server.bankData : null

const guilds = client.guilds.cache;
Expand All @@ -295,7 +311,7 @@ app.get('/server/sales/:id', functions.subscriptionStatus, async (req, res) => {

const textChannels = channels.filter(channel => channel.type === 0);

res.render('sales', { host: `${webConfig.host}`, bankData: bankData, user: user, server: server, channels: textChannels, formatarMoeda: functions.formatarMoeda })
res.render('sales', { perms: verifyPerms.perms, host: `${webConfig.host}`, bankData: bankData, user: user, server: server, channels: textChannels, formatarMoeda: functions.formatarMoeda })
})


Expand Down Expand Up @@ -343,6 +359,7 @@ app.get('/server/analytics/:id', functions.subscriptionStatus, async (req, res)


app.get('/server/permissions/:id', functions.subscriptionStatus, async (req, res) => {

let serverID = req.params.id
let user = await db.findOne({ colecao: 'users', doc: req.session.uid })
let server = await db.findOne(({ colecao: 'servers', doc: serverID }))
Expand All @@ -351,7 +368,27 @@ app.get('/server/permissions/:id', functions.subscriptionStatus, async (req, res
return
}

res.render('perms', { host: `${webConfig.host}`, user: user, server: server })
let verifyPerms = await functions.verifyPermissions(user.id, server.id, Discord, client)
if (verifyPerms.error == true) {
res.redirect('/dashboard')
return
}

if (verifyPerms.error == false && verifyPerms.perms.owner == false) {
res.redirect(`/server/${serverID}`)
return
}

const guilds = client.guilds.cache;
const isBotInServer = guilds.has(serverID);
if (!isBotInServer) {
res.redirect(`/addbot/${serverID}`)
return
}
let guild = guilds.get(serverID)
let roles = guild.roles.cache
let rolesFilter = roles.filter(role => role.managed == false && role.mentionable == false)
res.render('perms', { host: `${webConfig.host}`, user: user, server: server, roles: JSON.stringify(rolesFilter) })
})


Expand Down Expand Up @@ -475,12 +512,96 @@ app.post('/config/change', async (req, res) => {
}
res.status(200).json({ success: true })
} catch (error) {
res.status(200).json({ success: false,data:'Erro ao salvar as configurações' })
res.status(200).json({ success: false, data: 'Erro ao salvar as configurações' })
console.log(error);
}
})


app.post('/perms/changeOne', async (req, res) => {
try {
let server = await db.findOne({ colecao: "servers", doc: req.body.serverID })
let roleID = await req.body.roleID
if (!server || !roleID) {
return
}
if (!('permissions' in server)) {
await db.update('servers', req.body.serverID, {
permissions: [
{
id:roleID,
perms: {
botEdit: true,
paymentEdit: false,
commands: true,
commandsAllChannel: false,
}
}
]
})
}
server = await db.findOne({ colecao: "servers", doc: req.body.serverID })

let permissions = server.permissions
let rolePermission = permissions.find(element=>element.id == roleID)
let index = permissions.findIndex(element=>element.id == roleID)

rolePermission.perms[await req.body.item] = await req.body.value

permissions[index] = rolePermission

console.log(permissions);
await db.update('servers', req.body.serverID, {
permissions: permissions
})

} catch (error) {

}
})

app.post('/perms/get', async (req, res) => {
try {
let server = await db.findOne({ colecao: "servers", doc: req.body.serverID })
let roleID = await req.body.roleID
if (!server || !roleID) {
return
}
if (!('permissions' in server)) {
res.status(200).json({ success: true, data: {
botEdit: true,
paymentEdit: false,
commands: true,
commandsAllChannel: false,
owner: false
} })
return
}
let rolePermission = server.permissions.find(element=>element.id == roleID)
let roleData
if (rolePermission) {
roleData = rolePermission.perms
}else{
roleData = {
botEdit: true,
paymentEdit: false,
commands: true,
commandsAllChannel: false,
owner: false
}
}
res.status(200).json({ success: true, data: roleData})

} catch (error) {
console.log(error);
res.status(200).json({ success: false })
}
})





//TODO Mercado Pago

const mercadoPago = require('./mercadoPago.js')
Expand All @@ -496,7 +617,7 @@ app.use('/', stripeRoutes);
//TODO PRODUTOS ROUTES

const produtoRoutes = require('./stripe/productsRoutes.js');
const { doc } = require("firebase/firestore");
const { Filter } = require("firebase-admin/firestore");

app.use('/', produtoRoutes);

Expand Down
Loading

0 comments on commit 81df4eb

Please sign in to comment.