-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (42 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict'
const logger = require('winston')
const mtg = require('mtgsdk')
// creates a bot server with a single bot
const botFramework = require('watsonworkspace-bot')
botFramework.level('verbose')
botFramework.startServer()
const bot = botFramework.create() // bot settings defined by process.env
const UI = require('watsonworkspace-sdk').UI
// slash commands or action fulfillment
bot.on(`actionSelected`, (message, annotation, params) => {
// get the original message that created this actionSelected annotation
const referralMessageId = annotation.referralMessageId
const userId = message.userId
const actionId = annotation.actionId
logger.info(`${actionId} selected from message ${referralMessageId} by user ${userId}`)
logger.debug(message)
logger.debug(annotation)
})
bot.authenticate()
// handle the message-created webhook
bot.on('message-created', (message, annotation) => {
if (message.content.match(/\[.*?\]/g)) {
let cardNames = message.content.match(/\[.*?\]/g).map(str => str.substring(1, str.length-1));
console.log(`yo ${cardNames}`);
cardNames.forEach(cardName => {
mtg.card.where({ name: cardName })
.then(cards => {
console.log(cards[0]);
const card = cards[0];
let result = (`${card.name}\n${card.manaCost}\n${card.type}\n${card.text}\n`);
if(card.power && card.toughness) {
result = result.concat(`${card.power}/${card.toughness}`);
} else if (card.loyalty) {
result = result.concat(card.loyalty);
}
bot.sendMessage(message.spaceId, result)
}
).catch(err => console.log(err));
})
}
})