y
DIAdiscord.js - Imagine an app
•Created by y on 1/28/2024 in #djs-questions
@discordjs/voice with @discordjs/next
I expected the audio to play, but it does not.
No errors when running.
I couldn't find the answer on GitHub, this server, or Google. Do I need to implement @discordjs/voice myself?
// (Let me know if it needs to be a working code)
import { GatewaySendPayload } from "@discordjs/core";
import {
DiscordGatewayAdapterLibraryMethods,
createAudioPlayer,
createAudioResource,
joinVoiceChannel,
} from "@discordjs/voice";
import { WebSocketManager } from "@discordjs/ws";
const connection = joinVoiceChannel({
adapterCreator: voiceAdapterCreator(channelId, gateway),
guildId,
channelId,
});
const resource = createAudioResource("./rickroll.mp3");
const player = createAudioPlayer();
const adapters = new Map();
player.play(resource);
connection.subscribe(player);
function voiceAdapterCreator(channelId: string, gateway: WebSocketManager) {
// from: https://github.com/discordjs/discord.js/blob/bfc7bb55641c60d4d67e57c27c9d1e63b6f30c9b/packages/discord.js/src/structures/Guild.js#L1410
return (methods: DiscordGatewayAdapterLibraryMethods) => {
adapters.set(channelId, methods);
return {
sendPayload: (data: GatewaySendPayload) => {
gateway.send(0, data);
return true;
},
destroy: () => {
adapters.delete(channelId);
},
};
};
}
// (Let me know if it needs to be a working code)
import { GatewaySendPayload } from "@discordjs/core";
import {
DiscordGatewayAdapterLibraryMethods,
createAudioPlayer,
createAudioResource,
joinVoiceChannel,
} from "@discordjs/voice";
import { WebSocketManager } from "@discordjs/ws";
const connection = joinVoiceChannel({
adapterCreator: voiceAdapterCreator(channelId, gateway),
guildId,
channelId,
});
const resource = createAudioResource("./rickroll.mp3");
const player = createAudioPlayer();
const adapters = new Map();
player.play(resource);
connection.subscribe(player);
function voiceAdapterCreator(channelId: string, gateway: WebSocketManager) {
// from: https://github.com/discordjs/discord.js/blob/bfc7bb55641c60d4d67e57c27c9d1e63b6f30c9b/packages/discord.js/src/structures/Guild.js#L1410
return (methods: DiscordGatewayAdapterLibraryMethods) => {
adapters.set(channelId, methods);
return {
sendPayload: (data: GatewaySendPayload) => {
gateway.send(0, data);
return true;
},
destroy: () => {
adapters.delete(channelId);
},
};
};
}
6 replies