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
+52 -58

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
+25 -16
View File
@@ -18,24 +18,25 @@ module.exports = {
* @param {*} param3 * @param {*} param3
*/ */
run: async (client, interaction, args, { GuildDB }) => { run: async (client, interaction, args, { GuildDB }) => {
if (GuildDB.customChannelStatus==true&&!GuildDB.allowedChannels.includes(interaction.channel_id)) { 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.` }); 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); 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. 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) });
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);
// If panic is enabled, disable panic // If panic is enabled, disable panic
if (user.user.dispatchStatus=='Panic') { if (user.user.dispatchStatus == 'Panic') {
let myReq = {
const socket = io.connect(client.config.socket);
socket.emit('clear_panic', {
userID: user._id, userID: user._id,
communityID: user.user.activeCommunity communityID: user.user.activeCommunity
}; });
socket.emit('clear_panic', myReq);
let myUpdateReq = { let myUpdateReq = {
userID: user._id, userID: user._id,
@@ -44,17 +45,21 @@ module.exports = {
onDuty: null, onDuty: null,
updateDuty: false updateDuty: false
}; };
socket.emit('bot_update_status', myUpdateReq); socket.emit('bot_update_status', myUpdateReq);
socket.on('bot_updated_status', (res) => { 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\`.` }); interaction.send({ content: `Disabled Panic <@${interaction.member.user.id}> and set status to \`10-8\`.` });
socket.disconnect(); socket.disconnect();
}
}); });
return; return;
// If panic is disabled, enable panic // If panic is disabled, enable panic
} else { } 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.`, flags: (1 << 6) });
if (user.user.activeCommunity==null) return interaction.send({ content: `You must join a community to use this command.` });
client.forceUpdateStatus('Panic', user); client.forceUpdateStatus('Panic', user);
let req = { let req = {
@@ -62,11 +67,15 @@ module.exports = {
userUsername: user.user.username, userUsername: user.user.username,
activeCommunity: user.user.activeCommunity activeCommunity: user.user.activeCommunity
} }
const socket = io.connect(client.config.socket);
socket.emit('panic_button_update', req); socket.emit('panic_button_update', req);
socket.disconnect(); 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` }); 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) });
} }
}, },
}, },
+27 -40
View File
@@ -16,9 +16,8 @@ class LinesPoliceCadBot extends Client {
this.interactionHandlers = new Collection(); this.interactionHandlers = new Collection();
this.logger = new Logger(path.join(__dirname, "..", "logs/Logs.log")); this.logger = new Logger(path.join(__dirname, "..", "logs/Logs.log"));
if (this.config.Token === "") if (this.config.Token === "") return new TypeError(
return new TypeError( "The config.js is missing a token, please ensure the token is provided."
"The botconfig.js is not filled out. Please make sure nothing is blank, otherwise the bot will not work properly."
); );
this.db; this.db;
@@ -83,7 +82,7 @@ class LinesPoliceCadBot extends Client {
databaselogs = { databaselogs = {
attempts: 0, attempts: 0,
connected: false, connected: false,
} };
} }
if (databaselogs.attempts >= 5) { if (databaselogs.attempts >= 5) {
@@ -92,8 +91,8 @@ class LinesPoliceCadBot extends Client {
} }
try { try {
// Connect to MongoDB // Connect to Mongo database.
this.db = await MongoClient.connect(mongoURI,{useUnifiedTopology:true}); this.db = await MongoClient.connect(mongoURI, { useUnifiedTopology: true, connectTimeoutMS: 1000 });
this.dbo = this.db.db(dbo); this.dbo = this.db.db(dbo);
this.log('Successfully connected to mongoDB'); this.log('Successfully connected to mongoDB');
databaselogs.connected = true; databaselogs.connected = true;
@@ -101,12 +100,13 @@ class LinesPoliceCadBot extends Client {
this.databaseConnected = true; this.databaseConnected = true;
} catch (err) { } catch (err) {
databaselogs.attempts++; 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; failed = true;
} }
// write JSON string to a file // write JSON string to a file
await fs.writeFileSync(dbLogDir, JSON.stringify(databaselogs)); fs.writeFileSync(dbLogDir, JSON.stringify(databaselogs));
if (failed) process.exit(-1); if (failed) process.exit(-1);
} }
@@ -114,52 +114,39 @@ class LinesPoliceCadBot extends Client {
// This is for the 'panic' command when enabling panic // This is for the 'panic' command when enabling panic
// May be used by other interaction // May be used by other interaction
async forceUpdateStatus(status, user) { async forceUpdateStatus(status, user) {
let onDuty=null; let onDuty = null;
let updateDuty=false; let updateDuty = false;
if (status=='10-41') {
onDuty=true; // If the are off duty, make them on duty + online
updateDuty=true; if (status == '10-41') {
status='Online'; onDuty = true;
updateDuty = true;
status = 'Online';
} }
if (status=='10-42') {
onDuty=false; // If they are on duty,
updateDuty=true; if (status == '10-42') {
status='Offline'; onDuty = false;
updateDuty = true;
status = 'Offline';
} }
let req={
let req = {
userID: user._id, userID: user._id,
status: status, status: status,
setBy: 'Self', setBy: 'Self',
onDuty: onDuty, onDuty: onDuty,
updateDuty: updateDuty updateDuty: updateDuty
}; };
const socket = io.connect(this.config.socket); const socket = io.connect(this.config.socket);
socket.emit('bot_update_status', req); socket.emit('bot_update_status', req);
socket.on('bot_updated_status', (res) => { 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} exists(n) {return null != n && undefined != n && "" != n}
LoadCommandsAndInteractionHandlers() { LoadCommandsAndInteractionHandlers() {