dimsey
dimsey
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
I was thinking it might be a good way to share the underlying resources
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
Is there a certain reason for not running more than one bot on a node server?
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
Thanks @Qjuh , just got that figured out!
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
Figrued it out
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
Any help would be greatly appreciated ty ty!
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
export function initClient(token: string, botId: string) {
// If client already exists, destroy it and create a new one
if (botId in clientCache) {
console.warn(`Bot with ID ${botId} already exists.`);
const oldClient = clientCache[botId]
oldClient.destroy();
delete clientCache[botId];
}

try {
// Create client and add to cache to deal with resets, etc
const client = createClient();
clientCache[botId] = client;

// Setup client
errorEvent(client);
clientReadyEvent(client, "@ me to talk!!");
messageCreateEvent(client, botId);
loginClient(client, token);
} catch (e) {
console.log("error init")
}

}
export function initClient(token: string, botId: string) {
// If client already exists, destroy it and create a new one
if (botId in clientCache) {
console.warn(`Bot with ID ${botId} already exists.`);
const oldClient = clientCache[botId]
oldClient.destroy();
delete clientCache[botId];
}

try {
// Create client and add to cache to deal with resets, etc
const client = createClient();
clientCache[botId] = client;

// Setup client
errorEvent(client);
clientReadyEvent(client, "@ me to talk!!");
messageCreateEvent(client, botId);
loginClient(client, token);
} catch (e) {
console.log("error init")
}

}
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
Each bot runs this in its client
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 10/15/2023 in #djs-voice
Is it possible to have 2 bots in voice channels in discord js?
import { VoiceConnection, VoiceConnectionStatus, getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
import { ChannelType, Client, Events } from "discord.js";

export default function messageCreateEvent(client: Client, botId: string) {
client.on(Events.MessageCreate, async (message) => {
// Avoid bot replying to itself
if (message.author.bot) {
return;
}

if (client.user === null || client.user === undefined) {
await message.reply("Error occured: code 2001")
return;
}
// User DMs bot, bot has no guild context to join channels so user must do in server
else if (message.channel.type === ChannelType.DM) {
await message.reply('let\'s talk!! @ me in your server or in a specific voice channel');
}
else if (message.mentions.has(client.user)) {
const voiceConnection = getVoiceConnection(message.guildId ?? "")
if (voiceConnection && voiceConnection.state.status === VoiceConnectionStatus.Ready) {
// Bot is already connected to a voice channel
await message.reply("I'm already connected to a voice channel.");
return;
}

// Join voice channel of user if possible
if (message.channel.type === ChannelType.GuildVoice) {
console.log(`Bot ${client.user?.tag} was mentioned in voice channel ${message.channel.name}`);
// Join channel
// Listen and create connections -> out, listen for user
const voiceChannel = message.member?.voice.channel;

if (voiceChannel === undefined || voiceChannel === null) {
await message.reply("Error occured: code 2002")
return;
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

// Run voice functions
} else {
// Otherwise join other voice channel
const voiceChannels = message.guild?.channels.cache.filter(c => c.type === ChannelType.GuildVoice);
if (voiceChannels !== undefined && voiceChannels.size > 0) {
const firstVoiceChannel = voiceChannels.first();
if (firstVoiceChannel === undefined) {
return console.log("Error")
}
try {
const connection = joinVoiceChannel({
channelId: firstVoiceChannel.id,
guildId: firstVoiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: firstVoiceChannel.guild.voiceAdapterCreator,
});
connection.on("stateChange", (oldState, newState) => {
console.log("newstate", newState.status, oldState.status)
})
} catch (e) {
console.log("VOice connection failed: ", e)
}

message.reply(`pls join me in "${firstVoiceChannel.name}" voice channel`)
return;
}

// No voice channels in server.
else {
await message.reply({ content: 'There\'s no voice channels to join.'});
return console.log('No voice channels available to join.');
}
}
}
})
}
import { VoiceConnection, VoiceConnectionStatus, getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
import { ChannelType, Client, Events } from "discord.js";

export default function messageCreateEvent(client: Client, botId: string) {
client.on(Events.MessageCreate, async (message) => {
// Avoid bot replying to itself
if (message.author.bot) {
return;
}

if (client.user === null || client.user === undefined) {
await message.reply("Error occured: code 2001")
return;
}
// User DMs bot, bot has no guild context to join channels so user must do in server
else if (message.channel.type === ChannelType.DM) {
await message.reply('let\'s talk!! @ me in your server or in a specific voice channel');
}
else if (message.mentions.has(client.user)) {
const voiceConnection = getVoiceConnection(message.guildId ?? "")
if (voiceConnection && voiceConnection.state.status === VoiceConnectionStatus.Ready) {
// Bot is already connected to a voice channel
await message.reply("I'm already connected to a voice channel.");
return;
}

// Join voice channel of user if possible
if (message.channel.type === ChannelType.GuildVoice) {
console.log(`Bot ${client.user?.tag} was mentioned in voice channel ${message.channel.name}`);
// Join channel
// Listen and create connections -> out, listen for user
const voiceChannel = message.member?.voice.channel;

if (voiceChannel === undefined || voiceChannel === null) {
await message.reply("Error occured: code 2002")
return;
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

// Run voice functions
} else {
// Otherwise join other voice channel
const voiceChannels = message.guild?.channels.cache.filter(c => c.type === ChannelType.GuildVoice);
if (voiceChannels !== undefined && voiceChannels.size > 0) {
const firstVoiceChannel = voiceChannels.first();
if (firstVoiceChannel === undefined) {
return console.log("Error")
}
try {
const connection = joinVoiceChannel({
channelId: firstVoiceChannel.id,
guildId: firstVoiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: firstVoiceChannel.guild.voiceAdapterCreator,
});
connection.on("stateChange", (oldState, newState) => {
console.log("newstate", newState.status, oldState.status)
})
} catch (e) {
console.log("VOice connection failed: ", e)
}

message.reply(`pls join me in "${firstVoiceChannel.name}" voice channel`)
return;
}

// No voice channels in server.
else {
await message.reply({ content: 'There\'s no voice channels to join.'});
return console.log('No voice channels available to join.');
}
}
}
})
}
11 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 9/30/2023 in #djs-voice
Issue w/ Opus -> Ogg recording voice with [email protected] - ERR_REQUIRE_ESM
Ok! Well thank you so much, highlight of my day
10 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 9/30/2023 in #djs-voice
Issue w/ Opus -> Ogg recording voice with [email protected] - ERR_REQUIRE_ESM
or i can paste here quickly lol
10 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 9/30/2023 in #djs-voice
Issue w/ Opus -> Ogg recording voice with [email protected] - ERR_REQUIRE_ESM
@duck Do you mind adding me or something so I can send you the link? I could put it here but then anyone can see it
10 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 9/30/2023 in #djs-voice
Issue w/ Opus -> Ogg recording voice with [email protected] - ERR_REQUIRE_ESM
Thank you so much! That was my issue--my crc-node version was v3 when the documentation uses an older version. Also sending you nitro b/c that just saved me hours and hours
10 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 9/30/2023 in #djs-voice
Issue w/ Opus -> Ogg recording voice with [email protected] - ERR_REQUIRE_ESM
[email protected] + [email protected]
Stack trace
👂 Listening to dimsey8_0
/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/sr/Desktop/unicorn/Voice/node_modules/node-crc/lib/lib.js from /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js not supported.
Instead change the require of lib.js in /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js to a dynamic import() which is available in all CommonJS modules.
at require.extensions.<computed> [as .js] (/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851:20)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js:50:19)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/opus/OggLogicalBitstream.js:13:9)
at createListeningStream (file:///Users/sr/Desktop/unicorn/Voice/src/utils/createListeningStream.ts:18:23)
at Object.<anonymous> (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:42:21)
at Generator.next (<anonymous>)
at fulfilled (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:4:58) {
code: 'ERR_REQUIRE_ESM'
}
[nodemon] app crashed - waiting for file changes before starting...
Stack trace
👂 Listening to dimsey8_0
/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/sr/Desktop/unicorn/Voice/node_modules/node-crc/lib/lib.js from /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js not supported.
Instead change the require of lib.js in /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js to a dynamic import() which is available in all CommonJS modules.
at require.extensions.<computed> [as .js] (/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851:20)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js:50:19)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/opus/OggLogicalBitstream.js:13:9)
at createListeningStream (file:///Users/sr/Desktop/unicorn/Voice/src/utils/createListeningStream.ts:18:23)
at Object.<anonymous> (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:42:21)
at Generator.next (<anonymous>)
at fulfilled (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:4:58) {
code: 'ERR_REQUIRE_ESM'
}
[nodemon] app crashed - waiting for file changes before starting...
10 replies
DIAdiscord.js - Imagine an app
Created by dimsey on 9/30/2023 in #djs-voice
Issue w/ Opus -> Ogg recording voice with [email protected] - ERR_REQUIRE_ESM
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream';
import prism from 'prism-media';

function getDisplayName(userId: string, user?: User) {
return user ? `${user.username}_${user.discriminator}` : userId;
}

export function createListeningStream(receiver: VoiceReceiver, userId: string, user?: User) {

console.log(`👂 Listening to ${getDisplayName(userId, user)}`);
const sourceOpusStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000, // milliseconds of silence before ending? TODO: (sr) figure out what this does
},
});

