yaejunyang/packages/bot/events/readChannel.ts

72 lines
No EOL
2.2 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";
import { SUPERTONIC_DEFAULT_VOICE } from "../../env";
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 = {};
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";
if (matched[1].length) {
options.supertonicStyleId = matched[1]
}
} else if (content.match(/^\$\s/)) {
return;
}
if (profile.userSupertonicStyle.length) {
options.supertonicStyleId ??= profile.userSupertonicStyle;
}
options.supertonicStyleId ??= SUPERTONIC_DEFAULT_VOICE;
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 === "") {
content = message.attachments.size > 0
? `${message.attachments.size} 개의 첨부파일`
: "알수없는 메시지"
if (message.attachments.size == 1 && Math.random() < 0.05) {
content = "어이, 유저. 일개의 첨부파일이 뭘 할 수 있지?"
}
}
await playVoice(guild, profile, voice, content, options);
} catch(err) {
message.reply("말이 꼬이네요 ㅜ.ㅜ");
console.log("playVoice failed. ", err);
}
})