Skip to content

Instantly share code, notes, and snippets.

@ayunami2000
Created July 20, 2022 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayunami2000/dc8c9b218b02ac4e096b356e04ba3712 to your computer and use it in GitHub Desktop.
Save ayunami2000/dc8c9b218b02ac4e096b356e04ba3712 to your computer and use it in GitHub Desktop.
tiktok discord bot (FREE)
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, NoSubscriberBehavior } = require('@discordjs/voice');
const { spawn } = require('child_process');
const fs = require('fs');
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const { Readable } = require('stream');
const { Buffer } = require('buffer');
const { Client, GatewayIntentBits } = require('discord.js');
const { AudioPlayerStatus } = require('@discordjs/voice');
const bot = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.MessageContent ] });
let playing = false;
bot.on('messageCreate', async message => {
if (message.author.bot) return;
if (!message.content.toLowerCase().startsWith('tts!')) return;
if (playing) return;
let texts = message.content.slice(4);
if (texts.length == 0) return;
texts = texts.match(/.{1,250}/g);
playing = true;
const channel = await bot.channels.fetch('804744454056968236');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
const player = createAudioPlayer();
const subscription = connection.subscribe(player);
for (let text of texts) {
const command = spawn('ffmpeg', ['-re', '-i', 'pipe:0', '-vn', '-c:a', 'libopus', '-f', 'ogg', 'pipe:1']);
try {
const base64 = (await (await fetch("https://api16-normal-useast5.us.tiktokv.com/media/api/text/speech/invoke/?text_speaker=en_us_002&req_text=" + encodeURIComponent(text), { method: 'POST' })).json()).data.v_str;
Readable.from(Buffer.from(base64, 'base64')).pipe(command.stdin);
player.play(createAudioResource(command.stdout, { inputType: StreamType.OggOpus }));
await new Promise(r => player.on(AudioPlayerStatus.Idle, r));
} catch (e) {
console.error(e);
break;
}
}
player.stop();
connection.destroy();
playing = false;
});
bot.login('TOKEN');
{
"dependencies": {
"@discordjs/opus": "^0.8.0",
"@discordjs/voice": "^0.11.0",
"discord.js": "^14.0.3",
"libsodium-wrappers": "^0.7.10",
"node-fetch": "^3.2.9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment