clean my code because its bad + fix bot not responding when activating panic when pingOnPanic is disabled

This commit is contained in:
SowinskiBraeden committed 2024-01-22 19:21:14 -08:00
1 parent 2cd8185c0a
commit 13e66b29ad
3 files changed
+61 -67

No files matched your search

-2
View File
@@ -1,2 +0,0 @@
Worker: node --optimize_for_size --max-old-space-size=2560 index.js
BetaWorker: node --optimize_for_size --max-old-space-size=2560 beta.js
+29 -20
View File
@@ -18,24 +18,25 @@ module.exports = {
* @param {*} param3
*/
run: async (client, interaction, args, { GuildDB }) => {
if (GuildDB.customChannelStatus==true&&!GuildDB.allowedChannels.includes(interaction.channel_id)) {
return interaction.send({ content: `You are not allowed to use the bot in this channel.` });
if (GuildDB.customChannelStatus == true && !GuildDB.allowedChannels.includes(interaction.channel_id)) {
return interaction.send({ content: `You are not allowed to use the bot in this channel.`, flags: (1 << 6) });
}
let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true);
if (!useCommand) return interaction.send({ content: "You don't have permission to use this command" });
if (!useCommand) return interaction.send({ content: "You don't have permission to use this command", flags: (1 << 6) });
let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user);
if (!user) return interaction.send({ content: `You are not logged in.` });
if (user.user.activeCommunity==null) return interaction.send({ content: `You must join a community to use this command.` });
const socket = io.connect(client.config.socket);
let user = await client.dbo.collection("users").findOne({"user.discord.id": interaction.member.user.id}).then(user => user);
if (!user) return interaction.send({ content: `You are not logged in. Go to https://linespolice-cad.com/ to login, and connect your Discord account.`, flags: (1 << 6) });
if (user.user.activeCommunity == null) return interaction.send({ content: `You must join a community to use this command.`, flags: (1 << 6) });
// If panic is enabled, disable panic
if (user.user.dispatchStatus=='Panic') {
let myReq = {
if (user.user.dispatchStatus == 'Panic') {
const socket = io.connect(client.config.socket);
socket.emit('clear_panic', {
userID: user._id,
communityID: user.user.activeCommunity
};
socket.emit('clear_panic', myReq);
});
let myUpdateReq = {
userID: user._id,
@@ -44,29 +45,37 @@ module.exports = {
onDuty: null,
updateDuty: false
};
socket.emit('bot_update_status', myUpdateReq);
socket.on('bot_updated_status', (res) => {
interaction.send({ content: `Disabled Panic <@${interaction.member.user.id}> and set status to \`10-8\`.` });
socket.disconnect();
socket.on('bot_updated_status', (res) => {
if (res.userID == user._id) {
interaction.send({ content: `Disabled Panic <@${interaction.member.user.id}> and set status to \`10-8\`.` });
socket.disconnect();
}
});
return;
// If panic is disabled, enable panic
} else {
let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user);
if (!user) return interaction.send({ content: `You are not logged in.` });
if (user.user.activeCommunity==null) return interaction.send({ content: `You must join a community to use this command.` });
if (user.user.activeCommunity == null) return interaction.send({ content: `You must join a community to use this command.`, flags: (1 << 6) });
client.forceUpdateStatus('Panic', user);
let req = {
userID: user._id,
userUsername: user.user.username,
activeCommunity: user.user.activeCommunity
}
const socket = io.connect(client.config.socket);
socket.emit('panic_button_update', req);
socket.disconnect();
let guild = await client.dbo.collection("prefixes").findOne({"server.serverID":GuildDB.serverID}).then(guild => guild);
let guild = await client.dbo.collection("prefixes").findOne({"server.serverID": GuildDB.serverID}).then(guild => guild);
if (guild.server.pingOnPanic) return interaction.send({ content: `Attention <@&${guild.server.pingRole}> \`${user.user.username}\` has activated panic` });
return;
return interaction.send({ content: 'Successfully activated Panic', flags: (1 << 6) });
}
},
},
+32 -45
View File
@@ -16,9 +16,8 @@ class LinesPoliceCadBot extends Client {
this.interactionHandlers = new Collection();
this.logger = new Logger(path.join(__dirname, "..", "logs/Logs.log"));
if (this.config.Token === "")
return new TypeError(
"The botconfig.js is not filled out. Please make sure nothing is blank, otherwise the bot will not work properly."
if (this.config.Token === "") return new TypeError(
"The config.js is missing a token, please ensure the token is provided."
);
this.db;
@@ -74,7 +73,7 @@ class LinesPoliceCadBot extends Client {
async connectMongo(mongoURI, dbo) {
let failed = false;
let dbLogDir = path.join(__dirname, '..', 'logs', 'database-logs.json');
let databaselogs;
try {
@@ -83,7 +82,7 @@ class LinesPoliceCadBot extends Client {
databaselogs = {
attempts: 0,
connected: false,
}
};
}
if (databaselogs.attempts >= 5) {
@@ -92,8 +91,8 @@ class LinesPoliceCadBot extends Client {
}
try {
// Connect to MongoDB
this.db = await MongoClient.connect(mongoURI,{useUnifiedTopology:true});
// Connect to Mongo database.
this.db = await MongoClient.connect(mongoURI, { useUnifiedTopology: true, connectTimeoutMS: 1000 });
this.dbo = this.db.db(dbo);
this.log('Successfully connected to mongoDB');
databaselogs.connected = true;
@@ -101,12 +100,13 @@ class LinesPoliceCadBot extends Client {
this.databaseConnected = true;
} catch (err) {
databaselogs.attempts++;
this.error(`Failed to connect to mongodb: attempt ${databaselogs.attempts}`);
let db = (mongoURI.includes("@") ? mongoURI.split("@")[1] : mongoURI.split("//")[1]).endsWith("/") ? mongoURI.slice(0, -1) : mongoURI;
this.error(`Failed to connect to mongodb (mongodb://${db}/${dbo}): attempt ${databaselogs.attempts} - ${err}`);
failed = true;
}
// write JSON string to a file
await fs.writeFileSync(dbLogDir, JSON.stringify(databaselogs));
fs.writeFileSync(dbLogDir, JSON.stringify(databaselogs));
if (failed) process.exit(-1);
}
@@ -114,52 +114,39 @@ class LinesPoliceCadBot extends Client {
// This is for the 'panic' command when enabling panic
// May be used by other interaction
async forceUpdateStatus(status, user) {
let onDuty=null;
let updateDuty=false;
if (status=='10-41') {
onDuty=true;
updateDuty=true;
status='Online';
let onDuty = null;
let updateDuty = false;
// If the are off duty, make them on duty + online
if (status == '10-41') {
onDuty = true;
updateDuty = true;
status = 'Online';
}
if (status=='10-42') {
onDuty=false;
updateDuty=true;
status='Offline';
// If they are on duty,
if (status == '10-42') {
onDuty = false;
updateDuty = true;
status = 'Offline';
}
let req={
userID: user._id,
status: status,
setBy: 'Self',
onDuty: onDuty,
let req = {
userID: user._id,
status: status,
setBy: 'Self',
onDuty: onDuty,
updateDuty: updateDuty
};
const socket = io.connect(this.config.socket);
socket.emit('bot_update_status', req);
socket.on('bot_updated_status', (res) => {
socket.disconnect();
if (res.userID == user._id) socket.disconnect(); // ensure success response is for us
});
}
/* Not yet done
async canUseCommand(interaction, GuildDB) {
if (!GuildDB.customChannelStatus) return true;
let channel_id;
for (let i = 0; i < GuildDB.allowedChannels.length; i++) {
channel_id = GuildDB.allowedChannels[i];
if(interaction.guild.channels.cache.get(channel_id) === undefined) {
this.dbo.collection("prefixes").updateOne({"server.serverID":interaction.guild.id},{$pull:{"server.allowedChannels":channel_id},$set:{"server.hasCustomChannels":false}},function(err, res) {
if (err) throw err;
GuildDB.allowedChannels.splice(i, i+1);
if (GuildDB.allowedChannels.length == 0) GuildDB.customChannelStatus = false;
});
}
}
if (!GuildDB.allowedChannels.includes(interaction.channel_id)) {
return false;
}
}
*/
exists(n) {return null != n && undefined != n && "" != n}
LoadCommandsAndInteractionHandlers() {