Skip to content

Commit

Permalink
fix: Refactor PRIVMSG handler move to own function
Browse files Browse the repository at this point in the history
  • Loading branch information
KararTY committed Aug 11, 2020
1 parent a4dea39 commit 44f3261
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/Twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class Message {
constructor (msg: PrivmsgMessage, clientRef: any) {
this.msg = msg

// All messages are delayed by 1100ms for time-out checking.
this.timer = setTimeout(() => {
clientRef.onMessage(this)
}, 1000)
}, 1100)
}
}

Expand Down Expand Up @@ -125,7 +126,6 @@ export default class Client {
}

// Filter bad words.
// TODO: Add a global filter.
if (msg.flags instanceof Array) {
for (let index = 0; index < msg.flags.length; index++) {
const word = msg.flags[index].word
Expand Down Expand Up @@ -228,26 +228,7 @@ export default class Client {
})
this.ircClient.on('error', (error) => this.onError(error))

// All messages are delayed by 1000ms for time-out checking.
this.ircClient.on('PRIVMSG', (msg) => {
if (!msg.messageText.startsWith(this.commandPrefix)) {
return
}

const foundChannel = this.channels.get(msg.channelID)

if (foundChannel === undefined) {
this.leaveChannel({ id: msg.channelID, name: msg.channelName })
return
} else if (foundChannel.cooldown.getTime() > Date.now()) {
return
}

foundChannel.cooldown = new Date(Date.now() + 5000)

// Message class has a "clientRef" to "this" so it can call clientRef.onMessage().
this.msgs.set(msg.messageID, new Message(msg, this))
})
this.ircClient.on('PRIVMSG', (msg) => this.prepareMsg(msg))

this.ircClient.on('CLEARCHAT', (msg) => this.deleteMessage(msg))
this.ircClient.on('CLEARMSG', (msg) => this.deleteMessage(msg))
Expand All @@ -256,6 +237,27 @@ export default class Client {
return this.ircClient.connect()
}

private prepareMsg (msg: PrivmsgMessage): void {

if (!msg.messageText.startsWith(this.commandPrefix)) {
return
}

const foundChannel = this.channels.get(msg.channelID)

if (foundChannel === undefined) {
this.leaveChannel({ id: msg.channelID, name: msg.channelName })
return
} else if (foundChannel.cooldown.getTime() > Date.now()) {
return
}

foundChannel.cooldown = new Date(Date.now() + 5000)

// Message class has a "clientRef" to "this" so it can call clientRef.onMessage().
this.msgs.set(msg.messageID, new Message(msg, this))
}

public async checkReady (): Promise<any> {
if (!this.ready) {
await new Promise(resolve => setTimeout(resolve, 500))
Expand Down

0 comments on commit 44f3261

Please sign in to comment.