Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement execution of chatai-command #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Slack = require('slack-client');
var CronJob = require('cron').CronJob;
var google = require('googleapis');
var fs = require('fs');
var spawn = require('child_process').spawn;

var secret = require('./secret.json');

Expand Down Expand Up @@ -62,12 +63,13 @@ var yoruho = new CronJob('00 00 00 * * *', function () {
});
}, null, true, 'Asia/Tokyo');

// プロ->趣味
slack.on('message', function (message) {
var channel = slack.getChannelGroupOrDMByID(message.channel);
var user = slack.getUserByID(message.user);

if (channel.name === 'random' && message.type === 'message' && message.text) {
// プロ->趣味

var puro = /([ぷプ][ろロ]|pro)/ig;

// ignore frequently appearing words
Expand All @@ -94,5 +96,55 @@ slack.on('message', function (message) {

channel.send(response);
}

// ちゃたいコマンド

var execChatai = false;

// Execute on reply
if (message.text.length < 200) {
if (message.text.match('<@' + slack.self.id + '>')) {
execChatai = 'force';
} else if (Math.random() < .1) {
execChatai = 'possible';
}
}

if (execChatai) {
// Remove meta sequences and emojies
var input = message.text.replace(/(<.+?>|:.+?:)/g, '');

var command = spawn('shly', [
'run',
input,
'--debug', 't'
], {
cwd: '/home/hakatashi/chatai-command'
});

var reply = '';
command.stdout.on('data', function (chunk) {
reply += chunk;
});
command.on('close', function (exitCode) {
if (exitCode === 0) {
// Strip heading and trailing whitespaces
reply = reply.replace(/(^\s+|\s+$)/g, '');

// If reply is empty or NIL
if (reply === '' || reply === 'NIL') {
// Reply emoji if force reply
if (execChatai === 'force') {
var emojies = ['sleeping', 'confused', 'sleepy', 'persevere', 'shit', 'open_hands'];
var emoji = emojies[Math.floor(Math.random() * emojies.length)];
channel.send('@' + user.name + ' :' + emoji + ':');
}
} else {
// If reply is not empty
channel.send('@' + user.name + ' ' + reply.slice(0, 200));
}
}
});
}
}
});