chinese wintrader
chinese wintrader
DIAdiscord.js - Imagine an app
Created by chinese wintrader on 10/29/2023 in #djs-voice
TTS
i want to create a simple discord bot, that will read user messages in specific channel and bot will use TTS in voice channel to read user message. so basically if user joins - bot joins also and should play TTS with the message. but it doesnt work for me - bot joins after sending a message but its not playing TTS. any ideas? Code:
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, VoiceConnectionStatus } = require("@discordjs/voice");
const { Client, GatewayIntentBits } = require('discord.js');
const googleTTS = require('google-tts-api');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const token = 'yea';

const audioPlayer = createAudioPlayer();
let voiceConnection = null;

client.on('messageCreate', async (message) => {
if (message.channelId === '1168302709024239656') {
const userVoiceChannel = message.member.voice.channel;
if (userVoiceChannel) {
if (voiceConnection) {
voiceConnection.destroy();
}
voiceConnection = joinVoiceChannel({
channelId: userVoiceChannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});

voiceConnection.on(VoiceConnectionStatus.Ready, async () => {
const text = message.content;
const audioURL = googleTTS.getAudioUrl(text, {
lang: 'pl',
slow: false,
});

const audioResource = createAudioResource(audioURL, {
inputType: StreamType.Arbitrary,
});

audioPlayer.play(audioResource);
});

audioPlayer.on('end', () => {
if (voiceConnection) {
voiceConnection.destroy();
}
});
}
}
});

client.login(token);
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, VoiceConnectionStatus } = require("@discordjs/voice");
const { Client, GatewayIntentBits } = require('discord.js');
const googleTTS = require('google-tts-api');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const token = 'yea';

const audioPlayer = createAudioPlayer();
let voiceConnection = null;

client.on('messageCreate', async (message) => {
if (message.channelId === '1168302709024239656') {
const userVoiceChannel = message.member.voice.channel;
if (userVoiceChannel) {
if (voiceConnection) {
voiceConnection.destroy();
}
voiceConnection = joinVoiceChannel({
channelId: userVoiceChannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});

voiceConnection.on(VoiceConnectionStatus.Ready, async () => {
const text = message.content;
const audioURL = googleTTS.getAudioUrl(text, {
lang: 'pl',
slow: false,
});

const audioResource = createAudioResource(audioURL, {
inputType: StreamType.Arbitrary,
});

audioPlayer.play(audioResource);
});

audioPlayer.on('end', () => {
if (voiceConnection) {
voiceConnection.destroy();
}
});
}
}
});

client.login(token);
2 replies
DIAdiscord.js - Imagine an app
Created by chinese wintrader on 8/19/2023 in #djs-questions
embed with copy bar
4 replies
DIAdiscord.js - Imagine an app
Created by chinese wintrader on 8/10/2023 in #djs-questions
why this is not working?
const Discord = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.DirectMessageReactions,
Discord.GatewayIntentBits.GuildEmojisAndStickers,
]
});

client.once('ready', async () => {
console.log('bot is ready!');
});

const prefix = '!';

const pages = [
{
title: 'general commands [1/3]',
description: '!kick - kick user\n!ban - ban user',
emojiLeft: '<:prev:1138580435362906245>',
emojiRight: '<:next:1138580432963780708>',
page: 0
},
{
title: 'song commands [2/3]',
description: '!play - play song\n!skip - skip song',
emojiLeft: '<:prev:1138580435362906245>',
emojiRight: '<:next:1138580432963780708>',
page: 1
},
{
title: 'idk man commands [3/3]',
description: '!command 1 - a command \n!command 2 - a second command',
emojiLeft: '<:prev:1138580435362906245>',
emojiRight: '<:next:1138580432963780708>',
page: 2
}
];

client.on('messageCreate', async message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;

const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();

if (command === 'help' || command === 'commands') {
let page = 0;

const embed = new Discord.EmbedBuilder()
.setTitle(pages[page].title)
.setDescription(pages[page].description)
.setFooter({ text: `Page ${page + 1}/${pages.length}` })
.setColor(0x0099FF);

const msg = await message.channel.send({ embeds: [embed] });

await msg.react(pages[page].emojiLeft);
await msg.react(pages[page].emojiRight);

const filter = (reaction, user) => {
return [pages[page].emojiLeft, pages[page].emojiRight].includes(reaction.emoji.name) && user.id === message.author.id;
};

const collector = msg.createReactionCollector({ filter, time: 60000 });

collector.on('collect', async (reaction, user) => {
if (user.id === message.author.id) {
reaction.users.remove(user);

if (reaction.emoji.name === pages[page].emojiLeft) {
page = (page - 1 + pages.length) % pages.length;
} else if (reaction.emoji.name === pages[page].emojiRight) {
page = (page + 1) % pages.length;
}

embed.setTitle(pages[page].title)
.setDescription(pages[page].description)
.setFooter({ text: `Page ${page + 1}/${pages.length}` });

const editMsg = await msg.edit({ embeds: [embed] });
await editMsg.reactions.removeAll().catch(error => console.error('Failed to remove reactions: ', error));
await editMsg.react(pages[page].emojiLeft);
await editMsg.react(pages[page].emojiRight);
}
});

collector.on('end', async () => {
await msg.reactions.removeAll().catch(error => console.error('Failed to remove reactions: ', error));
});
}
});

client.login('yeah');
const Discord = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.DirectMessageReactions,
Discord.GatewayIntentBits.GuildEmojisAndStickers,
]
});

client.once('ready', async () => {
console.log('bot is ready!');
});

const prefix = '!';

const pages = [
{
title: 'general commands [1/3]',
description: '!kick - kick user\n!ban - ban user',
emojiLeft: '<:prev:1138580435362906245>',
emojiRight: '<:next:1138580432963780708>',
page: 0
},
{
title: 'song commands [2/3]',
description: '!play - play song\n!skip - skip song',
emojiLeft: '<:prev:1138580435362906245>',
emojiRight: '<:next:1138580432963780708>',
page: 1
},
{
title: 'idk man commands [3/3]',
description: '!command 1 - a command \n!command 2 - a second command',
emojiLeft: '<:prev:1138580435362906245>',
emojiRight: '<:next:1138580432963780708>',
page: 2
}
];

client.on('messageCreate', async message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;

const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();

if (command === 'help' || command === 'commands') {
let page = 0;

const embed = new Discord.EmbedBuilder()
.setTitle(pages[page].title)
.setDescription(pages[page].description)
.setFooter({ text: `Page ${page + 1}/${pages.length}` })
.setColor(0x0099FF);

const msg = await message.channel.send({ embeds: [embed] });

await msg.react(pages[page].emojiLeft);
await msg.react(pages[page].emojiRight);

const filter = (reaction, user) => {
return [pages[page].emojiLeft, pages[page].emojiRight].includes(reaction.emoji.name) && user.id === message.author.id;
};

const collector = msg.createReactionCollector({ filter, time: 60000 });

collector.on('collect', async (reaction, user) => {
if (user.id === message.author.id) {
reaction.users.remove(user);

if (reaction.emoji.name === pages[page].emojiLeft) {
page = (page - 1 + pages.length) % pages.length;
} else if (reaction.emoji.name === pages[page].emojiRight) {
page = (page + 1) % pages.length;
}

embed.setTitle(pages[page].title)
.setDescription(pages[page].description)
.setFooter({ text: `Page ${page + 1}/${pages.length}` });

const editMsg = await msg.edit({ embeds: [embed] });
await editMsg.reactions.removeAll().catch(error => console.error('Failed to remove reactions: ', error));
await editMsg.react(pages[page].emojiLeft);
await editMsg.react(pages[page].emojiRight);
}
});

collector.on('end', async () => {
await msg.reactions.removeAll().catch(error => console.error('Failed to remove reactions: ', error));
});
}
});

client.login('yeah');
- bot is not removing emojis when you want to change page - bot is not changing pages
2 replies