const filename = `./recordings/AA-${Date.now()}-${getDisplayName(userId, user)}.ogg`; // TODO: (sr) make this a .ogg file once we have an encoder

const destinationStream = createWriteStream(filename);

const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({
channelCount: 2,
sampleRate: 48000,
}),
pageSizeControl: {
maxPackets: 10,
},
});

// TODO: (sr) Need an OPUS to OGG encoder between the two
pipeline(sourceOpusStream, oggStream, destinationStream, (err) => {
if (err) {
console.error('Pipeline error:', err);
} else {
console.log('Pipeline complete');
}
});
}
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream';
import prism from 'prism-media';

function getDisplayName(userId: string, user?: User) {
return user ? `${user.username}_${user.discriminator}` : userId;
}

export function createListeningStream(receiver: VoiceReceiver, userId: string, user?: User) {

console.log(`👂 Listening to ${getDisplayName(userId, user)}`);
const sourceOpusStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000, // milliseconds of silence before ending? TODO: (sr) figure out what this does
},
});

const filename = `./recordings/AA-${Date.now()}-${getDisplayName(userId, user)}.ogg`; // TODO: (sr) make this a .ogg file once we have an encoder

const destinationStream = createWriteStream(filename);

const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({
channelCount: 2,
sampleRate: 48000,
}),
pageSizeControl: {
maxPackets: 10,
},
});

// TODO: (sr) Need an OPUS to OGG encoder between the two
pipeline(sourceOpusStream, oggStream, destinationStream, (err) => {
if (err) {
console.error('Pipeline error:', err);
} else {
console.log('Pipeline complete');
}
});
}
10 replies