diff --git a/README.md b/README.md index bc1b51d..051ad0b 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ When you are finished running the install script, edit the settings.json file to "token": "YOURTOKEN", "debug": false, "fileLogging": true, - "memory": 50, + "memory": 500, "newsAPIKey": "https://newsapi.org/ to get your API key." } ``` diff --git a/commands.js b/commands.js index 0b369b0..e6bcf12 100644 --- a/commands.js +++ b/commands.js @@ -230,10 +230,6 @@ module.exports.registerCommands = async (client, mbot) => { client.on('message', async message => { //if (message.author.bot) return; - if (message.channel.type === 'dm') return; - if (!mute.guilds.has(message.guild.id)) { - mute.guilds.set(message.guild.id, new Discord.Collection()); - } if (!timer.users.has(message.author.id)) { timer.users.set(message.author.id, new Discord.Collection()); const user = timer.users.get(message.author.id); @@ -242,6 +238,25 @@ module.exports.registerCommands = async (client, mbot) => { user.set('timeouts', new Discord.Collection()); user.set('names', new Discord.Collection()); user.set('ids', new Discord.Collection()); + user.set('shortIds', new Discord.Collection()); + } + if (message.channel.type === 'dm') { + const args = message.content.split(' '); + const command = args.shift().toLowerCase(); + console.log(command, args); + if (message.author.bot) return; + switch (command) { + case "timer": + if (args.length < timer.minArgs) { + return message.channel.send(`Please add params! ${timer.name} ${timer.usage}`); + } + client.commands.get('timer').execute(message, args); + break; + } + return; + } + if (!mute.guilds.has(message.guild.id)) { + mute.guilds.set(message.guild.id, new Discord.Collection()); } const muted = mute.guilds.get(message.guild.id); if (muted.has(message.author.id)) { @@ -251,19 +266,19 @@ module.exports.registerCommands = async (client, mbot) => { const exp = mutes.get(message.author.id) + muted.get(message.author.id); if (now < exp) { const left = (exp - now) / 1000; - let out; - if (left.toFixed(1) >= 3600) { - let result = left.toFixed() / 3600; - result = Math.round(result); - out = `${result} hour(s)`; - } else if ((left.toFixed(1)) >= 60) { - let result = left.toFixed() / 60; - result = Math.round(result); - out = `${result} minute(s)`; + let timeLeft = Math.floor(left); + if (timeLeft >= 3600) { + const mins = Math.floor((left / 60) % 60); + timeLeft = Math.floor(timeLeft / 3600); + timeLeft += ` hour(s) and ${mins} minute(s)`; + } else if (timeLeft >= 60) { + const secs = Math.floor(left % 60); + timeLeft = Math.floor(timeLeft / 60); + timeLeft += ` minute(s) and ${secs} second(s)`; } else { - out = `${left.toFixed(1)} second(s)`; + timeLeft += ` second(s)`; } - return message.author.send(`You are currently muted on ${message.guild.name}. Please wait around ${out} before typing again!`).catch((err) => { + return message.author.send(`You are currently muted on ${message.guild.name}. Please wait ${timeLeft} before typing again!`).catch((err) => { if (err) return; }); } diff --git a/commands/mod/mute.js b/commands/mod/mute.js index 2436d46..a5d63d0 100644 --- a/commands/mod/mute.js +++ b/commands/mod/mute.js @@ -31,7 +31,7 @@ module.exports = { return message.channel.send(`${message.author} Could not find that user!`); } const time = parseInt(args[1]); - if (!time) { + if (isNaN(time)) { return message.channel.send(`${message.author} Please use numbers!`); } const muted = this.guilds.get(message.guild.id); @@ -53,24 +53,36 @@ module.exports = { if (mRole.comparePositionTo(role) > 0 || mRole.position === role.position) { return message.channel.send(`${message.author} That user has a higher role than you!`); } + const timeArray = args[1].split(':'); let out = "Error occured"; - const mil = tools.parseTime(args[1]); - if (hasMin(args[1]) && !hasHour(args[1])) { - const minutes = parseInt(args[1]); - if (minutes >= 60) { - out = `${Math.floor(minutes / 60)} hour(s)`; + let mil = tools.parseTime(args[1]); + if (timeArray.length <= 1) { + if (hasMin(args[1]) && !hasHour(args[1])) { + const minutes = parseInt(args[1]); + if (minutes >= 60) { + out = `${Math.floor(minutes / 60)} hour(s)`; + } else { + out = `${minutes} minute(s)`; + } + } else if (hasHour(args[1]) && !hasMin(args[1])) { + out = `${parseInt(args[1])} hour(s)`; } else { - out = `${minutes} minute(s)`; + const sec = parseInt(args[1]); + if (sec >= 60) { + out = `${Math.floor(sec / 60)} minute(s)`; + } else { + out = `${sec} second(s)`; + } } - } else if (hasHour(args[1]) && !hasMin(args[1])) { - out = `${parseInt(args[1])} hour(s)`; } else { - const sec = parseInt(args[1]); - if (sec >= 60) { - out = `${Math.floor(sec / 60)} minute(s)`; - } else { - out = `${sec} second(s)`; + if (timeArray.length < 3 || timeArray.length > 3) { + return message.channel.send(`${message.author} Please follow the time format! hh:mm:ss`); } + const hours = tools.parseTime(timeArray[0] + 'hour'); + const minutes = tools.parseTime(timeArray[1] + 'min'); + const seconds = tools.parseTime(timeArray[2]); + mil = hours + minutes + seconds; + out = `${timeArray[0]}:${timeArray[1]}:${timeArray[2]}`; } tools.muteMember(message.guild.id, mention.id, mil); return message.channel.send(`${message.author} muted ${mention} for ${out}!`); diff --git a/commands/util/timer.js b/commands/util/timer.js index 3d719ad..e819593 100644 --- a/commands/util/timer.js +++ b/commands/util/timer.js @@ -3,6 +3,9 @@ const tls = require('../../tools'); const tools = new tls.Tools(); const crypto = require('crypto'); const words = require('../../words.json'); +const { + bot_owners_id, +} = require('../../settings.json'); const minAlias = ['min', 'minute', 'm', 'minutes', 'mins']; const hourAlias = ['hour', 'hours', 'h', 'hr', 'hrs']; @@ -17,25 +20,34 @@ module.exports = { execute(message, args) { if (args[0].toLowerCase() === "cancel") { const user = this.users.get(message.author.id); - if (!user) { - return message.channel.send(`You don't have any timers set up right now!`); - } if (!args[1]) { return message.channel.send(`${message.author} Please provide your timers name!`); } const timerName = args.slice(1, args.length).join(' '); const timerId = user.get('names').get(timerName.toLowerCase()); if (!timerId) { - return message.channel.send('Could not find a timer by that name!'); + const ids = user.get('names').array(); + if (!ids.length) { + return message.channel.send(`You don't have any timers set up right now!`); + } + for (let i = 0; i < ids.length; i++) { + if (ids[i].substr(0, 6) === timerName) { + const name = user.get('ids').get(ids[i]); + tools.deleteTimer(message.author.id, ids[i], name); + return message.channel.send(`${message.author} Timer ${name} (${timerName}) canceled successfully!`); + } else { + return message.channel.send('No timers were found with that name or id!'); + } + } } tools.deleteTimer(message.author.id, timerId, timerName); - return message.channel.send(`${message.author} Timer ${timerName} deleted successfully!`); + return message.channel.send(`${message.author} Timer ${timerName} cancled successfully!`); } else if (args[0].toLowerCase() === "list") { const user = this.users.get(message.author.id); const names = user.get('names').array(); - const ids = user.get('ids'); let send = []; for (let i = 0; i < names.length; i++) { + const id = user.get('ids').get(names[i]); const exp = user.get('dates').get(names[i]) + user.get('timers').get(names[i]); const left = (exp - Date.now()) / 1000; let timeLeft = Math.floor(left); @@ -50,7 +62,8 @@ module.exports = { } else { timeLeft += ` second(s)`; } - send.push(`${ids.get(names[i])} - ${timeLeft}`); + const shortId = user.get('shortIds').get(id); + send.push(`${id} (${shortId}) - ${timeLeft}`); } send = send.join('\n'); if (send !== '') { @@ -62,33 +75,39 @@ module.exports = { if (!this.users.has(message.author.id)) { this.users.set(message.author.id, new Discord.Collection()); } + for (let i = 0; i < bot_owners_id.length; i++) { + if (this.users.get(message.author.id).get('timers').array().length === 3 && message.author.id !== bot_owners_id[i]) { + return message.channel.send(`${message.author} You have reached the maximum number of timers that can be created!`); + } + } const time = parseInt(args[0]); - if (!time && !args[0].startsWith('t:')) { + if (isNaN(time)) { return message.channel.send(`${message.author} Please use numbers!`); } + const timeArray = args[0].split(':'); let out = "Error occured"; let mil = tools.parseTime(args[0]); - if (hasMin(args[0]) && !hasHour(args[0])) { - const minutes = parseInt(args[0]); - if (minutes >= 60) { - out = `${Math.floor(minutes / 60)} hour(s)`; + if (timeArray.length <= 1) { + if (hasMin(args[0]) && !hasHour(args[0])) { + const minutes = parseInt(args[0]); + if (minutes >= 60) { + out = `${Math.floor(minutes / 60)} hour(s)`; + } else { + out = `${minutes} minute(s)`; + } + } else if (hasHour(args[0]) && !hasMin(args[0])) { + out = `${parseInt(args[0])} hour(s)`; } else { - out = `${minutes} minute(s)`; + const sec = parseInt(args[0]); + if (sec >= 60) { + out = `${Math.floor(sec / 60)} minute(s)`; + } else if (sec >= 3600) { + out = `${Math.floor(sec / 3600)} hour(s)`; + } else { + out = `${sec} second(s)`; + } } - } else if (hasHour(args[0]) && !hasMin(args[0])) { - out = `${parseInt(args[0])} hour(s)`; } else { - const sec = parseInt(args[0]); - if (sec >= 60) { - out = `${Math.floor(sec / 60)} minute(s)`; - } else if (sec >= 3600) { - out = `${Math.floor(sec / 3600)} hour(s)`; - } else { - out = `${sec} second(s)`; - } - } - if (args[0].startsWith('t:')) { - const timeArray = args[0].split(':').slice(1); if (timeArray.length < 3 || timeArray.length > 3) { return message.channel.send(`${message.author} Please follow the time format! hh:mm:ss`); } @@ -108,13 +127,13 @@ module.exports = { wordNumbers += name.split(' ').join('_'); } const timerId = crypto.createHash('md5').update(wordNumbers).digest('hex'); - const timerName = name === '' ? 'No name provided' : name; + const timerName = name === '' ? 'no name provided' : name; if (this.users.get(message.author.id).get('names').has(timerName)) { return message.channel.send('A timer with that name already exists!'); } require('../../logger').debug(wordNumbers); tools.createTimer(message.author.id, mil, timerId, timerName.toLowerCase()); - return message.channel.send(`${message.author} Successfully created timer that will go off in ${out}\nName: ${timerName}`); + return message.channel.send(`${message.author} Successfully created timer that will go off in ${out}\nName: ${timerName} (${timerId.substr(0, 6)})`); }, }; diff --git a/package-lock.json b/package-lock.json index c21e6b8..960be55 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mbot", - "version": "7.5.1", + "version": "7.5.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 13db224..2d80115 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mbot", - "version": "7.5.1", + "version": "7.5.2", "description": "Private discord bot", "main": "mbot.js", "scripts": { diff --git a/tools.js b/tools.js index 64c54dc..ef55c26 100644 --- a/tools.js +++ b/tools.js @@ -783,6 +783,7 @@ class Tools { user.get('timers').set(timerId, time); user.get('names').set(timerName, timerId); user.get('ids').set(timerId, timerName); + user.get('shortIds').set(timerName, timerId.substr(0, 6)); const timeout = setTimeout(() => { this.deleteTimer(userId, timerId, timerName); mbot.event.emit('timerFinished', userId, timerId, timerName); @@ -800,6 +801,7 @@ class Tools { user.get('timeouts').delete(timerId); user.get('names').delete(timerName); user.get('ids').delete(timerId); + user.get('shortIds').delete(timerName); } } module.exports.Tools = Tools;