-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
689 lines (668 loc) · 33.6 KB
/
index.ts
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/* VoiceJP Main Script */
import "./includes/folder_manager";
import "./includes/web";
import {
ActionRowBuilder, ActivityType, BaseGuildTextChannel, ButtonBuilder, ButtonStyle, ChannelType,
ChatInputCommandInteraction, Client, Collection, Colors, EmbedBuilder, GuildMember,
IntentsBitField, Message, PermissionFlagsBits, REST, Routes, SlashCommandBuilder,
SlashCommandChannelOption, SlashCommandStringOption, SlashCommandSubcommandBuilder, VoiceState,
Webhook
} from "discord.js";
import dotenv from "dotenv";
import fs from "node:fs";
import path from "node:path";
import * as prism from "prism-media";
import * as vosk from "vosk";
import {
createAudioPlayer, createAudioResource, EndBehaviorType, joinVoiceChannel, StreamType,
VoiceConnectionStatus
} from "@discordjs/voice";
import nrCheck from "./includes/blocked_user_check";
import generateVoice from "./includes/speech";
import FillSilenceStream from "./models/fill_silence_stream";
dotenv.config();
const voiceModels = JSON.parse(fs.readFileSync(path.join(__dirname, "voice_models/models.json"), "utf-8"));
const voskModel = new vosk.Model(path.join(__dirname, "vosk_models", "vosk-model-ja-0.22"));
const token = process.env.TOKEN as string;
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.GuildVoiceStates,
IntentsBitField.Flags.MessageContent
]
});
const restClient = new REST({ version: "10" }).setToken(token);
const helpPages = [
() => {
return new EmbedBuilder()
.setTitle("使い方")
.setDescription("VoiceJPをお使いいただきありがとうございます。\nVoiceJPでは、日本語で音声読み上げと文字起こしができます。\n\n詳しい使用方法は、次のページをご覧ください。")
.setColor(Colors.LuminousVividPink)
.setFooter({
"text": "VoiceJP 使い方 1/5"
});
},
async () => {
return new EmbedBuilder()
.setTitle("使い方 - コマンド")
.setDescription("VoiceJPには、シンプルで簡単なコマンドが4つあります。\nこのページでは、VoiceJPをボイスチャットに入れる方法について説明します。")
.setColor(Colors.LuminousVividPink)
.setFields(
{
"name": `</join:${(await client.application?.commands.fetch())?.find((command) => command.name === "join")?.id}>`,
"value": "このコマンドを使用して、VoiceJPをボイスチャットに入れることができます。"
},
{
"name": `</leave:${(await client.application?.commands.fetch())?.find((command) => command.name === "leave")?.id}>`,
"value": "このコマンドを使用して、VoiceJPをボイスチャットから出すことができます。"
})
.setFooter({
"text": "VoiceJP 使い方 2/5"
});
},
async () => {
return new EmbedBuilder()
.setTitle("使い方 - 音声読み上げ")
.setDescription("VoiceJPでは、聞き専のための読み上げを行うことができます。\nこのページでは、音声読み上げについて説明します。")
.setColor(Colors.LuminousVividPink)
.setFields(
{
"name": `</speech synthesis:${(await client.application?.commands.fetch())?.find((command) => command.name === "speech")?.id}>`,
"value": "このコマンドを使用して、音声読み上げを設定することができます。\nもう一度実行すると、音声読み上げを解除します。"
},
{
"name": `</speech synthesis:${(await client.application?.commands.fetch())?.find((command) => command.name === "speech")?.id}> voice-id:`,
"value": "音声IDを設定します。"
},
{
"name": `</speech synthesis:${(await client.application?.commands.fetch())?.find((command) => command.name === "speech")?.id}> speed:`,
"value": "速度を設定します。\nデフォルトでは、1.25が設定されています。"
},
{
"name": `</speech synthesis:${(await client.application?.commands.fetch())?.find((command) => command.name === "speech")?.id}> tone:`,
"value": "音程を設定します。この設定では、声の高さが変わります。\nデフォルトでは、1が設定されています。"
},
{
"name": `</speech synthesis:${(await client.application?.commands.fetch())?.find((command) => command.name === "speech")?.id}> intonation:`,
"value": "抑揚を設定します。この設定では、声の抑揚が変わります。\nデフォルトでは、1が設定されています。"
},
{
"name": `</speech synthesis:${(await client.application?.commands.fetch())?.find((command) => command.name === "speech")?.id}> volume:`,
"value": "音量を設定します。\nデフォルトでは、0dBが設定されています。"
})
.setFooter({
"text": "VoiceJP 使い方 3/5"
});
},
async () => {
return new EmbedBuilder()
.setTitle("使い方 - 音声認識")
.setDescription("VoiceJPでは、音声認識を行うことができます。\nこのページでは、音声認識について説明します。")
.setColor(Colors.LuminousVividPink)
.setFields(
{
"name": `</speech recognition:${(await client.application?.commands.fetch())?.find((command) => command.name === "speech")?.id}>`,
"value": "このコマンドを使用して、音声認識を開始することができます。\nこのコマンドを実行すると、その実行したチャンネルで各メンバーのWebhookが作成されます。\nもう一度実行すると、音声認識を解除します。"
})
.setFooter({
"text": "VoiceJP 使い方 4/5"
});
},
() => {
return new EmbedBuilder()
.setTitle("使い方 - おわり")
.setDescription("VoiceJPの使い方は以上です。\n何かご不明な点がございましたら、[開発者Discordサーバー](https://discord.gg/VSHknmX7C4)または[ホームページ](https://renorari.net/contact.html)よりお問い合わせください。\n\n利用規約とプライバシポリシは以下のリンクより、ご覧ください。\n[利用規約 - VoiceJP公式ウェブサイト](https://voicejp.renorari.net/terms.html)\n[プライバシポリシ - renorari.net](https://renorari.net/privacy.html)")
.setColor(Colors.LuminousVividPink)
.setFooter({
"text": "VoiceJP 使い方 5/5"
});
}
];
const soundEffects = {
"join": () => {
return createAudioResource(path.join(__dirname, "sound_effects", "join.wav"), { inputType: StreamType.Arbitrary });
},
"enable": () => {
return createAudioResource(path.join(__dirname, "sound_effects", "enable.wav"), { inputType: StreamType.Arbitrary });
},
"disable": () => {
return createAudioResource(path.join(__dirname, "sound_effects", "disable.wav"), { inputType: StreamType.Arbitrary });
},
"error": () => {
return createAudioResource(path.join(__dirname, "sound_effects", "error.wav"), { inputType: StreamType.Arbitrary });
}
};
const voiceChannels = new Map();
function setActivity() {
client.user?.setActivity({
"name": `/help | ${client.guilds.cache.size}servers | joining ${voiceChannels.size}channels | ${client.ws.ping}ms`,
"type": ActivityType.Custom
});
}
client.on("ready", async () => {
console.log(`Logged in as ${client.user?.tag}`);
try {
await restClient.put(Routes.applicationCommands(client.application?.id as string), {
body: [
new SlashCommandBuilder()
.setName("ping")
.setDescription("Replies with Pong!")
.setDescriptionLocalization("ja", "Pong! と返信します。"),
new SlashCommandBuilder()
.setName("help")
.setDescription("Shows help.")
.setDescriptionLocalization("ja", "ヘルプを表示します。"),
new SlashCommandBuilder()
.setName("join")
.setDescription("Joins the voice channel.")
.setDescriptionLocalization("ja", "ボイスチャンネルに参加します。")
.addChannelOption(
new SlashCommandChannelOption()
.setName("channel")
.setDescription("The channel to join.")
.setDescriptionLocalization("ja", "参加するチャンネル。")
.addChannelTypes(ChannelType.GuildVoice)
.setRequired(true)
)
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Connect),
new SlashCommandBuilder()
.setName("leave")
.setDescription("Leaves the voice channel.")
.setDescriptionLocalization("ja", "ボイスチャンネルから退出します。")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Connect),
new SlashCommandBuilder()
.setName("speech")
.setDescription("set speech functions.")
.setDescriptionLocalization("ja", "音声機能を設定します。")
.addSubcommand(
new SlashCommandSubcommandBuilder()
.setName("synthesis")
.setDescription("set synthesis.")
.setDescriptionLocalization("ja", "音声合成を設定します。")
.addStringOption(
new SlashCommandStringOption()
.setName("voice-id")
.setDescription("set voice id.")
.setDescriptionLocalization("ja", "音声IDを設定します。")
.setChoices(...voiceModels.map((voiceModel: { id: string; name: string; }) => ({ value: voiceModel.id, name: voiceModel.name })))
)
.addStringOption(
new SlashCommandStringOption()
.setName("speed")
.setDescription("set speed.")
.setDescriptionLocalization("ja", "速度を設定します。")
)
.addStringOption(
new SlashCommandStringOption()
.setName("tone")
.setDescription("set tone.")
.setDescriptionLocalization("ja", "音程を設定します。")
)
.addStringOption(
new SlashCommandStringOption()
.setName("intonation")
.setDescription("set intonation.")
.setDescriptionLocalization("ja", "抑揚を設定します。")
)
.addStringOption(
new SlashCommandStringOption()
.setName("volume")
.setDescription("set volume.")
.setDescriptionLocalization("ja", "音量を設定します。")
)
)
.addSubcommand(
new SlashCommandSubcommandBuilder()
.setName("recognition")
.setDescription("set recognition.")
.setDescriptionLocalization("ja", "音声認識を設定します。")
)
]
});
} catch (error) {
console.error(error);
}
setActivity();
setInterval(setActivity, 1000 * 10);
});
async function disableVoice(guildId: string) {
if (!voiceChannels.has(guildId)) return;
clearInterval(voiceChannels.get(guildId).checker);
await disableSpeechSynthesis(guildId);
voiceChannels.get(guildId).connection.destroy();
voiceChannels.delete(guildId);
}
async function disableSpeechSynthesis(guildId: string) {
if (!voiceChannels.has(guildId) || !voiceChannels.get(guildId).synthesis) return;
voiceChannels.get(guildId).player.play(soundEffects.disable());
client.off("messageCreate", voiceChannels.get(guildId).synthesis.messageCreate);
voiceChannels.get(guildId).synthesis = null;
}
async function disableSpeechRecognition(guildId: string) {
if (!voiceChannels.has(guildId) || !voiceChannels.get(guildId).recognition) return;
await Promise.all(voiceChannels.get(guildId).recognition.recognizing.map(async (recognizingMember: { member: GuildMember; }) => {
await removeSpeechRecognizeMember(recognizingMember.member, guildId);
}));
client.off("voiceStateUpdate", voiceChannels.get(guildId).recognition.onVoiceStateUpdate);
voiceChannels.get(guildId).recognition = null;
}
const interactionCommands = new Map<string, (interaction: ChatInputCommandInteraction) => void>();
interactionCommands.set("ping", async (interaction: ChatInputCommandInteraction) => {
await interaction.reply({
content: "ポン!",
embeds: [{
title: "ポン!",
description: `レイテンシ: ${client.ws.ping}m秒`,
color: Colors.LuminousVividPink
}]
});
});
interactionCommands.set("help", async (interaction: ChatInputCommandInteraction) => {
await interaction.reply({
content: "VoiceJPの使い方を表示します。",
embeds: [await helpPages[0]()],
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId("help-0")
.setLabel("前へ")
.setStyle(ButtonStyle.Primary)
.setDisabled(true),
new ButtonBuilder()
.setCustomId("help-1")
.setLabel("次へ")
.setStyle(ButtonStyle.Primary)
)
]
});
});
interactionCommands.set("join", async (interaction: ChatInputCommandInteraction) => {
const channel = await interaction.guild?.channels.fetch(interaction.options.get("channel")?.value as string);
if (!channel) {
await interaction.reply({
"content": "エラーが発生しました。",
"embeds": [{
"title": "エラー",
"description": "チャンネルが見つかりません。",
"color": Colors.Red
}],
"ephemeral": true
});
return;
}
if (channel.type !== ChannelType.GuildVoice) {
await interaction.reply({
"content": "エラーが発生しました。",
"embeds": [{
"title": "エラー",
"description": "ボイスチャンネルを指定してください。",
"color": Colors.Red
}],
"ephemeral": true
});
return;
} else if (!channel.joinable) {
await interaction.reply({
"content": "エラーが発生しました。",
"embeds": [{
"title": "エラー",
"description": "参加できません。",
"color": Colors.Red
}],
"ephemeral": true
});
return;
}
await disableVoice(interaction.guildId as string);
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false
});
const player = createAudioPlayer();
connection.subscribe(player);
voiceChannels.set(interaction.guildId as string, { connection, player, channel });
connection.on(VoiceConnectionStatus.Ready, () => {
player.play(soundEffects.join());
const checker = setInterval(async () => {
if ((channel?.members as Collection<string, GuildMember>).size <= 1) {
clearInterval(checker);
await disableVoice(interaction.guildId as string);
interaction.channel?.send({
"content": "ボイスチャンネルから退出しました。",
"embeds": [{
"title": "退出",
"description": "ボイスチャンネルから退出しました。",
"color": Colors.Yellow
}]
});
}
}, 5000);
voiceChannels.get(interaction.guildId as string).checker = checker;
});
await interaction.reply({
"content": "ボイスチャンネルに参加しました。",
"embeds": [{
"title": "参加",
"description": `${channel.name}に参加しました。`,
"color": Colors.Green
}]
});
});
interactionCommands.set("leave", async (interaction: ChatInputCommandInteraction) => {
if (!voiceChannels.has(interaction.guildId as string)) {
await interaction.reply({
"content": "エラーが発生しました。",
"embeds": [{
"title": "エラー",
"description": "ボイスチャンネルに参加していません。",
"color": Colors.Red
}],
"ephemeral": true
});
return;
}
await disableVoice(interaction.guildId as string);
await interaction.reply({
"content": "ボイスチャンネルから退出しました。",
"embeds": [{
"title": "退出",
"description": "ボイスチャンネルから退出しました。",
"color": Colors.Green
}]
});
});
async function addSpeechRecognizeMember(member: GuildMember, guildId: string, channel: BaseGuildTextChannel) {
const webhook = await channel.createWebhook({
name: `${member.displayName}[VoiceJP]`,
avatar: member.user.displayAvatarURL({ "extension": "png", "size": 1024, "forceStatic": false }),
reason: "VoiceJP Voice Recognition"
});
const voice = voiceChannels.get(guildId).connection.receiver.subscribe(member.id, {
end: {
behavior: EndBehaviorType.Manual
},
}).pipe(new prism.opus.Decoder({ rate: 48000, channels: 1, frameSize: 960 }));
const recognizer = new vosk.Recognizer({
model: voskModel,
sampleRate: 48000,
});
const filledSilence = new FillSilenceStream();
voice.pipe(filledSilence);
filledSilence.on("data", async (data: Buffer) => {
if (recognizer.acceptWaveform(data)) {
const result = recognizer.result().text.replace(/ /g, "").replace(/。/g, "。\n").trim();
if (result === "") return;
webhook.send(result.slice(0, 1000) + (result.length > 1000 ? "…" : ""));
}
});
return {
webhook,
recognizer,
voice,
filledSilence
};
}
async function removeSpeechRecognizeMember(member: GuildMember, guildId: string) {
const recognizingMember = voiceChannels.get(guildId).recognition.recognizing.find((recognizing: { member: GuildMember }) => recognizing.member.id === member.id);
if (!recognizingMember) return;
recognizingMember.webhook.delete();
recognizingMember.filledSilence.destroy();
recognizingMember.recognizer.free();
recognizingMember.voice.destroy();
voiceChannels.get(guildId).recognition.recognizing.splice(voiceChannels.get(guildId).recognition.recognizing.indexOf(recognizingMember), 1);
}
interactionCommands.set("speech", async (interaction: ChatInputCommandInteraction) => {
if (!voiceChannels.has(interaction.guildId as string)) {
await interaction.reply({
"content": "エラーが発生しました。",
"embeds": [{
"title": "エラー",
"description": "ボイスチャンネルに参加していません。",
"color": Colors.Red
}],
"ephemeral": true
});
return;
}
const subCommand = interaction.options.getSubcommand();
if (subCommand === "synthesis") {
if (voiceChannels.get(interaction.guildId as string).synthesis) {
await disableSpeechSynthesis(interaction.guildId as string);
if (!interaction.options.get("voice-id")?.value && !interaction.options.get("speed")?.value && !interaction.options.get("tone")?.value && !interaction.options.get("intonation")?.value && !interaction.options.get("volume")?.value) {
await interaction.reply({
"content": "音声合成を解除しました。",
"embeds": [{
"title": "音声合成",
"description": "音声合成を解除しました。",
"color": Colors.Green
}]
});
return;
}
}
const voiceModel = voiceModels.find((voiceModel: { id: string; }) => voiceModel.id === (interaction.options.get("voice-id")?.value ?? voiceModels[0].id));
if (!voiceModel) {
await interaction.reply({
"content": "エラーが発生しました。",
"embeds": [{
"title": "エラー",
"description": "音声IDが見つかりません。",
"color": Colors.Red
}],
"ephemeral": true
});
return;
}
const onMessageCreate = async (message: Message) => {
if (nrCheck(message.author.id) || nrCheck(message.guildId as string)) return;
if (message.author.bot) return;
if (message.channel.id !== voiceChannels.get(interaction.guildId as string)?.synthesis?.channel) return;
if (message.content.length > 100 || message.content == "") return;
const filePath = path.join(__dirname, "temp", `${message.id}`);
const username = message.member?.displayName ? message.member.displayName : message.author.username;
const generatedVoice = await generateVoice(
// eslint-disable-next-line no-useless-escape
`${username.slice(0, 5)}。${message.cleanContent.replace(/https?:\/\/[\w/:%#\$&\?\(\)~\.=\+\-]+/g, "URL省略")}`,
filePath,
path.join(__dirname, "voice_models", voiceModel.file),
voiceChannels.get(interaction.guildId as string).synthesis.speed,
voiceChannels.get(interaction.guildId as string).synthesis.tone,
voiceChannels.get(interaction.guildId as string).synthesis.intonation,
voiceChannels.get(interaction.guildId as string).synthesis.volume,
0.25
).catch((error) => {
console.error(error);
});
if (!generatedVoice) return;
const resource = createAudioResource(generatedVoice, { inputType: StreamType.Arbitrary });
voiceChannels.get(interaction.guildId as string).player.play(resource);
};
voiceChannels.get(interaction.guildId as string).synthesis = {
"channel": interaction.channel?.id as string,
"voiceModel": voiceModel,
"speed": interaction.options.get("speed")?.value as number ?? 1.25,
"tone": interaction.options.get("tone")?.value as number ?? 1,
"intonation": interaction.options.get("intonation")?.value as number ?? 1,
"volume": interaction.options.get("volume")?.value as number ?? 0,
"messageCreate": onMessageCreate
};
client.on("messageCreate", onMessageCreate);
voiceChannels.get(interaction.guildId as string).player.play(soundEffects.enable());
await interaction.reply({
"content": "音声合成を設定しました。",
"embeds": [{
"title": "音声合成",
"description": `音声: ${voiceModel.name}\n速度: ${voiceChannels.get(interaction.guildId as string).synthesis.speed}\n音程: ${voiceChannels.get(interaction.guildId as string).synthesis.tone}\n抑揚: ${voiceChannels.get(interaction.guildId as string).synthesis.intonation}\n音量: ${voiceChannels.get(interaction.guildId as string).synthesis.volume}`,
"color": Colors.Green
}]
});
} else if (subCommand === "recognition") {
if (voiceChannels.get(interaction.guildId as string).recognition) {
await disableSpeechRecognition(interaction.guildId as string);
voiceChannels.get(interaction.guildId as string).player.play(soundEffects.disable());
await interaction.reply({
"content": "音声認識を解除しました。",
"embeds": [{
"title": "音声認識",
"description": "音声認識を解除しました。",
"color": Colors.Green
}]
});
return;
}
const recognitionMembers = new Map<string, GuildMember>();
voiceChannels.get(interaction.guildId as string).channel.members.forEach((member: GuildMember) => {
if (member.user.bot) return;
recognitionMembers.set(member.id, member);
});
const recognizing: { member: GuildMember; webhook: Webhook; recognizer: vosk.Recognizer<(vosk.Without<vosk.SpeakerRecognizerParam, Partial<vosk.GrammarRecognizerParam>> & Partial<vosk.GrammarRecognizerParam>) | (vosk.Without<Partial<vosk.GrammarRecognizerParam>, vosk.SpeakerRecognizerParam> & vosk.SpeakerRecognizerParam)>; voice: prism.opus.Decoder; filledSilence: FillSilenceStream; }[] = [];
recognitionMembers.forEach(async (member: GuildMember) => {
const { webhook, recognizer, voice, filledSilence } = await addSpeechRecognizeMember(member, interaction.guildId as string, interaction.channel as BaseGuildTextChannel);
recognizing.push({
member,
webhook,
recognizer,
voice,
filledSilence
});
});
const onVoiceStateUpdate = async (oldState: VoiceState, newState: VoiceState) => {
if (nrCheck(oldState.guild.id) || nrCheck(newState.guild.id)) return;
if (nrCheck(oldState.member?.id as string) || nrCheck(newState.member?.id as string)) return;
if (oldState.guild.id !== interaction.guildId) return;
if (newState.guild.id !== interaction.guildId) return;
if (!oldState.channel && newState.channel) {
if (newState.member?.user.bot) return;
if (voiceChannels.get(interaction.guildId as string).recognition.recognizing.find((recognizing: { member: GuildMember }) => recognizing.member.id === newState.member?.id)) return;
const { webhook, recognizer, voice, filledSilence } = await addSpeechRecognizeMember(newState.member as GuildMember, interaction.guildId as string, interaction.channel as BaseGuildTextChannel);
voiceChannels.get(interaction.guildId as string).recognition.recognizing.push({
member: newState.member as GuildMember,
webhook,
recognizer,
voice,
filledSilence
});
await interaction.followUp({
"content": "音声認識を更新しました。",
"embeds": [{
"title": "音声認識",
"description": "音声認識を更新しました。",
"color": Colors.Green
}]
});
}
if (oldState.channel && !newState.channel) {
if (oldState.member?.user.bot) return;
const recognizingMember = voiceChannels.get(interaction.guildId as string).recognition.recognizing.find((recognizing: { member: GuildMember }) => recognizing.member.id === oldState.member?.id);
if (!recognizingMember) return;
recognizingMember.voice.destroy();
recognizingMember.filledSilence.destroy();
recognizingMember.recognizer.free();
recognizingMember.webhook.delete();
voiceChannels.get(interaction.guildId as string).recognition.recognizing.splice(voiceChannels.get(interaction.guildId as string).recognition.recognizing.indexOf(recognizingMember), 1);
await interaction.followUp({
"content": "音声認識を更新しました。",
"embeds": [{
"title": "音声認識",
"description": "音声認識を更新しました。",
"color": Colors.Green
}]
});
}
};
client.on("voiceStateUpdate", onVoiceStateUpdate);
voiceChannels.get(interaction.guildId as string).recognition = {
"onVoiceStateUpdate": onVoiceStateUpdate,
"members": recognitionMembers,
"recognizing": recognizing
};
voiceChannels.get(interaction.guildId as string).player.play(soundEffects.enable());
await interaction.reply({
"content": "音声認識を開始しました。",
"embeds": [{
"title": "音声認識",
"description": "音声認識を開始しました。",
"color": Colors.Green
}]
});
}
});
client.on("interactionCreate", async interaction => {
if (nrCheck(interaction.user.id) || nrCheck(interaction.guildId as string)) return;
if (!interaction.isCommand()) return;
if (!interaction.member) {
await interaction.reply("申し訳ございませんが、DMでは使用できません。");
return;
}
if (interactionCommands.has(interaction.commandName)) {
const command = interactionCommands.get(interaction.commandName);
if (command !== undefined) {
command(interaction as ChatInputCommandInteraction);
} else {
await interaction.reply({
"content": "エラーが発生しました。",
"embeds": [{
"title": "エラー",
"description": "コマンドが見つかりません。",
"color": Colors.Red
}],
"ephemeral": true
});
}
}
});
client.on("interactionCreate", async interaction => {
if (nrCheck(interaction.user.id) || nrCheck(interaction.guildId as string)) return;
if (!interaction.isButton()) return;
if (interaction.customId.startsWith("help-")) {
const page = Number(interaction.customId.split("-")[1]);
await interaction.update({
content: "VoiceJPの使い方を表示します。",
embeds: [await helpPages[page]()],
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId(`help-${page - 1}`)
.setLabel("前へ")
.setStyle(ButtonStyle.Primary)
.setDisabled(page === 0),
new ButtonBuilder()
.setCustomId(`help-${page + 1}`)
.setLabel("次へ")
.setStyle(ButtonStyle.Primary)
.setDisabled(page === helpPages.length - 1)
)
]
});
}
});
client.on("guildCreate", async (guild) => {
if (nrCheck(guild.id)) {
await guild.leave();
}
guild.systemChannel?.send({
"content": "VoiceJPを追加していただきありがとうございます。",
"embeds": [
new EmbedBuilder()
.setTitle("VoiceJPを追加していただきありがとうございます")
.setDescription(`VoiceJPは、日本語で音声読み上げと文字起こしができるDiscordアプリです。\n\n詳しい使い方は、</help:${(await client.application?.commands.fetch())?.find((command) => command.name === "help")?.id}>をご覧いただくか、[公式ウェブサイト](https://voicejp.renorari.net/)をご覧ください。`)
.setColor(Colors.LuminousVividPink)
.setImage(client.user?.banner as string)
.setFooter({
"text": "VoiceJP"
})
]
});
});
client.login(token);
process.on("uncaughtException", function (error) {
console.error(error);
});