Fix issues in mongod & Set client as discord in /setup & add Message model & integrate messageManager

This commit is contained in:
ui_creeperlv 2024-03-06 19:14:30 +05:30
parent 32d18716cb
commit 1559faeeb7
6 changed files with 128 additions and 4 deletions

View File

@ -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);
},

View File

@ -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`);

View File

@ -43,6 +43,8 @@ async function initMessageManager(database) {
client.database = database;
database.build();
require("./messageManager/messageManager")(database , client);
}
client.commands = new Collection();

View File

@ -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;

View File

@ -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;

View File

@ -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;