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);
24 Replies
d.js toolkit
d.js toolkit14mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
treble/luna
treble/luna14mo ago
its messageCreate
™Z1cool
™Z1coolOP14mo ago
is this line of code? client.on("message", async (message) => {
treble/luna
treble/luna14mo ago
yes and that command check is pretty useless because you already check the command, just to check it again
™Z1cool
™Z1coolOP14mo ago
oh okay thanks
duck
duck14mo ago
you will also need the MessageContent intent to receive the content of messages
™Z1cool
™Z1coolOP14mo ago
so like this? client.on("messageCreate", async (message) => {
treble/luna
treble/luna14mo ago
yes
™Z1cool
™Z1coolOP14mo ago
Thanks Ill fix the other line instead of message.content?
treble/luna
treble/luna14mo ago
no you need to add the intent in your client constructor
™Z1cool
™Z1coolOP14mo ago
oh okay thanks So the message is getting recived but not joining vc nor telling me that I have to be in a vc
treble/luna
treble/luna14mo ago
show your updated code and also, you'd need the GuildVoiseStates intent Which you do so ignore that
™Z1cool
™Z1coolOP14mo ago
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("messageCreate", 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("messageCreate", 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);
when I try adding the
MessageContent
MessageContent
kept on getting an error so I will add that later on
treble/luna
treble/luna14mo ago
well That intent is the key of this working
™Z1cool
™Z1coolOP14mo ago
oh lol
d.js docs
d.js docs14mo ago
- Error [DisallowedIntents]: Privileged intent provided is not enabled or whitelisted. - Error: Used disallowed intents If you are using the GuildMembers, GuildPresences, or MessageContent intents, you need to enable them via Developer Portal > Your app > Bot > Privileged Gateway Intents
™Z1cool
™Z1coolOP14mo ago
heres the updated code
require("dotenv").config();
const { Client, GatewayIntentBits } = require("discord.js");

const intents = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent
];

const client = new Client({ intents });

client.on("ready", async () => {
console.log("Bot is ready!");
});

client.on("messageCreate", 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,
GatewayIntentBits.MessageContent
];

const client = new Client({ intents });

client.on("ready", async () => {
console.log("Bot is ready!");
});

client.on("messageCreate", 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);
but still isn't joining vc though when I use the command
treble/luna
treble/luna14mo ago
and what is logging also dont stringify your errors like that It'll make them useless
™Z1cool
™Z1coolOP14mo ago
it just logging Message received: !t test okay thanks
treble/luna
treble/luna14mo ago
well, !t test is not equal to !t your code expects it to be
™Z1cool
™Z1coolOP14mo ago
so now this is the new error
Message received: !t
Arguments: [ '!t' ]
Command: !t
Command received:
Attempting to join voice channel: General
Error: TypeError: message.member.voice.channel.join is not a function
Message received: !t
Arguments: [ '!t' ]
Command: !t
Command received:
Attempting to join voice channel: General
Error: TypeError: message.member.voice.channel.join is not a function
heres the code
require("dotenv").config();
const { Client, GatewayIntentBits } = require("discord.js");

const intents = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent
];

const client = new Client({ intents });

client.on("ready", async () => {
console.log("Bot is ready!");
});

client.on("messageCreate", 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 || !message.member.voice || !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,
GatewayIntentBits.MessageContent
];

const client = new Client({ intents });

client.on("ready", async () => {
console.log("Bot is ready!");
});

client.on("messageCreate", 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 || !message.member.voice || !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);
and I appreciate the help
duck
duck14mo ago
That'd be because that isn't a function
d.js docs
d.js docs14mo ago
guide Home: Introduction read more
duck
duck14mo ago
Whoops wrong introduction https://discordjs.guide/voice/

Did you find this page helpful?