xiongzhu 2 ani în urmă
părinte
comite
4ef78c53fb
7 a modificat fișierele cu 134 adăugiri și 1 ștergeri
  1. 1 0
      package.json
  2. 3 1
      src/app.module.ts
  3. 19 0
      src/twitch/twitch.controller.ts
  4. 9 0
      src/twitch/twitch.module.ts
  5. 4 0
      src/twitch/twitch.service.ts
  6. 85 0
      twitch.mjs
  7. 13 0
      yarn.lock

+ 1 - 0
package.json

@@ -76,6 +76,7 @@
     "rimraf": "^4.1.2",
     "rimraf": "^4.1.2",
     "rxjs": "^7.8.0",
     "rxjs": "^7.8.0",
     "tiktoken": "^1.0.10",
     "tiktoken": "^1.0.10",
+    "tmi.js": "^1.8.5",
     "tnwx": "^2.5.6",
     "tnwx": "^2.5.6",
     "typeorm": "^0.3.12",
     "typeorm": "^0.3.12",
     "uuid": "^9.0.0",
     "uuid": "^9.0.0",

+ 3 - 1
src/app.module.ts

@@ -14,6 +14,7 @@ import { SysConfigModule } from './sys-config/sys-config.module'
 import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
 import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
 import { DownloadModule } from './download/download.module'
 import { DownloadModule } from './download/download.module'
 import { GameModule } from './game/game.module';
 import { GameModule } from './game/game.module';
