-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
97 lines (88 loc) · 2.75 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
if (
process.env.VOICEVOX_CORE === undefined ||
process.env.BOT_TOKEN === undefined
) {
throw Error('環境変数を設定してください')
}
import '@/src/db'
import {
ActivityType,
GatewayIntentBits,
GuildMember,
VoiceChannel,
} from 'discord.js'
import GuildSetting from '@/model/guildSetting'
import packageJson from '@/package.json'
import Client from '@/src/client'
import {
CustomApplicationCommandData,
getConnectionManager,
} from '@/src/command'
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildIntegrations,
],
})
client.on('messageCreate', async (message) => {
if (message.author.bot) return
await client.textToSpeechBot.messageCatcher(message)
})
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return
try {
const command = client.commandList.find(
(v) => v.name === interaction.commandName
) as CustomApplicationCommandData
const connectionManager = await getConnectionManager(interaction, client)
await command.execute(interaction, connectionManager)
} catch (e) {
console.log(e)
}
})
client.on('voiceStateUpdate', async (oldState, newState) => {
const connectionManager = client.connectionManagers.get(oldState.guild.id)
if (!connectionManager) return
if (
!oldState.channel &&
newState.channelId === connectionManager.connection.joinConfig.channelId
) {
const member = oldState.member as GuildMember
connectionManager.readQueue.push({
content: `${member.nickname || member.user.displayName}が入室しました`,
userName: '',
})
await connectionManager.readText()
} else if (
oldState.channelId === connectionManager.connection.joinConfig.channelId &&
!newState.channelId
) {
if (
oldState.channel instanceof VoiceChannel &&
oldState.channel.members.size === 1
) {
await client.textToSpeechBot.ttsModeOff(connectionManager)
} else {
const member = oldState.member as GuildMember
connectionManager.readQueue.push({
content: `${member.nickname || member.user.displayName}が退室しました`,
userName: '',
})
await connectionManager.readText()
}
}
})
client.on('ready', async () => {
console.log('botを正常に起動しました')
client.user?.setActivity(`TTS Botだよ!(Ver: ${packageJson.version})`, {
type: ActivityType.Playing,
})
await GuildSetting.sync()
if (!client.application?.owner) await client.application?.fetch()
await client.application!.commands.set(client.commandList)
})
void client.login(process.env.BOT_TOKEN)