Skip to content
Open
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
230 changes: 230 additions & 0 deletions utils/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
const colors = {
WHITE: '\x0300',
BLACK: '\x0301',
NAVY: '\x0302',
GREEN: '\x0303',
RED: '\x0304',
BROWN: '\x0305',
MAROON: '\x0305',
PURPLE: '\x0306',
VIOLET: '\x0306',
ORANGE: '\x0307',
YELLOW: '\x0308',
LIGHTGREEN: '\x0309',
LIME: '\x0309',
TEAL: '\x0310',
BLUECYAN: '\x0310',
CYAN: '\x0311',
AQUA: '\x0311',
BLUE: '\x0312',
ROYAL: '\x0312',
LIGHTPURPLE: '\x0313',
PINK: '\x0313',
FUCHSIA: '\x0313',
GREY: '\x0314',
GRAY: '\x0314',
LIGHTGRAY: '\x0315',
LIGHTGREY: '\x0315',
SILVER: '\x0315',

NORMAL: '\x0F',
UNDERLINE: '\x1F',
BOLD: '\x02',
ITALIC: '\x1D',
REVERSE: '\u202E'
};

const _rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'navy', 'violet'];

/**
* Colors a message
*/
class Color extends String {
/**
* @param {string} message - The message you want to add rainbow colors too
*/
constructor(message) {
super(message);
}

white() {
return new Color(`${colors.WHITE}${this}${colors.NORMAL}`);
}

black() {
return new Color(`${colors.BLACK}${this}${colors.NORMAL}`);
}

navy() {
return new Color(`${colors.NAVY}${this}${colors.NORMAL}`);
}

green() {
return new Color(`${colors.GREEN}${this}${colors.NORMAL}`);
}

red() {
return new Color(`${colors.RED}${this}${colors.NORMAL}`);
}

brown() {
return new Color(`${colors.BROWN}${this}${colors.NORMAL}`);
}

maroon() {
return new Color(`${colors.MAROON}${this}${colors.NORMAL}`);
}

purple() {
return new Color(`${colors.PURPLE}${this}${colors.NORMAL}`);
}

violet() {
return new Color(`${colors.VIOLET}${this}${colors.NORMAL}`);
}

orange() {
return new Color(`${colors.ORANGE}${this}${colors.NORMAL}`);
}

yellow() {
return new Color(`${colors.YELLOW}${this}${colors.NORMAL}`);
}

lightgreen() {
return new Color(`${colors.LIGHTGREEN}${this}${colors.NORMAL}`);
}

lime() {
return new Color(`${colors.LIME}${this}${colors.NORMAL}`);
}

teal() {
return new Color(`${colors.TEAL}${this}${colors.NORMAL}`);
}

bluecyan() {
return new Color(`${colors.BLUECYAN}${this}${colors.NORMAL}`);
}

cyan() {
return new Color(`${colors.CYAN}${this}${colors.NORMAL}`);
}

aqua() {
return new Color(`${colors.AQUA}${this}${colors.NORMAL}`);
}

blue() {
return new Color(`${colors.BLUE}${this}${colors.NORMAL}`);
}

royal() {
return new Color(`${colors.ROYAL}${this}${colors.NORMAL}`);
}

lightpurple() {
return new Color(`${colors.LIGHTPURPLE}${this}${colors.NORMAL}`);
}

pink() {
return new Color(`${colors.PINK}${this}${colors.NORMAL}`);
}

fuchsia() {
return new Color(`${colors.FUCHSIA}${this}${colors.NORMAL}`);
}

grey() {
return new Color(`${colors.GREY}${this}${colors.NORMAL}`);
}

gray() {
return new Color(`${colors.GRAY}${this}${colors.NORMAL}`);
}

lightgray() {
return new Color(`${colors.LIGHTGRAY}${this}${colors.NORMAL}`);
}

lightgrey() {
return new Color(`${colors.LIGHTGREY}${this}${colors.NORMAL}`);
}

silver() {
return new Color(`${colors.SILVER}${this}${colors.NORMAL}`);
}


bold() {
return new Color(`${colors.BOLD}${this}${colors.NORMAL}`);
}

italic() {
return new Color(`${colors.ITALIC}${this}${colors.NORMAL}`);
}

underline() {
return new Color(`${colors.UNDERLINE}${this}${colors.NORMAL}`);
}

reverse() {
return new Color(`${colors.ITALIC}${this}${colors.NORMAL}`);
}

/**
* Colors a message with rainbow colors
* @return {string} - Returns your message returned with rainbow coloring
*/
rainbow() {
let i = 0;
let colored = '';

for (let character of this) {
if (i > (_rainbow.length - 1)) // We substract one because i starts at 0 and rainbow.length at 1
i = 0;

colored += `${colors[_rainbow[i].toUpperCase()]}${character}`;
i += 1;
}

return new Color(colored.concat('\x0F'));
}
}

