diff --git a/database/mongod.js b/database/mongod.js index 61b7ebd..414c8ac 100644 --- a/database/mongod.js +++ b/database/mongod.js @@ -17,10 +17,10 @@ module.exports.initMongoDBInstance = async (client,config,callback) => { }) }, - list: async (table , callback) => { + list: async (table , data, callback) => { var collection = db.collection(table); - var found_data = await collection.find(data); + var found_data = await collection.find(data).toArray(); callback(found_data); }, diff --git a/src/commands/servers/setup.js b/src/commands/servers/setup.js index 277e102..5895392 100644 --- a/src/commands/servers/setup.js +++ b/src/commands/servers/setup.js @@ -22,9 +22,10 @@ module.exports = { if(data == null){ interaction.client.database.insert("xan.guilds" , { serverId : interaction.guild.id, - channelId : channel.id + channelId : channel.id, + client: "discord" }, async (c) => { - await interaction.reply(`Saved Channel as #${channel.name} in Database`); + await interaction.reply(`Saved Channel as #${channel.name} in Database as Discord Client`); }) }else{ await interaction.reply(`Guild is already saved into Database`); diff --git a/src/index.js b/src/index.js index 2104568..c108956 100644 --- a/src/index.js +++ b/src/index.js @@ -43,6 +43,8 @@ async function initMessageManager(database) { client.database = database; database.build(); + + require("./messageManager/messageManager")(database , client); } client.commands = new Collection(); diff --git a/src/messageManager/Message.js b/src/messageManager/Message.js new file mode 100644 index 0000000..23072c9 --- /dev/null +++ b/src/messageManager/Message.js @@ -0,0 +1,50 @@ +class Message { + /** + * @typedef {Object} MessageFields + * @property {import('crypto').UUID} uuid + * @property {import('discord.js').Snowflake} serverId + * @property {string|null} serverName + * @property {import('discord.js').Snowflake} authorId + * @property {string|null} authorName + * @property {string|null} message + */ + + constructor(){ + this.uuid = require('crypto').randomUUID(); + this.serverId = 1; + this.serverName = "[No Server]"; + this.authorId = 1; + this.authorName = "[No Author]"; + this.message = "[No Message]"; + } + + /** + * Fill Product fields with new values + * @param {MessageFields} newFields - Object containing new values for Category fields + */ + fill(newFields) { + for (let field in newFields) { + if (this.hasOwnProperty(field) && newFields.hasOwnProperty(field)) { + if (this[field] !== 'undefined') { + this[field] = newFields[field]; + } + } + } + + return this.toArray(); + } + + toArray(){ + return { + uuid : this.uuid, + serverId : this.serverId, + serverName : this.serverName, + authorId : this.authorId, + authorName: this.authorName, + message: this.message + }; + } + +} + +module.exports = Message; diff --git a/src/messageManager/client/discord/send.js b/src/messageManager/client/discord/send.js new file mode 100644 index 0000000..5ad2c42 --- /dev/null +++ b/src/messageManager/client/discord/send.js @@ -0,0 +1,27 @@ +const { Client } = require("discord.js"); +const Message = require("../../Message"); +const { EmbedBuilder } = require("@discordjs/builders"); + +/** + * + * @param {Message} message + * @param {Client} discord + */ +const send = (message, database, channelId , discord) => { + discord.channels.cache?.get(channelId).send({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: message.authorName, + iconURL: discord.user.displayAvatarURL() + }) + .setDescription(message.message) + .setFooter({ + text: message.serverName, + iconURL: discord.user.displayAvatarURL() + }) + ] + }) +} + +module.exports = send; \ No newline at end of file diff --git a/src/messageManager/messageManager.js b/src/messageManager/messageManager.js new file mode 100644 index 0000000..78978dd --- /dev/null +++ b/src/messageManager/messageManager.js @@ -0,0 +1,44 @@ +const { Client, Events } = require("discord.js"); +const Message = require("./Message"); +const discordSend = require("./client/discord/send"); +const { data } = require("../commands/servers/setup"); +/** + * + * @param {Client} discord + */ +const initMessageManager = async ( + database, + discord +) => { + function postMessage(message){ + database.list("xan.guilds" , {} , (list) => { + for(var element of list){ + + if(element.client == "discord") discordSend(message , data , element.channelId , discord); + } + }); + } + + + discord.on(Events.MessageCreate , (message) => { + if(message.author.bot) return; + database.search("xan.guilds" , { + serverId : message.guild.id + } , async (data) => { + if(data !== null){ + var _message = new Message(); + _message.authorId = message.author.id; + _message.authorName = message.author.username; + _message.serverId = message.guild.id; + _message.serverName = message.guild.name, + _message.message = message.content; + + delete _message.uuid; + + postMessage(_message.toArray()); + } + }); + }); +} + +module.exports = initMessageManager; \ No newline at end of file