Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/maybeanerd/MagiBot. MagiBot - the community building discord bot that adds joinsounds to your voice discord.gg/2Evcf4T
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380import mongoose from 'mongoose';import config from './configuration';
if (!config.dburl) { throw new Error('Missing DB connection URL');}const url = config.dburl;
mongoose.connect(`${url}/MagiBot`, { ssl: true, maxPoolSize: 50,});
// const mongooseConnction = mongoose.connection;
// define types and Models of DB. later on we will move them out of here and just import them// configurations per guildexport type Configuration = { _id: string; commandChannels: Array<string>; adminRoles: Array<string>; // deprecated joinChannels: Array<string>; saltKing?: string; // deprecated saltRole?: string; // deprecated prefix: string; // deprecated lastConnected: Date; defaultJoinsound?: string; defaultJoinsoundTitle?: string;};const configurationSchema = new mongoose.Schema<Configuration>( { _id: { // _id is auto indexed and unique type: String, required: true, }, commandChannels: { type: [String], required: true, }, adminRoles: { // deprecated type: [String], required: true, }, joinChannels: { type: [String], required: true, }, saltKing: { // deprecated type: String, required: false, }, saltRole: { // deprecated type: String, required: false, }, prefix: { // deprecated type: String, required: false, }, lastConnected: { type: Date, required: true, }, defaultJoinsound: { type: String, required: false, }, defaultJoinsoundTitle: { type: String, required: false, }, }, { collection: 'settings' }, // outdated name);export const ConfigurationModel = mongoose.model<Configuration>( 'settings', // outdated name configurationSchema,);
// saltrank per usertype Saltrank = { salter: string; salt: number; guild: string };const saltrankSchema = new mongoose.Schema<Saltrank>( { salter: { type: String, required: true, }, salt: { type: Number, required: true, }, guild: { type: String, required: true, }, }, { collection: 'saltrank' },);saltrankSchema.index( { salter: 1, guild: -1, }, { unique: true },);export const SaltrankModel = mongoose.model<Saltrank>( 'saltrank', saltrankSchema,);
// salt reports per reporttype Salt = { salter: string; reporter: string; date: Date; guild: string;};const saltSchema = new mongoose.Schema<Salt>( { salter: { type: String, required: true, }, reporter: { type: String, required: true, }, date: { type: Date, required: true, }, guild: { type: String, required: true, index: true, }, }, { collection: 'salt' },);saltSchema.index({ salter: 1, reporter: -1,});export const SaltModel = mongoose.model<Salt>('salt', saltSchema);
// userdata per user per guildexport type User = { userID: string; guildID: string; botusage: number; sound?: string; soundTitle?: string;};const userSchema = new mongoose.Schema<User>( { userID: { type: String, required: true, }, guildID: { type: String, required: true, }, botusage: { type: Number, required: true, }, sound: { type: String, required: false, }, soundTitle: { type: String, required: false, }, }, { collection: 'users' },);userSchema.index( { userID: 1, guildID: -1, }, { unique: true },);export const UserModel = mongoose.model<User>('users', userSchema);
const deleteDuplicateUsers = true;UserModel.aggregate([ { $group: { _id: { userID: '$userID', guildID: '$guildID', }, count: { $sum: 1, }, }, },]).then((duplicateUsers) => { console.log( 'duplicate users:', JSON.stringify( duplicateUsers.filter((u) => u.count > 1), null, 2, ), ); if (deleteDuplicateUsers) { duplicateUsers .filter((u) => u.count > 1) .forEach(async (u) => { for (let i = 1; i < u.count; i++) { // eslint-disable-next-line no-underscore-dangle, no-await-in-loop await UserModel.deleteOne(u._id); // eslint-disable-next-line no-underscore-dangle console.log('Deleted user', u._id, 'run', i, 'of', u.count - 1); } }); }});
// global/default values for a specific userexport type globalUser = { userID: string; sound?: string; soundTitle?: string;};const globalUserSchema = new mongoose.Schema<globalUser>( { userID: { type: String, required: true, }, sound: { type: String, required: false, }, soundTitle: { type: String, required: false, }, }, { collection: 'globalUsers' },);globalUserSchema.index( { userID: 1, }, { unique: true },);export const GlobalUserDataModel = mongoose.model<globalUser>( 'globalUsers', globalUserSchema,);
// entry per voteexport type Vote = { messageID: string; channelID: string; options: Array<string>; topic: string; date: Date; guildid: string; authorID: string;};const voteSchema = new mongoose.Schema<Vote>( { messageID: { type: String, required: true, }, channelID: { type: String, required: true, }, options: { type: [String], required: true, }, topic: { type: String, required: true, }, date: { type: Date, required: true, index: true, }, guildid: { type: String, required: true, }, authorID: { type: String, required: true, }, }, { collection: 'votes' },);voteSchema.index({ messageID: 1, channelID: 1,});export const VoteModel = mongoose.model<Vote>('votes', voteSchema);
export type OngoingQueue = { guildId: string; channelId: string; messageId: string; creatorId: string; topic: string; queuedUsers: Array<string>;};const ongoingQueueSchema = new mongoose.Schema<OngoingQueue>( { guildId: { type: String, required: true, }, channelId: { type: String, required: true, }, messageId: { type: String, required: true, }, creatorId: { type: String, required: true, }, topic: { type: String, required: true, }, queuedUsers: { type: [String], required: true, }, }, { collection: 'ongoingQueue' },);ongoingQueueSchema.index({ guildId: 1 });export const OngoingQueueModel = mongoose.model<OngoingQueue>( 'ongoingQueue', ongoingQueueSchema,);
// one entry per user per guild// deprecated, we want to remove this system?type StillMuted = { userid: string; guildid: string };const stillMutedSchema = new mongoose.Schema<StillMuted>( { userid: { type: String, required: true, }, guildid: { type: String, required: true, }, }, { collection: 'stillMuted' },);stillMutedSchema.index({ userid: 1, guildid: 1,});export const StillMutedModel = mongoose.model<StillMuted>( 'stillMuted', stillMutedSchema,);