Skip to content

Commit

Permalink
Fixed linting errors. Added message variable to resubscriptions. Adde…
Browse files Browse the repository at this point in the history
…d reconnect messages for clarity
  • Loading branch information
d0p3t committed Jul 10, 2018
1 parent b8d00a0 commit feca20c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div align="center">

# SubNotifier (v1.0.4)
# SubNotifier (v1.0.5)

<img src="http://i.imgur.com/gZkK7Yu.png" width="200px" height="200px">

##### A Simple Sub and Resub Notifier for Twitch Chat

![Version](https://img.shields.io/badge/version-1.0.4-green.svg) [![Discord](https://img.shields.io/discord/330910293934997504.svg)](https://discord.gg/bSd4cYJ) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)]()
![Version](https://img.shields.io/badge/version-1.0.5-green.svg) [![Discord](https://img.shields.io/discord/330910293934997504.svg)](https://discord.gg/bSd4cYJ) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)]()

### Want to use SubNotifier but need a Host? Follow + Tweet me [@d0p3t](https://twitter.com/d0p3t)

Expand Down Expand Up @@ -141,6 +141,11 @@ Also a great thank you to [Ikatzuki](https://twitter.com/lolIkatzuki) and [dinu]
---

## Changelog
v1.0.5 (10 July 2018)
* Added `message` variable support to resubscriptions
* Added reconnecting and reconnected messages for clarity
* Fixed linting errors (1 remaining due to twitch-js constructor)

v1.0.4 (20 June 2018)
* Fixed security vulnerabilities
* Upgraded package.json
Expand Down
20 changes: 10 additions & 10 deletions src/config/config.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,44 @@ const Config = {
subscriptions: {
custom1: 'PogChamp SUB Thank you for subscribing {{username}}',
custom2: 'CoolStoryBob SUB Thank you for subscribing {{username}}',
custom3: 'DansGame SUB Thank you for subscribing {{username}}'
custom3: 'DansGame SUB Thank you for subscribing {{username}}',
},
giftsubscriptions: {
custom1: 'PogChamp GIFTED SUB Thank you for gifting {{recipient}} a subscription, {{username}}',
custom2: 'CoolStoryBob GIFTED SUB Thank you for gifting {{recipient}} a subscription, {{username}}',
custom3: 'DansGame GIFTED SUB Thank you for gifting {{recipient}} a subscription, {{username}}'
},
custom3: 'DansGame GIFTED SUB Thank you for gifting {{recipient}} a subscription, {{username}}',
},
resubscriptions: {
custom1: 'PogChamp RESUB Thank you {{username}} for resubscribing for {{months}} months! {{years}}',
custom2: 'WutFace RESUB Thank you {{username}} for resubscribing for {{months}} months! {{years}}',
custom3: '<3 RESUB Thank you {{username}} for resubscribing for {{months}} months! {{years}}'
custom3: '<3 RESUB Thank you {{username}} for resubscribing for {{months}} months! {{years}}',
},
bits: {
custom1: 'Wowzers, {{username}} cheered {{bits}} BITS!',
custom2: 'Oh so kind, {{username}} with the {{bits}} BITS!',
custom3: 'LUL we got a happy cheerer over here ({{username}}: {{bits}} bits)!'
custom3: 'LUL we got a happy cheerer over here ({{username}}: {{bits}} bits)!',
},
},
'#channel2': {
subscriptions: {
custom1: '',
custom2: '',
custom3: ''
custom3: '',
},
giftsubscriptions: {
custom1: '',
custom2: '',
custom3: ''
},
custom3: '',
},
resubscriptions: {
custom1: '',
custom2: '',
custom3: ''
custom3: '',
},
bits: {
custom1: '',
custom2: '',
custom3: ''
custom3: '',
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const bot = new TwitchBot(
Config.clientId,
Config.username,
Config.token,
Config.channels);
Config.channels,
);

bot.Start();

Expand Down
46 changes: 28 additions & 18 deletions src/lib/TwitchBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,37 @@ class TwitchBot {
this._messagesCount = [];

for (let i = 0; i < this._channels.length; i += 1) {
const channel = this._channels[i];
this._messagesCount.push({ channel: channel });
const allMsgs = Object.getOwnPropertyNames(Config.customMessages[channel]);
this._messagesCount[channel] = { subscriptions: 0, resubscriptions: 0, bits: 0, giftsubscriptions: 0 };
const thisChannel = this._channels[i];
this._messagesCount.push({ channel: thisChannel });
const allMsgs = Object.getOwnPropertyNames(Config.customMessages[thisChannel]);
this._messagesCount[thisChannel] = {
subscriptions: 0, resubscriptions: 0, bits: 0, giftsubscriptions: 0,
};
for (const type of allMsgs) {
const messagesOfThisType = Config.customMessages[channel][type];
for (let message in messagesOfThisType) {
if (messagesOfThisType.hasOwnProperty(message)) {
++this._messagesCount[channel][`${type}`];
Logger.debug(`FOUND [ ${type} ] message for channel [ ${channel} ]`);
const messagesOfThisType = Config.customMessages[thisChannel][type];
for (const message in messagesOfThisType) {
if (Object.prototype.hasOwnProperty.call(messagesOfThisType, message)) {
this._messagesCount[thisChannel][`${type}`] = this._messagesCount[thisChannel][`${type}`] + 1;
Logger.debug(`FOUND [ ${type} ] message for channel [ ${thisChannel} ]`);
}
}
if (this._messagesCount[type] < 1) {
Logger.error(`You MUST have at least 1 custom message for ${type} in channel [ ${channel} ]!`);
Logger.error(`You MUST have at least 1 custom message for ${type} in channel [ ${thisChannel} ]!`);
process.exit(1);
} else {
Logger.debug(`COUNTED ${this._messagesCount[channel][type]} ${type} messages for channel [ ${channel} ]`);
Logger.debug(`COUNTED ${this._messagesCount[thisChannel][type]} ${type} messages for channel [ ${thisChannel} ]`);
}
}
Logger.debug(`TOTAL MESSAGES IN CHANNEL [ ${channel} ] :`);
Logger.debug(JSON.stringify(this._messagesCount[channel]));
Logger.debug(`TOTAL MESSAGES IN CHANNEL [ ${thisChannel} ] :`);
Logger.debug(JSON.stringify(this._messagesCount[thisChannel]));
}

Logger.info('ENABLED Custom Messages!');
}

this._client.connect()
.then((data) => {
Logger.debug(`CONNECTED to chat on ${data[0]}:${data[1]}`);
Logger.info('CONNECTED: Waiting for events...');
Logger.debug(`CONNECTION ESTABLISHED on ${data[0]}:${data[1]}`);
})
.catch((err) => {
Logger.error(`ERROR Could not connect to chat (${err})`);
Expand All @@ -115,6 +116,14 @@ class TwitchBot {
this._client.on('disconnected', (reason) => {
Logger.warn(`DISCONNECTED from chat, trying to reconnect... (reason: ${reason})`);
});

this._client.on('reconnect', () => {
Logger.info('RECONNECTING...');
});

this._client.on('connected', () => {
Logger.info('CONNECTED: Waiting for events...');
});
}

/**
Expand All @@ -136,7 +145,7 @@ class TwitchBot {
* @return {[none]} [description]
*/
SubAlert() {
this._client.on('subscription', (channel, username, method, message, userstate) => {
this._client.on('subscription', (channel, username, method, message) => {
if (Config.enableCustomMessages) {
const subAlertMessages = Config.customMessages[channel].subscriptions;
if (subAlertMessages !== undefined) {
Expand Down Expand Up @@ -192,7 +201,7 @@ class TwitchBot {
* @return {[none]} [description]
*/
GiftSubAlert() {
this._client.on('subgift', (channel, username, recipient, method, userstate) => {
this._client.on('subgift', (channel, username, recipient) => {
if (Config.enableCustomMessages) {
const giftSubAlertMessages = Config.customMessages[channel].giftsubscriptions;
if (giftSubAlertMessages !== undefined) {
Expand Down Expand Up @@ -249,7 +258,7 @@ class TwitchBot {
* @return {[none]} [description]
*/
ResubAlert() {
this._client.on('resub', (channel, username, months, message, userstate, methods) => {
this._client.on('resub', (channel, username, months, message) => {
if (Config.enableCustomMessages) {
const resubAlertMessages = Config.customMessages[channel].resubscriptions;
if (resubAlertMessages !== undefined) {
Expand All @@ -262,6 +271,7 @@ class TwitchBot {
const yearMonths = months % 12;
let finalAlert = alert.replace(/{{username}}/gi, username);
finalAlert = finalAlert.replace(/{{months}}/gi, months);
finalAlert = finalAlert.replace(/{{message}}/gi, message);
if (years === 0) {
finalAlert = finalAlert.replace(/{{years}}/gi, '');
} else {
Expand Down
1 change: 1 addition & 0 deletions src/twitch-js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'twitch-js'

0 comments on commit feca20c

Please sign in to comment.