Skip to content

Instantly share code, notes, and snippets.

@Ebycow
Last active October 8, 2018 04:41
Show Gist options
  • Save Ebycow/cd48ff7ecd6600a1471a7cc77b8b5dac to your computer and use it in GitHub Desktop.
Save Ebycow/cd48ff7ecd6600a1471a7cc77b8b5dac to your computer and use it in GitHub Desktop.
Mixer棒読みちゃん.js

npm install @mixer/client-node ws

Tokenって?????????

GET

https://mixer.com/oauth/authorize
?response_type=token
&redirect_uri=http://example.com
&scope=  chat:connect  %20chat:chat  %20chat:bypass_slowchat
&client_id= {クライアントID}
const net = require('net');
class Bouyomi {
constructor(host='localhost', tcpPort=50001) {
this.hostname = host;
this.port = tcpPort;
this.client = new net.Socket();
this.client.on('error', (e) => {
if(e.errno === 'ECONNREFUSED'){
console.error("エラー 棒読みちゃんを起動して、ホストとポートが間違ってないか確認して");
}
console.log(e);
});
}
yomiage(word="", command=1, speed=-1, tone=-1, volume=-1, voice=1) {
const iCommand = new Buffer(2);
const iSpeed = new Buffer(2);
const iTone = new Buffer(2);
const iVolume = new Buffer(2);
const iVoice = new Buffer(2);
const bCode = new Buffer(1);
const bMessage = new Buffer(word, 'utf8'); //文字列のbyte配列
const iLength = new Buffer(4);
iCommand.writeInt16LE(command, 0); //コマンド( 0:メッセージ読み上げ)
iSpeed.writeInt16LE(speed, 0); //速度 (-1:棒読みちゃん画面上の設定)
iTone.writeInt16LE(tone, 0); //音程 (-1:棒読みちゃん画面上の設定)
iVolume.writeInt16LE(volume, 0); //音量 (-1:棒読みちゃん画面上の設定)
iVoice.writeInt16LE(voice, 0); //声質 ( 0:棒読みちゃん画面上の設定、1:女性1、2:女性2、3:男性1、4:男性2、5:中性、6:ロボット、7:機械1、8:機械2、10001~:SAPI5)
bCode.writeInt8(0, 0); //文字列のbyte配列の文字コード(0:UTF-8, 1:Unicode, 2:Shift-JIS)
iLength.writeInt32LE(bMessage.length, 0); //文字列のbyte配列の長さ
this.client.connect(this.port, this.hostname, () => {
this.client.write(iCommand);
this.client.write(iSpeed);
this.client.write(iTone);
this.client.write(iVolume);
this.client.write(iVoice);
this.client.write(bCode);
this.client.write(iLength);
this.client.write(bMessage);
this.client.end();
});
}
}
module.exports = {
Bouyomi,
}
const Mixer = require('@mixer/client-node');
const ws = require('ws');
const { Bouyomi } = require('./bouyomi');
const bouyomi = new Bouyomi();
let userInfo;
const client = new Mixer.Client(new Mixer.DefaultRequestRunner());
client.use(new Mixer.OAuthProvider(client, {
tokens: {
access: 'tokenを入力',
expires: Date.now() + (365 * 24 * 60 * 60 * 1000)
},
}));
client.request('GET', 'users/current')
.then(response => {
//console.log(response.body);
userInfo = response.body;
return new Mixer.ChatService(client).join(response.body.channel.id);
}).then(response => {
const body = response.body;
//console.log(body);
console.log(body.permissions);
return createChatSocket(userInfo.id, userInfo.channel.id, body.endpoints, body.authkey);
}).catch(error => {
console.error('Something went wrong.');
console.error(error);
});
function createChatSocket (userId, channelId, endpoints, authkey) {
const socket = new Mixer.Socket(ws, endpoints).boot();
socket.auth(channelId, userId, authkey)
.then(() => {
console.log('認証に成功しました');
console.log(`チャンネルID:\t${ channelId } (${ userInfo.channel.name }) \nユーザID:\t${ userId } (${ userInfo.username })`);
}).catch(error => {
console.error('エラー');
console.error(error);
});
socket.on('ClearMessages', () => {
console.log("メッセージが削除されました");
});
socket.on('ChatMessage', data => {
//console.log(data)
console.log(`[${ data.user_name }](${ data.user_level }) :${ data.message.message[0].text }`);
bouyomi.yomiage(data.message.message[0].text);
});
socket.on('error', error => {
console.error('Socket error');
console.error(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment