Skip to content

Commit

Permalink
emoji reaction role
Browse files Browse the repository at this point in the history
+/reactionrole web-page
+emoji Handler
  • Loading branch information
PAW122 committed Dec 13, 2024
1 parent ce18917 commit 13c10f2
Show file tree
Hide file tree
Showing 9 changed files with 1,085 additions and 5 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,25 @@ dropienie, walki itd
> każda komenda w tym foldeże jest dostępna tylko dla osób z subem
> komendy testowe będą na prefix
+ usówanie linków do serwerów dc
# in progres - reaction role
/reaction-role-add <msg id> <>
handler -> event add emoji -> check server.reaction-role.msg_id.emoji_id/name.data = {addedBy, addedTime, addRole_id, addEmoji, status} -> addRole_id.addToUser(userId)
handler -> event remove emoji -> ...
server.reaction_role.channel_id.msg_id.emoji_id/name.data
# reaction role web - V1
napisanie wiadomości w przeglądarce, wybranie kanału i send
+ lista emoji + rola
i wszystko się samo zrobi
# todo
opcje wrzucenia linku na dc i po zalogowaniu user może zareagować na role w przeglądarce
!!! login -> przenośić userna do /login
11 changes: 10 additions & 1 deletion api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const full_load = require("./endpoints/full_load")
const actions = require("./endpoints/actions")
const admin = require("./endpoints/admin")
const full_mod_load = require("./endpoints/full_mod_logs_load")
const reaction_role_api = require("./endpoints/reactionrole_settings")

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// index file serving
app.get("/", (req, res) => {
Expand All @@ -26,6 +30,12 @@ app.get("/partners", (req, res) => {
return res.sendFile(process.cwd() + "/api/webpanel/partners.html")
})

app.use("/api_reactionrole", reaction_role_api)

app.get("/reactionrole_settings/:id", (req, res) => {
return res.sendFile(process.cwd() + "/api/webpanel/reaction_role_settings.html")
})

app.get("/admin", (req, res) => {
return res.sendFile(process.cwd() + "/api/webpanel/admin.html")
})
Expand Down Expand Up @@ -85,7 +95,6 @@ app.use("/modlogs", mod_logs)
app.use("/actions", actions)
app.use("/admin", admin)
app.use("/mod_logs", full_mod_load)
app.use(bodyParser.json());
// AUDIO TEST API ==================================================

const { DataStore } = require("../handlers/audio/api")
Expand Down
291 changes: 291 additions & 0 deletions api/endpoints/reactionrole_settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
const express = require('express');
const router = express.Router();

const Database = require("../../db/database")
const db = new Database(__dirname + "/../../db/files/servers.json")

const { Auth, AuthV2 } = require("../handlers/auth")
const authV2 = AuthV2.getInstance();

const checkServerExists = require("../handlers/checkServerExists")

const config = require("../config.json")
const mod_logs_list = config.api.mod_logs_list

const BotLogs = require("../../handlers/bot_logs_handler")
const BotLogsHandler = BotLogs.getInstance()

// /reactionrole/...

/*
creating and deleting data by managing on bot web-page
*/

// add reaction-role to message by id
router.post("/save_reaction_by_id/:guildId", async (req, res) => {
// get data
if (!req.body) {
return res.status(400).json({ error: "No data provided" })
}
const tokenType = req.body.tokenType
const token = req.body.token
const server_id = req.params.guildId
const channel_id = req.body.channel_id
const message_id = req.body.message_id
const role_id = req.body.role_id
const emoji = req.body.emoji
const status = req.body.status
const name = req.body.name // name to display on web

let { client } = require("../../main");
if (!client) {
console.error("Client is undefind")
return res.status(401).json({ error: "client is offline" })
}

if (!tokenType || !token || !server_id) {
return res.status(400).json({ error: "one or more body argument is undefined" })
}

if (!channel_id || !emoji) {
return res.status(400).json({ error: "one or more body argumen's is undefined" })
}

const is_authV2 = await authV2.verification(tokenType, token, server_id)

if (!is_authV2) {
return res.status(400).json({ error: "Not auth" })
}

const is_server = await checkServerExists(server_id)
if (!is_server) {
return res.status(400).json({ error: "server_id is invalid" })
}

const save_data = {
emoji: emoji,
status: status,
role_id: role_id,
name: name,
}
db.init();
db.addToList(`${server_id}.reaction_role.${channel_id}.${message_id}`, save_data)

BotLogsHandler.SendLog(server_id, `Reaction role set on bot website\n channel_id: ${channel_id}\n message_id: ${message_id}`)

return res.status(200).json({ status: "ok" });
})

// delete reaction-role to message by id
router.post("/remove_reaction_by_id/:guildId", async (req, res) => {
console.log(req.body)
// get data
if (!req.body) {
return res.status(400).json({ error: "No data provided" })
}
const tokenType = req.body.tokenType
const token = req.body.token
const server_id = req.params.guildId
const message_id = req.body.message_id
const emoji = req.body.emoji

let { client } = require("../../main");
if (!client) {
console.error("Client is undefind")
return res.status(401).json({ error: "client is offline" })
}

if (!tokenType || !token || !server_id) {
return res.status(400).json({ error: "one or more body argument is undefined" })
}

if (!emoji) {
return res.status(400).json({ error: "one or more body argumen's is undefined" })
}

const is_authV2 = await authV2.verification(tokenType, token, server_id)

if (!is_authV2) {
return res.status(400).json({ error: "Not auth" })
}

const is_server = await checkServerExists(server_id)
if (!is_server) {
return res.status(400).json({ error: "server_id is invalid" })
}

db.init();
const data = await db.read(`${server_id}.reaction_role`)

// przejsc po wszystkich i matchowac messageId oraz emoji
// console.log('data')
// console.log(data)

// console.log("db")
// // console.log(data.emoji, data.status, data.role_id, data.name)

// console.log("to delete")
// console.log(emoji)
// console.log(message_id)
// console.log(server_id)

Object.keys(data).forEach(channel_id => {
console.log(`channel id: ${channel_id}`);

// Sprawdź, czy `data[channel_id][message_id]` istnieje i jest tablicą
const messageData = data[channel_id][message_id];
if (Array.isArray(messageData)) {
messageData.forEach((obj, index) => {
if (obj.emoji === emoji && obj.status === true) {
// Zmień status w pamięci
obj.status = false;

// Zapisz zmiany w bazie danych
const dbKey = `${server_id}.reaction_role.${channel_id}.${message_id}`;
const existingData = db.read(dbKey); // Pobierz aktualne dane
if (Array.isArray(existingData)) {
// Znajdź odpowiedni obiekt w bazie danych
const targetIndex = existingData.findIndex(
item => item.emoji === emoji
);

if (targetIndex !== -1) {
existingData[targetIndex].status = false; // Aktualizuj status
db.write(dbKey, existingData); // Zapisz zmiany w bazie
BotLogsHandler.SendLog(
server_id,
`Reaction role deleted on bot website\nchannel_id: ${channel_id}\nmessage_id: ${message_id}`
);

} else {
console.warn(`Object with emoji ${emoji} not found in DB`);
}
} else {
console.warn(`Data at ${dbKey} is not an array`);
}
}
});
} else {
console.warn(
`Message data for channel_id: ${channel_id} and message_id: ${message_id} is not an array`
);
}
});


return res.status(200).json({ status: "ok" });
})

// create re_ro message and autocomplete re_ro data
router.get("/create_reaction_role_message", async (req, res) => {

})

// delete re_ro message and autocomplete re_ro data
router.get("/delete_reaction_role_message", async (req, res) => {

})

/*
reaction-role-api
for webside loading / displaying data
*/

// get all user objects to display
router.get("/get_all_users_reaction_roles", async (req, res) => {

})

// load all data neede to display stuf on website
router.post("/load_all", async (req, res) => {
// get data
if (!req.body) {
return res.status(400).json({ error: "No data provided" })
}
const tokenType = req.body.tokenType
const token = req.body.token
const server_id = req.body.server_id

let { client } = require("../../main");
if (!client) {
console.error("Client is undefind")
return res.status(401).json({ error: "client is offline" })
}

if (!tokenType || !token || !server_id) {
return res.status(400).json({ error: "one or more body argument is undefined" })
}

const is_authV2 = await authV2.verification(tokenType, token, server_id)

if (!is_authV2) {
return res.status(400).json({ error: "Not auth" })
}

const is_server = await checkServerExists(server_id)
if (!is_server) {
return res.status(400).json({ error: "server_id is invalid" })
}

// loading aplication data
const server = client.guilds.cache.get(server_id);
if (!server) {
return res.status(400).json({ error: "invalid serverId" })
}

db.init();
let data = await db.read(`${server_id}.reaction_role`);
if (data) {
for (const guildKey in data) {
if (data.hasOwnProperty(guildKey)) {
for (const messageKey in data[guildKey]) {
if (data[guildKey].hasOwnProperty(messageKey)) {
const messageData = data[guildKey][messageKey];

if (Array.isArray(messageData)) {
data[guildKey][messageKey] = messageData.filter(
element => element.status === true
);
} else {
console.warn(`Warning: data[${guildKey}][${messageKey}] is not an array`);
}
}
}
}
}
}

// if (!data) {
// return res.status(400).json({error: "server reaction role not found"})
// }

const channels = server.channels.cache;//dont send
const server_channels_list = channels.map(channel => ({
id: channel.id,
name: channel.name
}));

const roles = server.roles.cache;//dont send
const server_roles_list = roles.map(role => ({
id: role.id,
name: role.name
}));

return res.status(200).json({
reaction_role_data: data,
channels: server_channels_list,
roles: server_roles_list
})
})

// api for future website to display guild reaction roles for not admin users
// to be able to get role from page without need to do that on discord.
router.get("/get_all_guild_reaction_roles", async (req, res) => {

})

function checkToken() {

}

module.exports = router;
Loading

0 comments on commit 13c10f2

Please sign in to comment.