Skip to content

Commit

Permalink
Fix issue where any redemption would cause a swap
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjpaz committed Jan 21, 2021
1 parent 93534a5 commit 55f7fb9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ const config = require('./config');

const ComfyJS = require("comfy.js");

const chatCommandRegExp = new RegExp(config.chatCommand);

const chatListCommandRegExp = new RegExp(config.chatListCommand);

const rewardNameRegExp = new RegExp(config.rewardNameRegExp);

class TwitchShufflerListener {
constructor(props = {}) {
this.swap = props.swap;

this.list = props.list;

this.lastCommandTimestamps = {};

this.chatCommand = props.chatCommand || config.chatCommand;
this.redemptionName = props.redemptionName || config.redemptionName;
this.chatListCommand = props.chatListCommand || config.chatListCommand;
}

async say(text) {
Expand Down Expand Up @@ -50,24 +48,36 @@ class TwitchShufflerListener {
}

async onCommand( user, command, message, flags, extra ) {
if(chatCommandRegExp.test(command)) {
const isCoolingDown = this.isCoolingDown(user, command, message, flags, extra);
if(this.chatCommand) {
const chatCommandRegExp = new RegExp(this.chatCommand);

if(chatCommandRegExp.test(command)) {
const isCoolingDown = this.isCoolingDown(user, command, message, flags, extra);

if(!isCoolingDown) {
this.lastCommandTimestamps['$$global$$'] = new Date().getTime();
this.lastCommandTimestamps[user] = new Date().getTime();
this.swap(message, `${user} via command`);
if(!isCoolingDown) {
this.lastCommandTimestamps['$$global$$'] = new Date().getTime();
this.lastCommandTimestamps[user] = new Date().getTime();
this.swap(message, `${user} via command`);
}
}
}

if(chatListCommandRegExp.test(command)) {
this.say(await this.list(message));
if(this.chatListCommand) {
const chatListCommandRegExp = new RegExp(this.chatListCommand);

if(chatListCommandRegExp.test(command)) {
this.say(await this.list(message));
}
}
}

async onReward( user, reward, cost, extra ) {
if(rewardNameRegExp.test(reward)) {
this.swap(extra, `${user} via reward`);
if(this.redemptionName) {
const matchesRemption = new RegExp(this.redemptionName).test(reward);

if(matchesRemption) {
this.swap(extra, `${user} via reward`);
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/twitch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ describe('twitch', () => {
expect(listener.swap.called).to.eql(true);
expect(listener.swap.calledWith("fake_message")).to.eql(true);
});

it('should NOT swap on invalid swap redemption', () => {
listener.swap = sinon.spy();

listener.onReward(
"fake_user",
"__INVALID__", // TODO
-1,
"fake_message"
);

expect(listener.swap.called).to.eql(false);
expect(listener.swap.calledWith("fake_message")).to.eql(false);
});
});

describe('cooldown', () => {
Expand Down

0 comments on commit 55f7fb9

Please sign in to comment.