Skip to content

Commit

Permalink
Merge pull request #31 from muricans/mbot-testing
Browse files Browse the repository at this point in the history
7.5.2
  • Loading branch information
muricans authored Jun 4, 2019
2 parents 43206e7 + 4065053 commit 10c24ad
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
```
Expand Down
45 changes: 30 additions & 15 deletions commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand All @@ -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;
});
}
Expand Down
40 changes: 26 additions & 14 deletions commands/mod/mute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}!`);
Expand Down
75 changes: 47 additions & 28 deletions commands/util/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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);
Expand All @@ -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 !== '') {
Expand All @@ -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`);
}
Expand All @@ -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)})`);
},
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mbot",
"version": "7.5.1",
"version": "7.5.2",
"description": "Private discord bot",
"main": "mbot.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 10c24ad

Please sign in to comment.