-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update ESLint rules, clean up imports, and remove incompatible…
… Twitch dependencies
- Loading branch information
Showing
11 changed files
with
150 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,112 @@ | ||
import {Link, Store} from '../store/model'; | ||
import {Print, logger} from '../logger'; | ||
import {RefreshableAuthProvider, StaticAuthProvider} from 'twitch-auth'; | ||
import {existsSync, promises, readFileSync} from 'fs'; | ||
import {ChatClient} from 'twitch-chat-client'; | ||
import {config} from '../config'; | ||
/** | ||
* TODO: Needs to be upgraded to @twurple/auth + @twurple/chat - currently throws the following error if no client params are in | ||
* dotenv and an item is found | ||
* | ||
* var _this = _super.call(this) || this; | ||
TypeError: Class constructor EventEmitter cannot be invoked without 'new' | ||
at ChatClient.IrcClient [as constructor] (/streetmerchant/node_modules/.pnpm/[email protected]/node_modules/ircv3/lib/IrcClient.js:24:28) | ||
at new ChatClient (/streetmerchant/node_modules/.pnpm/[email protected][email protected]/node_modules/twitch-chat-client/lib/ChatClient.js:44:28) | ||
at Object.<anonymous> (/streetmerchant/build/src/messaging/twitch.js:23:20) | ||
*/ | ||
|
||
const {twitch} = config.notifications; | ||
// import {Link, Store} from '../store/model'; | ||
// import {Print, logger} from '../logger'; | ||
// import {RefreshableAuthProvider, StaticAuthProvider} from 'twitch-auth'; | ||
// import {existsSync, promises, readFileSync} from 'fs'; | ||
// import {ChatClient} from 'twitch-chat-client'; | ||
// import {config} from '../config'; | ||
|
||
const messages: string[] = []; | ||
let alreadySaying = false; | ||
// const {twitch} = config.notifications; | ||
|
||
let tokenData = { | ||
accessToken: twitch.accessToken, | ||
expiryTimestamp: 0, | ||
refreshToken: twitch.refreshToken, | ||
}; | ||
// const messages: string[] = []; | ||
// let alreadySaying = false; | ||
|
||
if (existsSync('./twitch.json')) { | ||
tokenData = { | ||
...JSON.parse(readFileSync('./twitch.json', 'utf-8')), | ||
...tokenData, | ||
}; | ||
} | ||
// let tokenData = { | ||
// accessToken: twitch.accessToken, | ||
// expiryTimestamp: 0, | ||
// refreshToken: twitch.refreshToken, | ||
// }; | ||
|
||
const chatClient: ChatClient = new ChatClient( | ||
new RefreshableAuthProvider( | ||
new StaticAuthProvider(twitch.clientId, tokenData.accessToken), | ||
{ | ||
clientSecret: twitch.clientSecret, | ||
expiry: | ||
tokenData.expiryTimestamp === null | ||
? null | ||
: new Date(tokenData.expiryTimestamp), | ||
onRefresh: async ({accessToken, refreshToken, expiryDate}) => { | ||
return promises.writeFile( | ||
'./twitch.json', | ||
JSON.stringify( | ||
{ | ||
accessToken, | ||
expiryTimestamp: | ||
expiryDate === null ? null : expiryDate.getTime(), | ||
refreshToken, | ||
}, | ||
null, | ||
4 | ||
), | ||
'utf-8' | ||
); | ||
}, | ||
refreshToken: tokenData.refreshToken, | ||
} | ||
), | ||
{ | ||
channels: [twitch.channel], | ||
} | ||
); | ||
// if (existsSync('./twitch.json')) { | ||
// tokenData = { | ||
// ...JSON.parse(readFileSync('./twitch.json', 'utf-8')), | ||
// ...tokenData, | ||
// }; | ||
// } | ||
|
||
chatClient.onJoin((channel: string, user: string) => { | ||
if (channel === `#${twitch.channel}` && user === chatClient.currentNick) { | ||
while (messages.length) { | ||
const message: string | undefined = messages.shift(); | ||
// const chatClient: ChatClient = new ChatClient( | ||
// new RefreshableAuthProvider( | ||
// new StaticAuthProvider(twitch.clientId, tokenData.accessToken), | ||
// { | ||
// clientSecret: twitch.clientSecret, | ||
// expiry: | ||
// tokenData.expiryTimestamp === null | ||
// ? null | ||
// : new Date(tokenData.expiryTimestamp), | ||
// onRefresh: async ({accessToken, refreshToken, expiryDate}) => { | ||
// return promises.writeFile( | ||
// './twitch.json', | ||
// JSON.stringify( | ||
// { | ||
// accessToken, | ||
// expiryTimestamp: | ||
// expiryDate === null ? null : expiryDate.getTime(), | ||
// refreshToken, | ||
// }, | ||
// null, | ||
// 4 | ||
// ), | ||
// 'utf-8' | ||
// ); | ||
// }, | ||
// refreshToken: tokenData.refreshToken, | ||
// } | ||
// ), | ||
// { | ||
// channels: [twitch.channel], | ||
// } | ||
// ); | ||
|
||
if (message !== undefined) { | ||
try { | ||
void chatClient.say(channel, message); | ||
logger.info('✔ twitch message sent'); | ||
} catch (error: unknown) { | ||
logger.error("✖ couldn't send twitch message", error); | ||
} | ||
} | ||
} | ||
} | ||
// chatClient.onJoin((channel: string, user: string) => { | ||
// if (channel === `#${twitch.channel}` && user === chatClient.currentNick) { | ||
// while (messages.length) { | ||
// const message: string | undefined = messages.shift(); | ||
|
||
void chatClient.quit(); | ||
}); | ||
// if (message !== undefined) { | ||
// try { | ||
// void chatClient.say(channel, message); | ||
// logger.info('✔ twitch message sent'); | ||
// } catch (error: unknown) { | ||
// logger.error("✖ couldn't send twitch message", error); | ||
// } | ||
// } | ||
// } | ||
// } | ||
|
||
chatClient.onDisconnect(() => { | ||
alreadySaying = false; | ||
}); | ||
// void chatClient.quit(); | ||
// }); | ||
|
||
export function sendTwitchMessage(link: Link, store: Store) { | ||
if ( | ||
tokenData.accessToken && | ||
twitch.channel && | ||
twitch.clientId && | ||
twitch.clientSecret && | ||
tokenData.refreshToken | ||
) { | ||
logger.debug('↗ sending twitch message'); | ||
// chatClient.onDisconnect(() => { | ||
// alreadySaying = false; | ||
// }); | ||
|
||
messages.push( | ||
`${Print.inStock(link, store)}\n${link.cartUrl ? link.cartUrl : link.url}` | ||
); | ||
// export function sendTwitchMessage(link: Link, store: Store) { | ||
// if ( | ||
// tokenData.accessToken && | ||
// twitch.channel && | ||
// twitch.clientId && | ||
// twitch.clientSecret && | ||
// tokenData.refreshToken | ||
// ) { | ||
// logger.debug('↗ sending twitch message'); | ||
|
||
if (!alreadySaying) { | ||
alreadySaying = true; | ||
void chatClient.connect(); | ||
} | ||
} | ||
} | ||
// messages.push( | ||
// `${Print.inStock(link, store)}\n${link.cartUrl ? link.cartUrl : link.url}` | ||
// ); | ||
|
||
// if (!alreadySaying) { | ||
// alreadySaying = true; | ||
// void chatClient.connect(); | ||
// } | ||
// } | ||
// } |
Oops, something went wrong.