- Settings will now get the active device(s) available on Linux (UNTESTED) - Added CLI handler for interacting with the CLI - Added wrapper for extracting values with regex -
148 lines
3.6 KiB
JavaScript
148 lines
3.6 KiB
JavaScript
import {
|
|
NoSubscriberBehavior,
|
|
StreamType,
|
|
createAudioPlayer,
|
|
createAudioResource,
|
|
entersState,
|
|
AudioPlayerStatus,
|
|
VoiceConnectionStatus,
|
|
joinVoiceChannel,
|
|
getVoiceConnection,
|
|
} from '@discordjs/voice';
|
|
|
|
import { GatewayIntentBits } from 'discord-api-types/v10';
|
|
|
|
import { Client, Events, ActivityType } from 'discord.js';
|
|
|
|
import prism_media from 'prism-media';
|
|
const { FFmpeg } = prism_media;
|
|
|
|
// Import the DAB settings from the dynamic settings file
|
|
import {device, maxTransmissionGap, type} from './dabSettings.mjs'
|
|
|
|
const player = createAudioPlayer({
|
|
behaviors: {
|
|
noSubscriber: NoSubscriberBehavior.Play,
|
|
maxMissedFrames: Math.round(maxTransmissionGap / 20),
|
|
},
|
|
});
|
|
|
|
function attachRecorder() {
|
|
player.play(
|
|
createAudioResource(
|
|
new FFmpeg({
|
|
args: [
|
|
'-analyzeduration',
|
|
'0',
|
|
'-loglevel',
|
|
'0',
|
|
'-f',
|
|
type,
|
|
'-i',
|
|
type === 'dshow' ? `audio=${device}` : device,
|
|
'-acodec',
|
|
'libopus',
|
|
'-f',
|
|
'opus',
|
|
'-ar',
|
|
'48000',
|
|
'-ac',
|
|
'2',
|
|
],
|
|
}),
|
|
{
|
|
inputType: StreamType.OggOpus,
|
|
},
|
|
),
|
|
);
|
|
console.log('Attached recorder - ready to go!');
|
|
}
|
|
|
|
player.on('stateChange', (oldState, newState) => {
|
|
if (oldState.status === AudioPlayerStatus.Idle && newState.status === AudioPlayerStatus.Playing) {
|
|
console.log('Playing audio output on audio player');
|
|
} else if (newState.status === AudioPlayerStatus.Idle) {
|
|
console.log('Playback has stopped. Attempting to restart.');
|
|
attachRecorder();
|
|
}
|
|
});
|
|
|
|
/**
|
|
*
|
|
* @param {any} channel
|
|
* @returns {any}
|
|
*/
|
|
export async function connectToChannel(channel) {
|
|
const connection = joinVoiceChannel({
|
|
channelId: channel.id,
|
|
guildId: channel.guild.id,
|
|
adapterCreator: channel.guild.voiceAdapterCreator,
|
|
});
|
|
try {
|
|
await entersState(connection, VoiceConnectionStatus.Ready, 30_000);
|
|
await connection.subscribe(player);
|
|
return connection;
|
|
} catch (error) {
|
|
connection.destroy();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function getVoiceChannelFromID(client, channelID) {
|
|
return client.channels.cache.get(channelID)
|
|
}
|
|
|
|
export async function checkIfConnectedToVC(guildId) {
|
|
const connection = getVoiceConnection(guildId)
|
|
console.log("Connection!", connection);
|
|
return connection
|
|
}
|
|
|
|
export const getVoiceConnectionFromGuild = async (guildId) => {
|
|
return getVoiceConnection(guildId);
|
|
}
|
|
|
|
export async function initDiscordBotClient(token, systemName, readyCallback) {
|
|
const client = new Client({
|
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.MessageContent],
|
|
});
|
|
|
|
client.on(Events.ClientReady, () => {
|
|
console.log('discord.js client is ready!');
|
|
|
|
// Attach the recorder to the VC connection
|
|
attachRecorder();
|
|
|
|
// Set the activity of the bot user
|
|
client.user.setPresence({
|
|
activities: [{ name: `${systemName}`, type: ActivityType.Listening }],
|
|
});
|
|
|
|
//
|
|
readyCallback(client);
|
|
});
|
|
|
|
/* on event create
|
|
// TODO - Implement methods for discord users to interact directly with the bots for realtime info (last talked ID/TG, etc.)
|
|
client.on(Events.MessageCreate, async (message) => {
|
|
if (!message.guild) return;
|
|
console.log(`New Message:`, message.content);
|
|
if (message.content === '-join') {
|
|
const channel = message.member?.voice.channel;
|
|
if (channel) {
|
|
try {
|
|
const connection = await connectToChannel(channel);
|
|
connection.subscribe(player);
|
|
await message.reply('Playing now!');
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
} else {
|
|
await message.reply('Join a voice channel then try again!');
|
|
}
|
|
}
|
|
});
|
|
*/
|
|
|
|
client.login(token);
|
|
} |