Skip to content

Commit

Permalink
Use <:name:id> format for manamoji.
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckharmston committed Jan 10, 2017
1 parent c9464d2 commit 06aba6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Messenger {
makePromise(cardName, responseType) {
return new Promise((resolve, reject) => {
try {
new responseType(cardName).embed().then(embed => {
new responseType(this.client, cardName).embed().then(embed => {
resolve(embed);
});
} catch(err) {
Expand Down
29 changes: 15 additions & 14 deletions lib/middleware/manamoji.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let substitutions = {
'CHAOS': ':manachaos:',
'{∞}': ':manainfinity:',
'{½}': ':manahalf:',
'{hr}': ':manahr:'
'CHAOS': 'manachaos',
'{∞}': 'manainfinity',
'{½}': 'manahalf',
'{hr}': 'manahr'
};

let COLORS = ['W', 'U', 'B', 'R', 'G'];
Expand All @@ -13,7 +13,7 @@ function _(before, after) {
if (typeof after === 'undefined') {
after = before;
}
substitutions[`{${before}}`] = `:mana${after.toString().toLowerCase()}:`;
substitutions[`{${before}}`] = `mana${after.toString().toLowerCase()}`;
}

ADDTL.forEach(a => { _(a) });
Expand All @@ -25,17 +25,18 @@ COLORS.forEach(c => { COLORS.forEach(d => {
}) });
NUMBERS.forEach(n => { _(n) });

function manamoji(str) {
const re = new RegExp(Object.keys(substitutions).map(v => {
return v.replace('{', '\\{').replace('}', '\\}');
}).join('|'), 'gi');
return str.replace(re, matched => {
return substitutions[matched];
});
}

module.exports = (client, embed) => {
function manamoji(str) {
const re = new RegExp(Object.keys(substitutions).map(v => {
return v.replace('{', '\\{').replace('}', '\\}');
}).join('|'), 'gi');
return str.replace(re, matched => {
const emoji = client.emojis.find('name', substitutions[matched]);
return emoji ? emoji.toString() : matched;
});
}

module.exports = embed => {
if (embed.title) {
embed.title = manamoji(embed.title);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/response-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const Url = require('urijs');

const manamoji = require('./middleware/manamoji');


class TextResponse {
constructor(cardName) {
constructor(client, cardName) {
this.client = client;
this.cardName = cardName;
}

Expand Down Expand Up @@ -39,7 +39,7 @@ class TextResponse {
let parts = response.body.split('\n');
const embedTitle = parts.shift();
return {
title: embedTitle,
title: `${embedTitle}`,
description: parts.join('\n'),
url: response.headers['x-scryfall-card'],
thumbnail: {
Expand All @@ -53,7 +53,7 @@ class TextResponse {
this.makeRequest().then(response => {
let embed = this.makeEmbed(response);
this.middleware.length > 0 && this.middleware.forEach(mw => {
embed = mw(embed);
embed = mw(this.client, embed);
});
resolve(embed);
});
Expand Down

0 comments on commit 06aba6e

Please sign in to comment.