Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
397 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/env node | ||
const Bot = require('../../index.js') | ||
const Hand = require('pokersolver').Hand | ||
|
||
const bot = new Bot() | ||
|
||
const deck = | ||
'2s,3s,4s,5s,6s,7s,8s,9s,Ts,Js,Qs,Ks,As,2c,3c,4c,5c,6c,7c,8c,9c,Tc,Jc,Qc,Kc,Ac,2d,3d,4d,5d,6d,7d,8d,9d,Td,Jd,Qd,Kd,Ad,2h,3h,4h,5h,6h,7h,8h,9h,Th,Jh,Qh,Kh,Ah' | ||
const deckI2S = deck.split(',').reduce((prev, curr, ind) => { | ||
prev[ind] = curr | ||
return prev | ||
}, {}) | ||
|
||
async function main() { | ||
try { | ||
const username = process.env.KB_USERNAME | ||
const paperkey = process.env.KB_PAPERKEY | ||
await bot.init(username, paperkey) | ||
const info = bot.myInfo() | ||
console.log(`Bot initialized with username ${info.username}.`) | ||
|
||
console.log(`Listening for all messages...`) | ||
await bot.chat.watchAllChannelsForNewMessages( | ||
async msg => { | ||
try { | ||
console.log(msg) | ||
|
||
if (msg.channel.membersType !== 'impteamnative') { | ||
// Only react to direct messages. | ||
console.log(`Invalid type - ${msg.channel.membersType}`) | ||
return | ||
} | ||
|
||
if (msg.content.type === 'flip') { | ||
let resolved | ||
while (!resolved) { | ||
try { | ||
const flip = await bot.chat.loadFlip( | ||
msg.conversationId, | ||
msg.content.flip.flipConvId, | ||
msg.id, | ||
msg.content.flip.gameId | ||
) | ||
if (flip.phase === 2) { | ||
resolved = true | ||
|
||
if (flip.resultInfo.typ !== 3 || !flip.resultInfo.hands.find(x => x.target === 'me')) { | ||
await bot.chat.send(msg.channel, { | ||
body: `Invalid flip type! Please do a /flip cards 5 me instead.`, | ||
}) | ||
return | ||
} | ||
|
||
const myHand = flip.resultInfo.hands.find(x => x.target === 'me') | ||
const hand = Hand.solve(myHand.hand.map(card => deckI2S[card])) | ||
await bot.chat.send(msg.channel, { | ||
body: `You got ${hand.descr}.`, | ||
}) | ||
} | ||
} catch (err) { | ||
console.log('flip fetch error', err) | ||
} | ||
} | ||
return | ||
} | ||
|
||
await bot.chat.send(msg.channel, { | ||
body: `Hi ${msg.sender.username}! Do a /flip cards 5 me and I'll tell what's in your poker hand!`, | ||
}) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
}, | ||
e => console.error(e) | ||
) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
async function shutDown() { | ||
await bot.deinit() | ||
process.exit() | ||
} | ||
|
||
process.on('SIGINT', shutDown) | ||
process.on('SIGTERM', shutDown) | ||
|
||
main() |
Oops, something went wrong.