|
@@ -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}`)
|
|
|
|
|
+}
|