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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168import { ConfigurationModel, UserModel, Configuration, SaltrankModel, StillMutedModel, GlobalUserDataModel,} from './db';import { sendJoinEvent } from './webhooks';
export async function getUser(userid: string, guildID: string) { const result = await UserModel.findOneAndUpdate( { userID: userid, guildID, }, { $setOnInsert: { warnings: 0, kicks: 0, bans: 0, botusage: 0, sound: undefined, }, }, { returnOriginal: false, upsert: true, }, ); return result;}
export async function getUserInAllGuilds(userid: string) { const result = await UserModel.find({ userID: userid, }); return result;}
export async function getGlobalUser(userId: string) { const result = await GlobalUserDataModel.findOneAndUpdate( { userID: userId, }, { $setOnInsert: { sound: undefined, }, }, { returnOriginal: false, upsert: true, }, ); return result;}
export async function removeAllJoinsoundsOfUserFromDb(userId: string) { await UserModel.updateMany( { userID: userId }, { $unset: { sound: 1, soundTitle: 1 } }, ); await GlobalUserDataModel.updateMany( { userID: userId }, { $unset: { sound: 1, soundTitle: 1 } }, );}
async function createFirstConfiguration(guildID: string) { const configuration = new ConfigurationModel({ _id: guildID, commandChannels: [], adminRoles: [], joinChannels: [], saltKing: undefined, saltRole: undefined, lastConnected: new Date(), defaultJoinsound: undefined, }); await configuration.save(); return configuration;}
export async function getConfiguration(guildID: string) { let result = await ConfigurationModel.findById(guildID); if (!result) { await sendJoinEvent( `:wastebasket: couldn't find configuration for guild ${guildID} , resetting/setting empty state. Found this:${JSON.stringify( result, null, 2, )}`, );
result = await createFirstConfiguration(guildID); } return result;}
export async function checkGuild(id: string) { // create configuration if (await getConfiguration(id)) { return true; } return false;}
export async function setConfiguration( guildID: string, // this was difficult to find, but is awesome update: { [Property in keyof Configuration]?: Configuration[Property] },) { if (await getConfiguration(guildID)) { await ConfigurationModel.updateOne({ _id: guildID }, { $set: update }); } return true;}
// top 5 salty peopleexport async function topSalt(guildID: string) { const result = await SaltrankModel.find({ guild: guildID }) .sort({ salt: -1 }) .limit(5); if (!result) { return []; } return result;}
export async function isJoinableVc(guildID: string, channelID: string) { const configuration = await getConfiguration(guildID); return ( configuration.joinChannels.length === 0 || configuration.joinChannels.includes(channelID) );}
export async function toggleStillMuted( userID: string, guildID: string, add: boolean,) { if (add) { const isStillMutedAmount = await StillMutedModel.find({ userid: userID, guildid: guildID, }).countDocuments(); if (isStillMutedAmount === 0) { const newMute = new StillMutedModel({ userid: userID, guildid: guildID, }); await newMute.save(); } } else { await StillMutedModel.deleteMany({ userid: userID, guildid: guildID, }); }}
export async function getCommandChannels(guildID: string) { const configuration = await getConfiguration(guildID); return configuration.commandChannels;}