Stryker-SC/Discord-Bot-VCMaster-JavaScript-Revamped
0
1const mongoose = require('mongoose');2const logger = require('./utils/logger');3 4// Schemas5const guildConfigSchema = new mongoose.Schema({6 guild_id: { type: String, required: true, unique: true },7 hub_channel_id: { type: String, required: true },8 category_id: { type: String }9});10 11const tempChannelSchema = new mongoose.Schema({12 channel_id: { type: String, required: true, unique: true },13 owner_id: { type: String, required: true },14 guild_id: { type: String, required: true },15 created_at: { type: Number, required: true },16 text_channel_id: { type: String },17 last_renamed: { type: Number, default: 0 }18});19 20const roleConfigSchema = new mongoose.Schema({21 guild_id: { type: String, required: true, unique: true },22 auto_join_role_id: { type: String },23 auto_bot_role_id: { type: String }24});25 26// Models27const GuildConfig = mongoose.model('GuildConfig', guildConfigSchema);28const TempChannel = mongoose.model('TempChannel', tempChannelSchema);29const RoleConfig = mongoose.model('RoleConfig', roleConfigSchema);30 31module.exports = {32 // connect function to be called in index.js33 connect: async (uri) => {34 try {35 await mongoose.connect(uri, {36 maxPoolSize: 5,37 serverSelectionTimeoutMS: 5000,38 socketTimeoutMS: 45000,39 autoIndex: false40 });41 logger.info('[Database] Connected to MongoDB');42 43 mongoose.connection.on('disconnected', () => {44 logger.warn('[Database] MongoDB disconnected.');45 });46 47 mongoose.connection.on('error', (err) => {48 logger.error('[Database] MongoDB ongoing connection error:', err);49 });50 } catch (err) {51 logger.error('[Database] MongoDB Connection Error:', err);52 process.exit(1);53 }54 },55 56 // Guild Config57 setHubChannel: async (guildId, hubId, categoryId = null) => {58 return await GuildConfig.findOneAndUpdate(59 { guild_id: guildId },60 { hub_channel_id: hubId, category_id: categoryId },61 { upsert: true, new: true }62 );63 },64 65 getGuildConfig: async (guildId) => {66 return await GuildConfig.findOne({ guild_id: guildId });67 },68 69 // Temp Channels70 addTempChannel: async (channelId, ownerId, guildId, textChannelId = null) => {71 return await TempChannel.create({72 channel_id: channelId,73 owner_id: ownerId,74 guild_id: guildId,75 created_at: Date.now(),76 text_channel_id: textChannelId77 });78 },79 80 getTempChannel: async (channelId) => {81 return await TempChannel.findOne({ channel_id: channelId });82 },83 84 removeTempChannel: async (channelId) => {85 return await TempChannel.findOneAndDelete({ channel_id: channelId });86 },87 88 getAllTempChannels: async () => {89 return await TempChannel.find({});90 },91 92 updateTempChannelOwner: async (channelId, newOwnerId) => {93 return await TempChannel.findOneAndUpdate(94 { channel_id: channelId },95 { owner_id: newOwnerId },96 { new: true }97 );98 },99 100 updateTempChannelRenameTime: async (channelId, timestamp) => {101 return await TempChannel.findOneAndUpdate(102 { channel_id: channelId },103 { last_renamed: timestamp },104 { new: true }105 );106 },107 108 // Role Config109 setAutoJoinRole: async (guildId, roleId) => {110 return await RoleConfig.findOneAndUpdate(111 { guild_id: guildId },112 { auto_join_role_id: roleId },113 { upsert: true, new: true }114 );115 },116 117 setAutoBotRole: async (guildId, roleId) => {118 return await RoleConfig.findOneAndUpdate(119 { guild_id: guildId },120 { auto_bot_role_id: roleId },121 { upsert: true, new: true }122 );123 },124 125 getRoleConfig: async (guildId) => {126 return await RoleConfig.findOne({ guild_id: guildId });127 }128};129 