wolfcountBot/client.js

99 lines
3.0 KiB
JavaScript

require('dotenv').config();
const {Client, ActivityType} = require('discord.js');
const mongoose = require('mongoose');
const Wolfcount = require('./models/wolfcount');
console.clear();
const uri = `mongodb+srv://${process.env.MONGODBUSER}:${process.env.MONGODBPASS}@${process.env.MONGODBCLUSTER}/${process.env.DATABASE}?retryWrites=true&w=majority`
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`Connected to the database`);
}).catch((error) => {
console.log(`Failed to connect to the database`);
console.log(error);
});
const client = new Client({ intents: 34305 });
client.on("ready", () => {
// set the status to do not disturb
// client.user.setStatus('dnd')
client.user.setPresence({
activities: [{ name: `over msgs for certain letters`, type: ActivityType.Watching }],
status: 'dnd',
});
});
// Error handling
client.on("error", (error) => {
console.log('');
console.error("Something went wrong:", error);
});
// When a message is sent, check if the message includes the letters of wolf
const wolfChars = ['w', 'o', 'l', 'f'];
// Find a document by validation
Wolfcount.findOne({ validation: 'wolfcount' }).then((countdocument) => {
console.log('');
console.log(countdocument);
}).catch((error) => {
console.error(error);
});
basecount = 0
// Reset the wolf count
Wolfcount.findByIdAndUpdate("6447da87681fd1bc486a4923", {count: basecount}).then(() => {
console.log('');
console.log('Count reset!');
}).catch((error) => {
console.error(error);
});
client.on("messageCreate", (message) => {
// convert the message to lowercase
const lowerMsg = message.content.toLowerCase();
// console.log(lowerMsg);
// check if the message has the letters
if (wolfChars.every(char=>lowerMsg.includes(char))) {
message.react("🐺").catch(err=>console.log(err));
newcount = basecount + 1;
client.user.setPresence({
activities: [{ name: `${newcount} message(s) detected`, type: ActivityType.Watching }],
status: 'dnd',
});
basecount = newcount;
console.log('');
console.log('Live wolf count:', newcount);
// update the wolf count
Wolfcount.findByIdAndUpdate("6447da87681fd1bc486a4923", {count: newcount}).then(() => {
console.log('Count updated!');
}).catch((error) => {
console.error(error);
});
// })
// Wolfcount.findByIdAndUpdate("6447da87681fd1bc486a4923", {count: newcount}, function(err, result){
// if(err){
// console.log('Something went wrong while updating the count:', err)
// }
// else{
// console.log('Successfully updated wolfcount to', result)
// }
// })
}
});
// Connect to Discord
client.login(process.env.BOT_TOKEN);