require("dotenv").config();
const { Client, GatewayIntentBits } = require("discord.js");
const intents = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates
];
const client = new Client({ intents });
client.on("ready", async() => {
console.log("Bot is ready!");
});
client.on("message", async (message) => {
console.log("Message received:", message.content);
if (message.content.toLowerCase() === "!t") {
try {
const args = message.content.trim().split(/ +/);
console.log("Arguments:", args);
const command = args.shift().toLowerCase();
console.log("Command:", command);
if (command === "!t") {
const textToSpeak = args.join(" ");
console.log("Command received:", textToSpeak);
if (!message.member.voice.channel) {
return message.reply("You need to be in a voice channel to use this command.");
}
console.log("Attempting to join voice channel:", message.member.voice.channel.name);
const connection = await message.member.voice.channel.join();
console.log("Joined voice channel:", connection.channel.name);
const dispatcher = connection.play(`http://ttsmp3.com/text-to-speech/${encodeURIComponent(textToSpeak)}`);
dispatcher.on("finish", () => {
console.log("Finished playing TTS. Disconnecting from voice channel.");
connection.disconnect();
});
}
} catch (error) {
console.error("Error:", error);
}
}
});
client.login(process.env.BOT_TOKEN);