Skip to content

Commit

Permalink
Add online check and join message
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammster committed Feb 1, 2018
1 parent 55f65c1 commit 4c2f1e8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
50 changes: 42 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
import * as vscode from 'vscode'
import { TwitchChatProvider } from './twitchChat'
import * as twitchBot from 'twitch-bot'
import * as https from 'https'
import * as EventEmitter from 'events'

class TwitchChatEventEmitter extends EventEmitter {}
const twitchChatEventEmitter = new TwitchChatEventEmitter();

function getStreamData(channel) {
https.get({
'host': 'api.twitch.tv',
'path': '/kraken/streams/' + channel,
'port': 443,
'method': 'GET',
'headers': {
// This is limited to a certain amount of requests,
// if the plugin gets to popular we need a config to set a custom Client-ID
'Client-ID': 't4z7hvfvcrgmfrg9xtmed66ihiwk3d'
}
}, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', data => {
body += data;
})
res.on('end', () => {
twitchChatEventEmitter.emit('streamDataUpdate', JSON.parse(body));
return JSON.parse(body)
})
})
}

export function activate(context: vscode.ExtensionContext) {
const rootPath = vscode.workspace.rootPath
Expand All @@ -13,7 +42,7 @@ export function activate(context: vscode.ExtensionContext) {
const oauth = config.oauth
let joined = false
let activeChannel = channel

let streamData = getStreamData(activeChannel)

if (channel && username && oauth) {
const bot = new twitchBot({
Expand All @@ -31,25 +60,30 @@ export function activate(context: vscode.ExtensionContext) {
}
})

bot.on("error", err => {
bot.on('error', err => {
vscode.window.showErrorMessage(err)
})

twitchChatEventEmitter.on('streamDataUpdate', (newStreamData) => {
twitchChatProvider.connectToChannel(activeChannel, newStreamData)
})

vscode.commands.registerCommand('twitchChat.sendMessage', () => {
vscode.window.showInputBox({ prompt: "Enter a Chat message" }).then((message) => {
if(message) {
vscode.window.showInputBox({ prompt: 'Enter a Chat message' }).then((message) => {
if (message) {
bot.say(message)
twitchChatProvider.addItem({message:message, username:username})
twitchChatProvider.addItem({ message: message, username: username })
}
})
})

vscode.commands.registerCommand('twitchChat.changeChannel', () => {
vscode.window.showInputBox({ prompt: "Enter a channelname" }).then((newChannel) => {
if(newChannel) {
vscode.window.showInputBox({ prompt: 'Enter a channelname' }).then((newChannel) => {
if (newChannel) {
bot.part(channel)
bot.join(newChannel)
activeChannel = newChannel
streamData = getStreamData(newChannel)
}
})
})
Expand All @@ -62,4 +96,4 @@ export function activate(context: vscode.ExtensionContext) {
} else {
vscode.window.showWarningMessage('TwitchChat need additional configuration')
}
}
}
19 changes: 15 additions & 4 deletions src/twitchChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@ export class TwitchChatProvider implements vscode.TreeDataProvider<ChatEntry> {
readonly onDidChangeTreeData: vscode.Event<ChatEntry | undefined> = this._onDidChangeTreeData.event
public chatlog = [];

constructor(private workspaceRoot: string, private config: vscode.WorkspaceConfiguration) {}
constructor(private workspaceRoot: string, private config: vscode.WorkspaceConfiguration) { }

addItem(chatLogObject): void {
this.chatlog.push(new ChatEntry(this.formatMessage(chatLogObject), vscode.TreeItemCollapsibleState.None))
addItem(chatLogObjectOrString): void {
const message = typeof (chatLogObjectOrString) === 'object' ? this.formatMessage(chatLogObjectOrString) : chatLogObjectOrString
this.chatlog.push(new ChatEntry(message, vscode.TreeItemCollapsibleState.None))
this.refresh()
}

connectToChannel(channel, streamData): void {
const channelName = streamData.stream ? streamData.stream.channel.display_name : channel
this.chatlog.push(new ChatEntry('>>>>>>> JOINED: ' + channelName + ' [' + (streamData.stream ? 'online' : 'offline') + ']', vscode.TreeItemCollapsibleState.None))
this.refresh()
}

sliceChat(): void {
if (this.chatlog.length > this.config.historysize) {
const tempArray: Array<ChatEntry> = this.chatlog.slice().reverse()
this.chatlog = tempArray.slice(0, this.config.historysize).reverse();
}
this.refresh()
}

formatMessage(chatlogObject): string {
return `${chatlogObject.username}: ${chatlogObject.message}`
}

refresh(): void {
this.sliceChat()
this._onDidChangeTreeData.fire()
}

Expand Down

0 comments on commit 4c2f1e8

Please sign in to comment.