diff --git a/bot.js b/bot.js index 5f71143..70577e6 100644 --- a/bot.js +++ b/bot.js @@ -1,172 +1,154 @@ //This was made on my 14th birthday!!! -Mutant - const { Client, MessageEmbed } = require('discord.js'); const bot = new Client; const token = 'your-token-goes-here'; const mongoose = require('mongoose'); mongoose.connect("mongodb+srv://:@?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true }).catch(err => { - if (err) throw err; + if (err) console.error(err) }).then(() => { - console.log("Connected!"); + console.log("Connected!"); }) const serverSchema = new mongoose.Schema({ - serverid: String, - lastPerson: String, - countingChannel: String, - counter: String, - PREFIX: String + serverid: String, + lastPerson: String, + countingChannel: String, + counter: Number, + PREFIX: String }) const serverModel = mongoose.model("server", serverSchema); bot.on('ready', ready => { - bot.user.setActivity(`People count and spam c!help because im cool. Bot in ${bot.guilds.cache.size} guilds`, { type: 'LISTENING' }).then(() => { console.log('Ready!'); - }).catch(err => { - if(err){ - console.log('Error: ', err); - } - }); + setInterval(()=>{ + bot.user.setActivity(`People count || c!help || ${bot.guilds.cache.size} guilds`, { type: 'LISTENING' }) + .catch(err => { + if (err) console.error(err); + }); + },10000) }) -bot.on('message', message => { - if(message.author.bot){ - return; - } - var PREFIX; - if(message.channel.type == 'dm'){ - PREFIX = 'c!'; - if(message.content.toLowerCase() == `${PREFIX}help`){ - const help = new MessageEmbed() - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('Help!') - .setDescription("Below is the help you requested...") - .addField("General", "After using the command ``c!setchannel`` that channel will be **dedicated** to counting! Counting will start at 0, and you need to count __up__ from there.\nKeep in mind that you **cannot** count more then twice in a row, you must alternate counting with another person.") - .addField("Commands!", "``c!setchannel`` => this command will set the counting channel.\n``c!unsetchannel`` => this command will unset the counting channel turning it into a regular channel.\n``c!setprefix`` => this command will set the prefix for this bot in your server.\n``c!clear`` => this command will restart the server counter to **0**.\n``c!count`` => this command will show the current number this server is at incase you get lost.") - .addField("Invite!", "If you would like to invite this bot, an invite can be found [here](https://discord.com/api/oauth2/authorize?client_id=759932294026362900&permissions=354368&scope=bot).") - .setFooter("Thanks for using us!") - .setTimestamp() - .setColor('RANDOM'); - message.author.send(help).then(() => { - message.react('👍').catch(err => { - message.channel.send("Error: Invalid permissions, please enable the permission ``ADD_REACTIONS`` to this bot."); - }); - }).catch(err => { - if(err){ - message.channel.send("Please enable ``DMS`` for this server to use this command."); +bot.on('message', async (message) => { + if (message.author.bot) return; + + const server = await serverModel.findOne({ serverid: message.guild?.id }, async (err, data) => { + if (err) { + sendMessage(message.channel, "Error please contact a developer..."); + console.error(err); + } else { + return data } - }) + }) + + const PREFIX = server?.PREFIX || 'c!'; + + if (message.content.startsWith(PREFIX)){ + const cmdArguments = (message.content.slice(PREFIX.length).trim()).split(" ") + return handleCommand(message, cmdArguments, server) + } + + if (message.channel.id !== server?.countingChannel) return; + if (isNaN(message.content)) return deleteMessage(message) + if (server.lastPerson == message.author.id) { + deleteMessage(message) + return message.author.send('You were the last person to count, so you cannot count again!').catch(err => { }); } - }else{ - var serverMade; - const server = serverModel.findOne({ serverid: message.guild.id }, (err, data) => { - if(err){ - message.channel.send("Error please contact a developer..."); - console.log("Error: ", err); - }else{ - if(data){ - PREFIX = data.PREFIX; - serverMade = true; - }else{ - PREFIX = 'c!' - serverMade = false; + + server.counter++; + if (server.counter !== parseInt(message.content)) { + await deleteMessage(message) + return message.author.send(`That is the wrong number! Next number is: ${"`"}${server.counter}${"`"}.`).catch(err => { }); + } + + server.updateOne({ counter: server.counter, lastPerson: message.author.id }).then(() => { + react(message, '👍') + }).catch(err => { + if (err) { + sendMessage(message.channel, 'A fatal error occoured...'); + console.log('Error: ', err); } - } - if(message.content.startsWith(PREFIX)){ - if(message.content.toLowerCase() == `${PREFIX}setchannel`){ - if(!message.member.permissions.has(['MANAGE_GUILD'])){ - return message.channel.send("In order to use this command you must have the permission ``MANAGE_GUILD``"); - } - if(serverMade){ + }) +}) + +async function deleteMessage(message){ + await message.delete().catch(err => { + sendMessage(message.channel, "Error: invalid permissions, please enable ``MANAGE_MESSAGES`` for this bot."); + }) +} + +function sendMessage(message, content, user){ + message.channel.send(content).catch(err => {}) +} + +function react(message, reaction){ + message.react(reaction).catch(err => { + sendMessage(message.channel, "Error: Invalid permissions, please enable the permission ``ADD_REACTIONS`` to this bot."); + }); +} + +async function handleCommand(message, cmdArguments, server){ + switch(cmdArguments[0]){ + case `setchannel`: + if (!message.member.permissions.has(['MANAGE_GUILD'])) return sendMessage(message.channel, "In order to use this command you must have the permission ``MANAGE_GUILD``"); + if (server) return serverModel({ serverid: message.guild.id, countingChannel: message.channel.id, PREFIX: 'c!', lastPerson: "0", counter: "0" }).save(err => { + if (err) { + sendMessage(message.channel, 'I wasnt able to set this channel.'); + console.error(err); + } else { + sendMessage(message.channel, `This channel was set to the counting channel! Current count: ${"`"}0${"`"}, next number: ${"`"}1${"`"}`); + } + }); + server.updateOne({ countingChannel: message.channel.id }).then(() => { - var count = parseInt(data.counter); - count++; - var black = "``"; - message.channel.send(`This channel was set to the counting channel! Current count: ${black}${data.counter}${black}, next number: ${black}${count}${black}`); + sendMessage(message.channel, `This channel was set to the counting channel! Current count: ${"`"}${server.counter}${"`"}, next number: ${"`"}${server.counter+1}${"`"}`); }).catch(err => { - if(err){ - message.channel.send('I wasnt able to set this channel.'); - console.log("Error: ", err); - } + if (err) { + sendMessage(message.channel, 'I wasnt able to set this channel.'); + console.error(err); + } }) - }else{ - serverModel({ serverid: message.guild.id, countingChannel: message.channel.id, PREFIX: 'c!', lastPerson: "0", counter: "0" }).save(err => { - if(err){ - message.channel.send('I wasnt able to set this channel.'); - console.log("Error: ", err); - }else{ - var black = "``"; - message.channel.send(`This channel was set to the counting channel! Current count: ${black}0${black}, next number: ${black}1${black}`); - } - }); - } - }else if(message.content.toLowerCase() == `${PREFIX}unsetchannel`){ - if(!message.member.permissions.has(['MANAGE_GUILD'])){ - return message.channel.send("In order to use this command you must have the permission ``MANAGE_GUILD``"); - } - if(serverMade){ + break; + case `unsetchannel`: + if (!message.member.permissions.has(['MANAGE_GUILD'])) return sendMessage(message.channel, "In order to use this command you must have the permission ``MANAGE_GUILD``"); + if (!server) return sendMessage(message.channel, "This channel is not a counting channel."); + server.updateOne({ countingChannel: '0' }).then(() => { - message.channel.send('This channel was unset and is now a regular text channel!'); + sendMessage(message.channel, 'This channel was unset and is now a regular text channel!'); }).catch(err => { - if(err){ - message.channel.send("I wasn't able to unset this channel..."); - console.log("Error: ", err); - } + if (err) { + sendMessage(message.channel, "I wasn't able to unset this channel..."); + console.error(err); + } }) - }else{ - serverModel({ serverid: message.guild.id, countingChannel: '0', PREFIX: 'c!', lastPerson: "0", counter: '0' }).save(err => { - if(err){ - message.channel.send("I wasn't able to unset this channel..."); - console.log("Error: ", err); - }else{ - message.channel.send("This channel was unset and is now a regular text channel!"); - } + break; + case `clear`: + if (!message.member.permissions.has(['MANAGE_GUILD'])) return sendMessage(message.channel, "In order to use this command you must have the permission ``MANAGE_GUILD``"); + + if (!server) return serverModel({ serverid: message.guild.id, countingChannel: '0', PREFIX: 'c!', lastPerson: "0", counter: '0' }).save(err => { + if (err) { + sendMessage(message.channel, "I wasn't able to reset this servers count."); + console.error(err); + } else { + sendMessage(message.channel, "The count for this server has been set to ``0`` :)"); + } }); - } - }else if(message.content.toLowerCase() == `${PREFIX}clear`){ - if(!message.member.permissions.has(['MANAGE_GUILD'])){ - return message.channel.send("In order to use this command you must have the permission ``MANAGE_GUILD``"); - } - if(serverMade){ + server.updateOne({ counter: '0', lastPerson: '0' }).then(() => { - message.channel.send("The count for this server has been set to ``0`` :)"); + sendMessage(message.channel, "The count for this server has been set to ``0`` :)"); }).catch(err => { - if(err){ - message.channel.send("I wasn't able to reset this servers count."); - console.log("Error: ", err); - } + if (err) { + sendMessage(message.channel, "I wasn't able to reset this servers count."); + console.error(err); + } }) - }else{ - serverModel({ serverid: message.guild.id, countingChannel: '0', PREFIX: 'c!', lastPerson: "0", counter: '0' }).save(err => { - if(err){ - message.channel.send("I wasn't able to reset this servers count."); - console.log("Error: ", err); - }else{ - message.channel.send("The count for this server has been set to ``0`` :)"); - } - }); - } - }else if(message.content.toLowerCase() == `${PREFIX}count`){ - if(serverMade){ - var count = parseInt(data.counter); - count++; - var black = "``"; - message.channel.send(`The count for this server is ${black}${data.counter}${black}, next number is ${count}`); - }else{ - serverModel({ serverid: message.guild.id, countingChannel: '0', PREFIX: 'c!', lastPerson: "0", counter: '0' }).save(err => { - if(err){ - message.channel.send("A fatal error occourerd, please contact the developers..."); - console.log("Error: ", err); - }else{ - message.channel.send('The count for this server is ``0``, next number is ``1``'); - } - }); - } - }else if(message.content.toLowerCase() == `${PREFIX}help`){ - const help = new MessageEmbed() - .setAuthor(message.author.username, message.author.displayAvatarURL()) + break; + case `count`: + sendMessage(message.channel, server ? `The count for this server is \`${server.counter}\`, next number is \`${server.counter+1}$\`` : `This channel is not a counting channel.`) + break; + case `help`: + const help = new MessageEmbed() .setTitle('Help!') .setDescription("Below is the help you requested...") .addField("General", "After using the command ``c!setchannel`` that channel will be **dedicated** to counting! Counting will start at 0, and you need to count __up__ from there.\nKeep in mind that you **cannot** count more then twice in a row, you must alternate counting with another person.") @@ -175,96 +157,38 @@ bot.on('message', message => { .setFooter("Thanks for using us!") .setTimestamp() .setColor('RANDOM'); - message.author.send(help).then(() => { - message.react('👍').catch(err => { - message.channel.send("Error: Invalid permissions, please enable the permission ``ADD_REACTIONS`` to this bot."); + message.author.send(help).then(() => { + react(message, '👍') + }).catch(err => { + if (err) { + sendMessage(message.channel, "Please enable ``DMS`` for this server to use this command."); + } + }) + break; + case `setprefix ${cmdArguments[1]}`: + if (!message.member.permissions.has(['MANAGE_GUILD'])) return sendMessage(message.channel, "In order to use this command you must have the permission ``MANAGE_GUILD``"); + + const newPrefix = cmdArguments[1]; + + if (!server) return serverModel({ serverid: message.guild.id, countingChannel: '0', PREFIX: newPrefix, lastPerson: "0", counter: '0' }).save(err => { + if (err) { + sendMessage(message.channel, "Unfortunately i wasn't able to change the prefix for this server."); + console.error(err); + } else { + sendMessage(message.channel, `Succesfully changed the prefix to ${"`"}${newPrefix}${"`"} :)`); + } }); - }).catch(err => { - if(err){ - message.channel.send("Please enable ``DMS`` for this server to use this command."); - } - }) - }else if(message.content.toLowerCase().startsWith(`${PREFIX}setprefix`)){ - if(!message.member.permissions.has(['MANAGE_GUILD'])){ - return message.channel.send("In order to use this command you must have the permission ``MANAGE_GUILD``"); - } - var newPrefix = message.content.substring(PREFIX.length + 9).trim(); - if(serverMade){ + server.updateOne({ PREFIX: newPrefix }).then(() => { - var black ="``"; - message.channel.send(`Succesfully changed the prefix to ${black}${newPrefix}${black} :)`); + sendMessage(message.channel, `Succesfully changed the prefix to ${"`"}${newPrefix}${"`"} :)`); }).catch(err => { - if(err){ - message.channel.send("Unfortunately i wasn't able to change the prefix for this server"); - console.log("Error: ", err); - } + if (err) { + sendMessage(message.channel, "Unfortunately i wasn't able to change the prefix for this server"); + console.error(err); + } }) - }else{ - serverModel({ serverid: message.guild.id, countingChannel: '0', PREFIX: newPrefix, lastPerson: "0", counter: '0' }).save(err => { - if(err){ - message.channel.send("Unfortunately i wasn't able to change the prefix for this server."); - console.log("Error: ", err); - }else{ - var black ="``"; - message.channel.send(`Succesfully changed the prefix to ${black}${newPrefix}${black} :)`); - } - }); - } - }else if(message.content.toLowerCase().startsWith(`${PREFIX}updatestatus`) && message.author.id == "your-user-id-here"){ - bot.user.setActivity(`People count and spam c!help because im cool. Bot in ${bot.guilds.cache.size} guilds`, { type: 'LISTENING' }).then(() => { - message.channel.send("Updated status!"); - }).catch(err => { - if(err){ - message.channel.send("Error!"); - console.log('Error: ', err); - } - }); - } - }else { - if(data == null){ - return; - } - if(message.channel.id === data.countingChannel){ - if(isNaN(message.content)){ - message.delete().catch(err => { - message.channel.send("Error: invalid permissions, please enable ``MANAGE_MESSAGES`` for this bot."); - }); - }else{ - var lastPerson = data.lastPerson; - if(lastPerson == message.author.id){ - message.delete().catch(err => { - message.channel.send("Error: Invalid permissions, please enable the permission ``MANAGE_MESSAGES`` for this bot."); - }); - message.author.send('You were the last person to count, so you cannot count again!').catch(err => {}); - return; - }else{ - var count = parseInt(data.counter); - count++; - if(count == parseInt(message.content)){ - server.updateOne({ counter: `${count}`, lastPerson: message.author.id }).then(() => { - message.react('👍').catch(err => { - message.channel.send("Error: Invalid permissions, please enable the permission ``ADD_REACTIONS`` to this bot."); - }); - }).catch(err => { - if(err){ - message.channel.send('A fatal error occoured...'); - console.log('Error: ', err); - } - }) - }else{ - message.delete().then(() => { - var black = "``"; - message.author.send(`That is the wrong number! Next number is: ${black}${count}${black}.`).catch(err => {}); - }).catch(err => { - message.channel.send("Error: invalid permissions, please enable the permission ``MANAGE_MESSAGES`` for this bot."); - }); - } - } - } - } - } - }) - } -}) + break; + } +} bot.login(token);