Skip to content

Commit

Permalink
timein command take args anywhere. Properly formated date it gives back.
Browse files Browse the repository at this point in the history
  • Loading branch information
MCE626 authored and MCE626 committed Nov 12, 2018
1 parent 43c6605 commit 8f20887
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 58 deletions.
3 changes: 1 addition & 2 deletions commands/random/time.js
Expand Up @@ -13,14 +13,13 @@ module.exports = class TimeCommand extends commando.Command{

async run(message, args){
const date = new Date();
const nowDate = new Date()
const embed = new discord.RichEmbed();

embed.setColor(0x007fed);
embed.setTitle(`__${message.author.username}'s Time Info__`);
embed.addField("Your 24hr Time", date.toTimeString(), true);
embed.addField("Date", (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear() + "\n" + date.toDateString(), true);
embed.addField("Day of the Week", date.getDay(), true);
embed.addField("Day of the Week", (date.getDay() + 1), true);
embed.addField("UTC Time", date.toUTCString(), true);

message.channel.send({embed});
Expand Down
135 changes: 79 additions & 56 deletions commands/random/timein.js
Expand Up @@ -7,78 +7,101 @@ module.exports = class TimeInCommand extends commando.Command{
name: 'timein',
group: 'random',
memberName: 'timein',
description: 'What will the time be in *xx* hours/days/months/etc from now?',
examples: ['timein 1h 2m - *1 hour and 2 minutes*', 'timein 4d 2mo - *4 days, 2 months*', 'timein 1d 5h - *1 day, 5 hours*', 'timein 5y 3d - *5 years, 3 days*'],
description: 'What will the time be in *xx* hours/minutes/days/months/years (h,m,d,M,y) from now?',
examples: ['timein 1h 2m - *1 hour and 2 minutes*', 'timein 4d 2M - *4 days, 2 months*', 'timein 1d 5h - *1 day, 5 hours*', 'timein 5y 3d - *5 years, 3 days*'],
args: [
{
key: 'hours',
prompt: 'How many hours?',
type: 'string',
default: ''
},
{
key: 'mins',
prompt: 'How many minutes?',
type: 'string',
default: ''
},
{
key: 'days',
prompt: 'How many days?',
type: 'string',
default: ''
},
{
key: 'months',
prompt: 'How many months?',
type: 'string',
default: ''
},
{
key: 'years',
prompt: 'How many years?',
type: 'string',
default: ''
key: 'time',
prompt: 'What time will it be in xx hours, xx minutes, xx days, xx months and xx years?',
type: 'string'
}
]
});
}

/*
Whole lot of Stuff. All this makes it so command can do "1h 6d 4y" or "5M 1m 7h" or any combination.
Args are an array, each item is a string. This goes through each array item, and finds a specific (case-sensitive) char. Such as "h" or "d", etc. Then if slices off the character leaving the number behind. That number is then used to calculate the new date.
Slice here slices off stuff from strings, and here i have it start at 0 and the end is the string's length - 1 to only cut off the character. This is so you can have more than one number for the value. Such as 34d or 150d would return only the number.
*/
async run(message, args){
args = message.content.slice(`${this.client.commandPrefix}`.length + "timein".length).trim().split(/ +/g);
// Converts the arg type of string into an array. Couldn't get the args type value to be an array from the start..
args = message.content.slice(`${this.client.commandPrefix}`.length + 'timein'.length).trim().split(/ +/g);

let tempHours = args.indexOf("h");
message.content.tempHours.slice(this.length - 1);
var hours = tempHours;

let tempMins = args.indexOf("m");
message.content.tempMins.slice(this.length - 1);
var mins = tempMins;
// Go through array, and each item to find specific char
const strHours = args.filter(s => s.includes('h')).toString();
const strMins = args.filter(s => s.includes('m')).toString();
const strDays = args.filter(s => s.includes('d')).toString();
const strMonths = args.filter(s => s.includes('M')).toString();
const strYears =args.filter(s => s.includes('y')).toString();

// Cut off the char to leave only number left
if(strHours.includes('h')) var tempHours = strHours.slice(0, strHours.length-1); else var tempHours = 0;
if(strMins.includes('m')) var tempMins = strMins.slice(0, strMins.length-1); else var tempMins = 0;
if(strDays.includes('d')) var tempDays = strDays.slice(0, strDays.length-1); else var tempDays = 0;
if(strMonths.includes('M')) var tempMonths = strMonths.slice(0, strMonths.length-1); else var tempMonths = 0;
if(strYears.includes('y')) var tempYears = strYears.slice(0, strYears.length-1); else var tempYears = 0;

// Calculate new date
const newDate = new Date();
newDate.setHours(newDate.getHours() + Number(tempHours));
newDate.setMinutes(newDate.getMinutes() + Number(tempMins));
newDate.setDate(newDate.getDate() + Number(tempDays));
newDate.setMonth(newDate.getMonth() + Number(tempMonths));
newDate.setFullYear(newDate.getFullYear() + Number(tempYears));

const stringTitle = () =>{
var s = [];

if(Number(tempHours) > 0){
if(Number(tempHours) === 1)
s.push(`${tempHours} hour`);
else
s.push(`${tempHours} hours`);
}

if(Number(tempMins) > 0){
if(Number(tempMins) === 1)
s.push(`${tempMins} minute`);
else
s.push(`${tempMins} minutes`);
}

if(Number(tempDays) > 0){
if(Number(tempDays) === 1)
s.push(`${tempDays} day`);
else
s.push(`${tempDays} days`);
}

let tempDays = args.indexOf("d");
message.content.tempDays.slice(this.length - 1);
var days = tempDays;
if(Number(tempMonths) > 0){
if(Number(tempMonths) === 1)
s.push(`${tempMonths} month`);
else
s.push(`${tempMonths} months`);
}

let tempMonths = args.indexOf("M");
message.content.tempMonths.slice(this.length - 1);
var months = tempMonths;
if(Number(tempYears) > 0){
if(Number(tempYears) === 1)
s.push(`${tempYears} year`);
else
s.push(`${tempYears} years`);
}

let tempYears = args.indexOf("y");
message.content.tempYears.slice(this.length - 1);
var years = tempYears;
return s.join(', ');
}

const date = new Date();
date.setHours(date.getHours() + hours);
date.setMinutes(date.getMinutes() + mins);
date.setMonth(date.getMonth() + 1 + months);
date.setDate(date.getDate() + days);
date.setFullYear(date.getFullYear() + years);
const calculateFullMinutes = () => {
return (newDate.getMinutes()<10?'0':'') + newDate.getMinutes();
}

const embed = new discord.RichEmbed();
embed.setColor(0x007fed);
embed.setTitle(`__What will it be in ${hours} hours, ${mins} minutes, ${days} days, ${months} months and ${years} years from now?__`);
embed.addField("24hr Time", date.getHours() + ":" + date.getMinutes(), true);
embed.addField("Date", date.getMonth() + "/" + date.getDate() + "/" + date.getFullYear(), true);
console.log(stringTitle())
embed.setTitle('__What will it be in ' + stringTitle() + ' from now?__');
embed.addField("24hr Time", newDate.getHours() + ":" + calculateFullMinutes(), true);
embed.addField("Date", (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear(), true);

message.channel.send({embed});
}
Expand Down

0 comments on commit 8f20887

Please sign in to comment.