yaejunyang/packages/bot/events/readChannel.ts
2026-05-19 17:47:17 +00:00

73 lines
No EOL
2.1 KiB
TypeScript

import { getOrCreateVoiceConnection } from "../util";
import { getUserProfile, hasGuildReadChannel } from "../db";
import { defineEvent } from "../event";
import { playVoice, PlayVoiceOptions } from "../tts";
import { Voice } from "../../db/generated/prisma/enums";
export default defineEvent("messageCreate", async (message) => {
if (message.author.bot)
return;
const guild = message.guild;
if (guild == null)
return;
const hasChannel = await hasGuildReadChannel(guild.id, message.channelId);
if (!hasChannel)
return;
const profile = await getUserProfile(message.author.id);
let content = message.cleanContent;
let voice: Voice | null = null
let options: PlayVoiceOptions | undefined = undefined;
let matched: RegExpMatchArray | null = null;
if (content.startsWith("$t ")) {
voice = "TypeCast";
} else if (content.startsWith("$p ")) {
voice = "Papago";
} else if (matched = content.match(/^\$s(\S*) /)) {
voice = "Supertonic";
let style: string | undefined = undefined
if (matched[1].length) {
style = matched[1]
}
options = { supertonicStyleId: style };
} else if (content.match(/^\$\s/)) {
return;
}
if (voice) {
content = content.replace(/^\$\S+\s+/, "")
} else {
voice = profile.voice;
}
try {
if (!await getOrCreateVoiceConnection(guild))
return;
if (!guild.members.me?.voice.channel)
return;
if (message.content === "") {
return await playVoice(
guild,
profile,
voice,
content =
message.attachments.size > 0
? `${message.attachments.size} 개의 첨부파일`
: "알수없는 메시지"
);
} else {
for (const text of content.split("\n")) {
await playVoice(guild, profile, voice, text);
}
}
} catch(err) {
message.reply("말이 꼬이네요 ㅜ.ㅜ");
console.log("playVoice failed. ", err);
}
})