Created
July 4, 2020 15:39
-
-
Save cjavad/736deeeb0560a56f343838d6dfde5b58 to your computer and use it in GitHub Desktop.
Count down days and hours until event using vc channel name. Format: `;addcd | eventname | javascript passable date`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Discord = require('discord.js'); | |
const { JsonDB } = require('node-json-db'); | |
const { DataError } = require('node-json-db/dist/lib/Errors'); | |
const { Config } = require('node-json-db/dist/lib/JsonDBConfig'); | |
const db = new JsonDB(new Config('db', true, false, '/')); | |
const client = new Discord.Client(); | |
// Initialize db | |
try { | |
db.getData('/countdowns'); | |
} catch (error) { | |
if (error instanceof DataError) { | |
db.push('/countdowns', []); | |
} else { | |
throw error; | |
} | |
} | |
function getTimeUntilDate(targetDate) { | |
const currentDate = new Date(); | |
let delta = Math.abs(targetDate.getTime() - currentDate.getTime()) / 1000; | |
// calculate (and subtract) whole days | |
var days = Math.floor(delta / 86400); | |
delta -= days * 86400; | |
// calculate (and subtract) whole hours | |
var hours = Math.floor(delta / 3600) % 24; | |
delta -= hours * 3600; | |
return [days, hours]; | |
} | |
function addCountdown(guild, countdownName, countdownDate) { | |
const [days, hours] = getTimeUntilDate(countdownDate); | |
const name = `${countdownName}: ${days}d ${hours}h`; | |
guild.channels.create(name, { | |
position: 1, | |
type: 'voice', | |
permissionOverwrites: [ | |
{ | |
id: guild.roles.everyone, | |
deny: [ | |
'READ_MESSAGE_HISTORY', | |
'SEND_MESSAGES' | |
], | |
allow: [ | |
'VIEW_CHANNEL' | |
] | |
} | |
] | |
}) | |
.then((countdownChannel) => { | |
db.push('/countdowns[]', { | |
gid: guild.id, | |
cid: countdownChannel.id, | |
ts: countdownDate.getTime(), | |
name: countdownName | |
}); | |
}) | |
.catch(console.error); | |
} | |
async function updateCountdowns() { | |
for (const countdown of db.getData('/countdowns')) { | |
console.log(countdown); | |
const guild = client.guilds.resolve(countdown.gid); | |
let channel = guild.channels.resolve(countdown.cid); | |
const [days, hours] = getTimeUntilDate(new Date(countdown.ts)); | |
console.log(days, hours); | |
channel = await client.channels.fetch(countdown.cid); | |
if (days <= 0 && hours <= -12) { | |
await channel.delete(); | |
} else if (days <= 0 && hours <= 0) { | |
await channel.setName(`${countdown.name}: OUT NOW!!!`); | |
} else { | |
await channel.setName(`${countdown.name}: ${days}d ${hours}h`); | |
} | |
}; | |
} | |
async function main() { | |
updateCountdowns(); | |
setInterval(() => { | |
updateCountdowns(); | |
}, 3600000); | |
} | |
function argParse(prefix, seperator) { | |
return str => { | |
if (str.startsWith(prefix)) { | |
str = str.substring(1); | |
var args = str.split(seperator); | |
for (let i = 0; i < args.length; i++) { | |
args[i] = args[i].trim(); | |
} | |
var command = args[0]; | |
args.shift(); | |
return { | |
command: command, | |
args: args | |
}; | |
} | |
}; | |
}; | |
client.on('message', (message) => { | |
if (!message.member) return; | |
if (message.member.hasPermission('ADMINISTRATOR')) { | |
const parser = argParse(';', '|'); | |
const parsed = parser(message.content); | |
if (parsed !== undefined && parsed.command === 'addcd' && parsed.args.length === 2) { | |
const targetDate = parsed.args[1] | |
const date = new Date(targetDate); | |
console.log(date); | |
if (!isNaN(date.getTime())) { | |
addCountdown(message.guild, parsed.args[0], date); | |
} | |
} else if (parsed !== undefined && parsed.command !== 'addcd') { | |
message.channel.send('Wrong :('); | |
} | |
} | |
}); | |
client.on('ready', () => { | |
main(); | |
}); | |
client.login('discord token'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment