/
index.js
136 lines (121 loc) · 3.93 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
require("dotenv").config();
const { Client, GatewayIntentBits, REST, Routes } = require("discord.js");
const {
joinVoiceChannel,
createAudioResource,
createAudioPlayer,
getVoiceConnection,
} = require("@discordjs/voice");
const tts = require("google-translate-tts");
const { Readable } = require("stream");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const BOT_TOKEN = process.env.BOT_TOKEN;
client.once("ready", async () => {
console.log("Ready!");
const commandsData = [
{
name: "leave",
description: "Leave the voice channel",
},
{
name: "say",
description: "Make the bot speak a message",
options: [
{
name: "text",
type: 3,
description: "Text to be spoken",
required: true,
},
],
},
];
await new REST({ version: "10" })
.setToken(BOT_TOKEN)
.put(Routes.applicationCommands(client.user.id), { body: commandsData });
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand() || interaction.channel.name !== "tts") return;
const { commandName } = interaction;
if (commandName === "leave") {
if (interaction?.guild?.members?.me?.voice?.channel) {
const connection = getVoiceConnection(
interaction?.guild?.members?.me?.voice?.channel?.guild?.id
);
connection.destroy();
await interaction.reply("Left the voice channel!");
} else {
await interaction.reply("I am not in a voice channel!");
}
} else if (commandName === "say") {
let text = interaction.options.getString("text");
const mentionRegex = /<@!?(\d+)>/g;
text = text.replace(mentionRegex, (match, userId) => {
const user = client.users.cache.get(userId);
return user ? (user.globalName ? user.globalName : user.username) : match;
});
if (!interaction?.member?.voice?.channel?.joinable)
return interaction.channel.send(
"I need permission to join your voice channel!"
);
const connection = joinVoiceChannel({
channelId: interaction?.member?.voice?.channel?.id,
guildId: interaction?.member?.voice?.guild?.id,
adapterCreator: interaction?.member?.voice?.guild?.voiceAdapterCreator,
});
const player = createAudioPlayer();
connection.subscribe(player);
const buffer = await tts.synthesize({
text: text,
voice: "hi",
accent: "co.in",
});
const stream = Readable.from(buffer);
const resource = createAudioResource(stream);
player.play(resource);
return interaction.reply(
`${
interaction?.member?.user?.globalName ||
interaction?.member?.user?.username
} said "${text}"`
);
}
});
client.on("messageCreate", async (message) => {
if (message.author.bot || message.channel.name !== "tts") return;
if (!message.content.startsWith("!")) {
if (!message.member?.voice?.channel?.joinable)
return message.channel.send(
"I need permission to join your voice channel!"
);
const connection = joinVoiceChannel({
channelId: message?.member?.voice?.channel?.id,
guildId: message?.member?.voice?.guild?.id,
adapterCreator: message?.member?.voice?.guild?.voiceAdapterCreator,
});
const player = createAudioPlayer();
connection.subscribe(player);
let text = message.content;
const mentionRegex = /<@!?(\d+)>/g;
text = text.replace(mentionRegex, (match, userId) => {
const user = client.users.cache.get(userId);
return user ? (user.globalName ? user.globalName : user.username) : match;
});
const buffer = await tts.synthesize({
text: text,
voice: "hi",
accent: "co.in",
});
const stream = Readable.from(buffer);
const resource = createAudioResource(stream);
player.play(resource);
}
});
client.login(BOT_TOKEN);