/**
* Colors a message with rainbow colors
* @param {string} msg - The message you want to add rainbow colors too
* @param {string} bg - The message you want to add rainbow colors too
* @return {string} - Returns your message returned with rainbow coloring
*/
function add_background(msg, bg) {
let c = msg.indexOf('\x03') !== -1;

if (c && bg !== null) {
return `${msg.slice(0, 3)},${colors[bg.toUpperCase()].slice(1)}${msg.slice(3)}`;
} else if (!c && bg !== null) {
return `${colors.BLACK},${colors[bg.toUpperCase()].slice(1)}${msg}\x0F`;
} else if (bg === null) {
return msg;
}
}


function addStyling(msg, background, rainbow, style) {
let myRe = /\${(\w+)}/g;
let myArray;

while ((myArray = myRe.exec(msg)) !== null) {
msg.replace(myArray[0], colors[myArray[1]]);
}

return ((...args)=>{})(add_background(msg, background), style);
}

module.exports = {
Color,
colors,
add_background,
addStyling
};
21 changes: 13 additions & 8 deletions wrappers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { range } = require('node-python-funcs');
const { chunks } = require('./utils/general');
const colors = require('./utils/colors');

/** Class that provides methods for IRC commands */
class ConnectionWrapper {
Expand All @@ -25,27 +26,31 @@ class ConnectionWrapper {
* @func
* @param {object} event - The event object created when parsing the incoming messages.
* @param {string} message - The message you wish to reply with.
* @param {string} [background=null]
* @param {boolean} [rainbow=false]
* @param {string} [style=null]
*/
reply(event, message) {
if (event.target === this.bot.nickname) {
this.privmsg(event.source.nick, message);
} else {
this.privmsg(event.target, message);
}
reply(event, message, background=null, rainbow=false, style=null) {
let isPRIVMSG = event.target === this.bot.nickname;

this.privmsg(isPRIVMSG ? event.source.nick : event.target, message, background, rainbow, style);
}

/**
* @func
* @param {string} target - The user or channel you wish to send a PRIVMSG to.
* @param {string} message - The message you wish to send.
* @param {string} [background=null]
* @param {boolean} [rainbow=false]
* @param {string} [style=null]
*/
privmsg(target, message) {
privmsg(target, message, background=null, rainbow=false, style=null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better for plugins/root to add colours to the message before calling message functions with it hence the extra three arguments wouldn't be necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it would be nice if colours were provided as a util. Maybe something like

const colors = require('../utils/colors.js');
colors("some text").red().bold();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't know how to do that kind of function, with the chained functions

const channel = target.startsWith('#') ? target : this.bot.state.channels.keys()[0];
const db = this.bot.state.channels[channel].users[this.bot.nickname];
// The maximum length for messages is 512 bytes total including nick, ident & host
const MAXLEN = 512 - 2 - Buffer.byteLength(db.hostmask); // 1 for beggining double colon
const MSGLEN = MAXLEN - Buffer.byteLength(`PRIVMSG ${target} :\r\n`);
const msg = Buffer.from(message);
const msg = Buffer.from(colors.addStyling(message, background, rainbow, style));

for (const i of range(0, msg.byteLength, MSGLEN)) {
this.bot.send(`PRIVMSG ${target} :${msg.slice(i, i + MSGLEN).toString()}`);
Expand Down