+import { TwitchModule } from './twitch/twitch.module';
 @Module({
 @Module({
     imports: [
     imports: [
         DevtoolsModule.register({
         DevtoolsModule.register({
@@ -65,7 +66,8 @@ import { GameModule } from './game/game.module';
         FileModule,
         FileModule,
         SysConfigModule,
         SysConfigModule,
         DownloadModule,
         DownloadModule,
-        GameModule
+        GameModule,
+        TwitchModule
     ],
     ],
     controllers: [],
     controllers: [],
     providers: [
     providers: [

+ 19 - 0
src/twitch/twitch.controller.ts

@@ -0,0 +1,19 @@
+import { Controller, Get, Query, Req } from '@nestjs/common'
+import { Public } from '../auth/public.decorator'
+import axios from 'axios'
+@Controller('twitch')
+export class TwitchController {
+    @Get('/auth_callback')
+    @Public()
+    async authCallback(@Query() query) {
+        console.log(query)
+        const data = new FormData()
+        data.append('client_id', '530df6x09eq34be0vjhsezkl2oxap0')
+        data.append('client_secret', 'p7vcii89g50eiln4y8x4g1i0aih3q5')
+        data.append('grant_type', 'authorization_code')
+        data.append('redirect_uri', 'http://localhost:3000/api/twitch/auth_callback')
+        data.append('code', query.code)
+        const res = await axios.post('https://id.twitch.tv/oauth2/token',data)
+        return res.data
+    }
+}

+ 9 - 0
src/twitch/twitch.module.ts

@@ -0,0 +1,9 @@
+import { Module } from '@nestjs/common';
+import { TwitchService } from './twitch.service';
+import { TwitchController } from './twitch.controller';
+
+@Module({
+  providers: [TwitchService],
+  controllers: [TwitchController]
+})
+export class TwitchModule {}

+ 4 - 0
src/twitch/twitch.service.ts

@@ -0,0 +1,4 @@
+import { Injectable } from '@nestjs/common';
+
+@Injectable()
+export class TwitchService {}

+ 85 - 0
twitch.mjs

@@ -0,0 +1,85 @@
+import tmi from 'tmi.js'
+import axios from 'axios'
+
+/* let u = new URL('https://id.twitch.tv/oauth2/authorize?')
+u.searchParams.append('client_id', '530df6x09eq34be0vjhsezkl2oxap0')
+u.searchParams.append('redirect_uri', 'http://localhost:3000/api/twitch/auth_callback')
+u.searchParams.append('response_type', 'code')
+u.searchParams.append('scope', 'channel:manage:broadcast chat:read chat:edit channel:manage:polls')
+u.href */
+
+// Define configuration options
+const accessToken = 'o5s0s5b4aq9nak5m3a09ouv5smz21y'
+const clientId = '530df6x09eq34be0vjhsezkl2oxap0'
+const clientSecret = 'p7vcii89g50eiln4y8x4g1i0aih3q5'
+axios.defaults.headers.common['Authorization'] = 'Bearer ' + accessToken
+axios.defaults.headers.common['Client-Id'] = clientId
+
+const {
+    data: {
+        data: [user]
+    }
+} = await axios.get('https://api.twitch.tv/helix/users')
+console.log(user)
+
+const {
+    data: {
+        data: [broadcaster]
+    }
+} = await axios.get('https://api.twitch.tv/helix/channels?broadcaster_id=' + user.id)
+console.log(broadcaster)
+
+// Create a client with our options
+const client = new tmi.client({
+    identity: {
+        username: user.display_name,
+        password: 'oauth:' + accessToken
+    },
+    channels: [user.display_name]
+})
+
+// Register our event handlers (defined below)
+client.on('message', onMessageHandler)
+client.on('connected', onConnectedHandler)
+
+// Connect to Twitch:
+client.connect()
+
+// Called every time a message comes in
+function onMessageHandler(target, context, msg, self) {
+    if (self) {
+        return
+    } // Ignore messages from the bot
+
+    // Remove whitespace from chat message
+    const commandName = msg.trim()
+
+    // If the command is known, let's execute it
+    if (commandName === '!dice') {
+        const num = rollDice()
+        client.say(target, `You rolled a ${num}`)
+        console.log(`* Executed ${commandName} command`)
+    } else if (commandName === '!roll') {
+        axios.post('https://api.twitch.tv/helix/polls', {
+            broadcaster_id: broadcaster.broadcaster_id,
+            title: 'Streaming next Tuesday. Which time works best for you?',
+            choices: [{ title: '9AM' }, { title: '10AM' }, { title: '7PM' }, { title: '8PM' }, { title: '9PM' }],
+            duration: 300,
+            channel_points_voting_enabled: true,
+            channel_points_per_vote: 0
+        })
+    } else {
+        console.log(`* Unknown command ${commandName}`)
+    }
+}
+
+// Function called when the "dice" command is issued
+function rollDice() {
+    const sides = 6
+    return Math.floor(Math.random() * sides) + 1
+}
+
+// Called every time the bot connects to Twitch chat
+function onConnectedHandler(addr, port) {
+    console.log(`* Connected to ${addr}:${port}`)
+}

+ 13 - 0
yarn.lock

@@ -6861,6 +6861,14 @@ tiny-case@^1.0.3:
   resolved "https://registry.npmjs.org/tiny-case/-/tiny-case-1.0.3.tgz"
   resolved "https://registry.npmjs.org/tiny-case/-/tiny-case-1.0.3.tgz"
   integrity sha512-Eet/eeMhkO6TX8mnUteS9zgPbUMQa4I6Kkp5ORiBD5476/m+PIRiumP5tmh5ioJpH7k51Kehawy2UDfsnxxY8Q==
   integrity sha512-Eet/eeMhkO6TX8mnUteS9zgPbUMQa4I6Kkp5ORiBD5476/m+PIRiumP5tmh5ioJpH7k51Kehawy2UDfsnxxY8Q==
 
 
+tmi.js@^1.8.5:
+  version "1.8.5"
+  resolved "https://registry.npmmirror.com/tmi.js/-/tmi.js-1.8.5.tgz#8298821677599f163af075113f5d1bd51a648087"
+  integrity sha512-A9qrydfe1e0VWM9MViVhhxVgvLpnk7pFShVUWePsSTtoi+A1X+Zjdoa7OJd7/YsgHXGj3GkNEvnWop/1WwZuew==
+  dependencies:
+    node-fetch "^2.6.1"
+    ws "^8.2.0"
+
 tmp@^0.0.33:
 tmp@^0.0.33:
   version "0.0.33"
   version "0.0.33"
   resolved "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz"
   resolved "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz"
@@ -7444,6 +7452,11 @@ write-file-atomic@^4.0.2:
     imurmurhash "^0.1.4"
     imurmurhash "^0.1.4"
     signal-exit "^3.0.7"
     signal-exit "^3.0.7"
 
 
+ws@^8.2.0:
+  version "8.14.2"
+  resolved "https://registry.npmmirror.com/ws/-/ws-8.14.2.tgz#6c249a806eb2db7a20d26d51e7709eab7b2e6c7f"
+  integrity sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==
+
 xml2js@^0.4.16, xml2js@^0.4.22, xml2js@^0.4.23:
 xml2js@^0.4.16, xml2js@^0.4.22, xml2js@^0.4.23:
   version "0.4.23"
   version "0.4.23"
   resolved "https://registry.npmmirror.com/xml2js/-/xml2js-0.4.23.tgz#a0c69516752421eb2ac758ee4d4ccf58843eac66"
   resolved "https://registry.npmmirror.com/xml2js/-/xml2js-0.4.23.tgz#a0c69516752421eb2ac758ee4d4ccf58843eac66"