Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable @typescript-eslint/no-unused-vars */
- /* eslint-disable @typescript-eslint/explicit-function-return-type */
- import 'dotenv/config';
- import { Client, Guild, IntentsBitField } from 'discord.js';
- import {
- joinVoiceChannel,
- EndBehaviorType,
- createAudioPlayer,
- createAudioResource,
- } from '@discordjs/voice';
- import prism from 'prism-media';
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore - node-wit's types are not up to date
- import { Wit } from 'node-wit';
- import { Readable } from 'node:stream';
- if (!process.env.DISCORD_TOKEN) throw new Error('DISCORD_TOKEN is not defined');
- if (!process.env.WIT_AI_TOKEN) throw new Error('WIT_AI_TOKEN is not defined');
- const witClient = new Wit({ accessToken: process.env.WIT_AI_TOKEN });
- const client = new Client({
- intents: [
- IntentsBitField.Flags.Guilds,
- IntentsBitField.Flags.GuildMessages,
- IntentsBitField.Flags.GuildVoiceStates,
- IntentsBitField.Flags.MessageContent,
- ],
- });
- // [...]
- // Drop 2 bytes every 2 bytes to convert stereo to mono
- const convertStereoToMono = (stereoData: Buffer): Buffer =>
- Buffer.from(stereoData.filter((_, index) => index % 4 < 2));
- client.on('voiceStateUpdate', (oldMember, newMember) => {
- if (
- oldMember.id !== process.env.MAFIOU_ID ||
- newMember.id !== process.env.MAFIOU_ID
- )
- return;
- const newUserChannel = newMember.channelId;
- const oldUserChannel = oldMember.channelId;
- if (newUserChannel && newUserChannel != '') {
- const connection = joinVoiceChannel({
- channelId: newUserChannel,
- guildId: newMember.guild.id,
- adapterCreator: newMember.guild.voiceAdapterCreator,
- selfDeaf: false,
- });
- const player = createAudioPlayer();
- connection.subscribe(player);
- if (newUserChannel === '') {
- connection.destroy();
- }
- const receiver = connection.receiver;
- receiver.speaking.on('start', (userId) => {
- const user = client.users.cache.get(userId)?.username ?? userId;
- console.log(`User ${user} started speaking`);
- const opusStream = connection.receiver.subscribe(userId, {
- end: {
- behavior: EndBehaviorType.AfterSilence,
- duration: 300,
- },
- });
- const audioBuffers: Buffer[] = [];
- opusStream
- .pipe(
- new prism.opus.Decoder({ rate: 48_000, channels: 2, frameSize: 960 }),
- )
- .on('data', (chunk) => {
- audioBuffers.push(chunk);
- })
- .on('end', async () => {
- const resultBuffer = Buffer.concat(audioBuffers);
- try {
- const voiceDuration = resultBuffer.length / 48_000 / 2;
- if (voiceDuration < 1) return; // Ignore environment noises
- const witResponse = await witClient.speech(
- 'audio/raw;endian=little;encoding=signed-integer;rate=48k;bits=16',
- Readable.from(convertStereoToMono(resultBuffer)),
- );
- console.log(
- `${user}: ${witResponse.text}`,
- witResponse,
- witResponse?.speech?.tokens,
- );
- if (!mav) return;
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- const mafiou = await mav.members.fetch(process.env.MAFIOU_ID);
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- const ftefane = await mav.members.fetch(process.env.FTEFANE_ID);
- // Actions associées à chaque phrase
- interface Actions {
- [key: string]: (guild: Guild) => Promise<void>;
- }
- const aboie = createAudioResource('../sounds/aboie.mp3');
- const actions: Actions = {
- // [...]
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
- 'aboie matthew': async (guild: Guild) => {
- console.log('oe');
- console.log(`aboie : ${aboie}`);
- player.play(aboie);
- },
- 'aboie mafiou': async (guild: Guild) => {
- console.log('oe');
- console.log(`aboie : ${aboie}`);
- player.play(aboie);
- },
- 'aboie mafieux': async (guild: Guild) => {
- console.log('oe');
- console.log(`aboie : ${aboie}`);
- player.play(aboie);
- },
- 'aboa mafiou': async (guild: Guild) => {
- console.log('oe');
- console.log(`aboie : ${aboie}`);
- player.play(aboie);
- },
- };
- // [...]
Advertisement
Add Comment
Please, Sign In to add comment