™Z1cool
™Z1cool
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by ™Z1cool on 1/3/2024 in #djs-questions
Discord TTS bot
Hi there I am trying to create a discord bot where it would join a vc that I am in and when I use !t <Text> it wold join the vc I am in and would use a API to start talking and saying the text. (Yes I'm aware of discord TTS but wanted to do it as a learning experience). But when i start the bot and execute the command it doesn't work nor sends anything to the terminal that it has received anything please help thank you. Here's the code
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);
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);
38 replies