Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ module.exports = {
'eslint-comments/no-unlimited-disable': 'error',
'eslint-comments/no-unused-disable': 'error',
'eslint-comments/no-unused-enable': 'error',

'no-plusplus': 'off',
'no-restricted-globals': "off",
'radix': "off",
"consistent-return": "off",
},
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"dependencies": {
"common-tags": "^1.8.0",
"discord.js": "^11.5.1",
"got": "^9.6.0"
"got": "^9.6.0",
"moment": "^2.24.0",
"timestring": "^6.0.0"
},
"devDependencies": {
"eslint": "^5.16.0",
Expand Down
28 changes: 28 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ class Utils {
}
return String(bool);
}

/**
* @description Takes a simple string and splits up the messages
* surrounded by the divider. (Strings not on divider will be ignored)
* @param {string} string - String to split.
* @param {string} divider - Where the string would be splitted.
* @returns {Array<string>} Array of string splitted.
*/
static splitOnDivider(string, divider) {
let open = false;
let message = '';
const array = [];
for (let i = 0; i < string.length; i++) {
const char = string.charAt(i);
if (char === divider) {
if (!open) {
open = true;
} else {
open = false;
array.push(message);
message = '';
}
} else if (open) {
message += char;
}
}
return array;
}
}

module.exports = Utils;
121 changes: 121 additions & 0 deletions src/modules/Giveaway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const { Collection, RichEmbed } = require('discord.js');
const moment = require('moment');

const active = new Collection();

class Giveaway {
constructor({ time, description, price, winners, channelID, client }) {
this.client = client;
this.time = time;
this.price = price;
this.winners = isNaN(winners) ? 1 : parseInt(winners);
this.description = description;
this.messageID = null;
this.channelID = channelID;
this.interval = null;
this.paused = false;
this.users = [];
this.pausedTime = 0;
}

run() {
return new Promise(resolve => {
const interval = () => {
const every = Math.sqrt(this.getTimeLeft() / 60000);
this.interval = setTimeout(async () => {
const message = await this.client.channels
.get(this.channelID)
.fetchMessage(this.messageID);

if (this.getTimeLeft() < 1) {
clearTimeout(this.interval);
await message.edit(this.embed());
return resolve(this);
}
await message.edit(this.embed());
interval();
}, Math.floor(every * 60000));
};
interval();
});
}

embed() {
let duration = `Ends in **${moment
.duration(this.getTimeLeft(), 'ms')
.humanize()}**`;
if (this.getTimeLeft() < 1) {
const winners = this.drawWinners();
winners.forEach(winner => {
this.client.users
.get(winner)
.send(`You wont the giveaway: **${this.price}** :tada: :tada:`);
});
duration = `**Giveaway ended**\nWinner: <@${winners.join(', ')}>`;
} else if (this.paused) duration = '**Giveaway Paused**';
return new RichEmbed()
.setTitle(this.price)
.setDescription(
`
${this.description ? `\n${this.description}\n` : ''}
${duration}
Users participating: **${this.users.length}**
Total winners: **${this.winners}**
`,
)
.setFooter('Click the reaction to enter!')
.setTimestamp(this.time)
.setColor(this.getTimeLeft() > 0 ? 'green' : 'red');
}

getTimeLeft() {
return this.time - Date.now();
}

async start() {
const channel = await this.client.channels.get(this.channelID);

const message = await channel.send(this.embed());
await message.react('🎉');
this.messageID = message.id;

active.set(this.messageID, this);
return this.run();
}

async pause() {
this.paused = true;
this.pausedTime = Date.now();
clearTimeout(this.interval);
const channel = await this.client.channels.get(this.channelID);
return channel.send(this.embed());
}

resume() {
this.paused = false;
this.pausedTime = 0;
this.run();
}

enter(userID) {
this.users.push(userID);
}

drawWinners() {
const winners = [];
for (let i = 0; i < this.winners; i++) {
const user = this.users[Math.floor(Math.random() * this.users.length)];
if (!winners.includes(`<@${user}>`)) winners.push(`<@${user}>`);
else i--;

if (this.users.length === i + 1) break;
}

return winners;
}
}

module.exports = {
Giveaway,
activeGiveaways: active,
};
45 changes: 45 additions & 0 deletions src/modules/general/commands/giveaway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const moment = require('moment');
const timestring = require('timestring');

const { Command } = require('../../../handler');
const Utils = require('../../../Utils.js');
const { Giveaway } = require('../../Giveaway');

module.exports = class extends Command {
constructor({ client }) {
super('giveaway', {
aliases: ['ga'],
info:
'Wanna give things to people? Use this command to create giveaways.',
usage: 'giveaway "[time]" "[title]" "{description}" {winners}',
guildOnly: false,
});

this.client = client;
}

run(message, args) {
const gArgs = Utils.splitOnDivider(args.join(' '), '"');

if (gArgs.length < 2) {
return message.reply('Wrong usage');
}

const time = timestring(gArgs[0], 'ms');
const price = gArgs[1];
const description = gArgs[2];

const giveaway = new Giveaway({
time: moment()
.add(time)
.toDate()
.getTime(),
description,
price,
winners: args[args.length - 1],
channelID: message.channel.id,
client: this.client,
});
return giveaway.start();
}
};
21 changes: 21 additions & 0 deletions src/modules/general/events/reactionAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { Event } = require('../../../handler');
const { activeGiveaways } = require('../../Giveaway');

module.exports = class extends Event {
constructor() {
super('messageReactionAdd');
}

run(client, reaction, user) {
if (user.bot) return;

const message = reaction.message;
if (activeGiveaways.has(message.id)) {
const emoji = reaction.emoji;
if (emoji.name !== '🎉') return;

const giveaway = activeGiveaways.get(message.id);
giveaway.enter(user.id);
}
}
};