forked from Chipsnet/damare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (143 loc) · 5.34 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const bunyan = require('bunyan');
const { exec } = require('child_process');
const Encoding = require('encoding-japanese');
const packageJson = require('./package.json');
const fs = require('fs');
const { exit } = require('process');
const Discord = require('discord.js');
const chokidar = require('chokidar');
const yaml = require("js-yaml");
const log = bunyan.createLogger({name: 'damare', level: 'debug'});
log.info("Damare reading bot v" + packageJson.version);
log.info('Checking softalk...');
if (fs.existsSync('./softalk/SofTalk.exe')) {
log.info('Softalk found.');
} else {
log.error('Softalk not found. Can\'t Start damare. Please put softalk to current dir. If you want more info, visit https://github.com/Chipsnet/damare.');
exit()
}
try {
config = yaml.load(
fs.readFileSync("./config.yml", "utf-8")
);
} catch (error) {
log.fatal('Config file not found. Please make config file. More information: https://github.com/Chipsnet/warbot-js.')
log.error(error);
process.exit(0)
}
const toString = (bytes) => {
return Encoding.convert(bytes, {
from: 'SJIS',
to: 'UNICODE',
type: 'string',
});
};
const client = new Discord.Client();
const broadcast = client.voice.createBroadcast();
let connection = null;
let readMessages = [];
let canReadMessage = true;
let readChannel = null;
let prefix = config.prefix;
client.on('ready', () => {
log.info('Discord login success! Logged in as : ' + client.user.tag);
});
client.on('message', async message => {
if (!message.guild) return;
if (message.guild.id != config.useguild) return;
if (message.content === `${prefix}talk`) {
if (message.member.voice.channel) {
readChannel = message.channel.id
connection = await message.member.voice.channel.join();
connection.play(broadcast, {volume: 0.3});
message.reply('✨ VCに接続しました!');
}
}
if (message.content === `${prefix}stop`) {
if (connection === null) {
message.reply('⚠ ボイスチャンネルに接続されていないので、切断ができませんでした。');
} else {
connection.disconnect();
message.reply('👍 無事切断できました')
connection = null;
readChannel = null;
}
}
if (message.content === `${prefix}reset`) {
readMessages = [];
canReadMessage = true;
message.reply('💥 読み上げ状態をリセットしました');
}
if (message.content === `${prefix}help`) {
message.reply('```\n'+
'Damare 読み上げBot コマンドリスト\n' +
'Author:巳波みなと Version:v' + packageJson.version + '\n' +
'https://github.com/Chipsnet/damare\n\n' +
`${prefix}talk : 現在のテキストチャンネルを現在入っているVCで読み上げます。\n` +
`${prefix}stop : 再生を停止してVCから切断します。\n` +
`${prefix}reset : 読み上げ状態や内部のキューをリセットします。問題が発生した場合にのみ使用してください。\n` +
`${prefix}help : ヘルプを表示します。\n` +
'```'
);
}
if (message.channel.id === readChannel && message.content != ']talk' && message.author.bot == false && message.content.startsWith(prefix) == false) {
if (message.content.startsWith('http')) {
message.content = "ユーアールエル"
}
if (canReadMessage) {
log.debug(`Message recived. canReadMessage: ${canReadMessage}`)
readMessages.push(message.content);
softalk();
} else {
log.debug(`Message recived. canReadMessage: ${canReadMessage}`)
readMessages.push(message.content);
}
}
});
async function softalk() {
canReadMessage = false;
let mes = readMessages.shift();
mes = mes.split('|').join('')
mes = mes.split(';').join('')
mes = mes.split('&').join('')
mes = mes.split('-').join('')
mes = mes.split('\\').join('')
mes = mes.split('/').join('')
mes = mes.split(':').join('')
mes = mes.split('<').join('')
mes = mes.split('>').join('')
mes = mes.split('$').join('')
mes = mes.split('*').join('')
mes = mes.split('?').join('')
mes = mes.split('{').join('')
mes = mes.split('}').join('')
mes = mes.split('[').join('')
mes = mes.split(']').join('')
mes = mes.split('!').join('')
mes = mes.split('`').join('')
log.debug('softalk talk message: ' + mes);
log.debug('in queue' + readMessages);
exec('"./softalk/SofTalk.exe" /NM:女性01 /R:' + __dirname + '\\voice.wav /T:0 /X:1 /V:100 /W:' + mes, { encoding: 'Shift_JIS' }, (error, stdout, stderr) => {
if (error) {
log.error(toString(stderr));
if (readMessages.length === 0) {
canReadMessage = true;
} else {
softalk();
}
return;
}
})
}
chokidar.watch("./voice.wav").on('change', () => {
let dispatcher = broadcast.play('./voice.wav');
dispatcher.on('finish', () => {
if (readMessages.length === 0) {
canReadMessage = true;
} else {
softalk();
}
})
})
client.login(config.token);
log.info('Trying Login to discord...');