adapter creator for djs v12

This commit is contained in:
Snowflake107 2021-06-23 00:56:50 +05:45
parent 18aa70531e
commit 2b82fe2b70
7 changed files with 81 additions and 17 deletions

View file

@ -140,7 +140,7 @@ class Player extends EventEmitter<PlayerEvents> {
try {
prev.destroy();
} catch { } // eslint-disable-line no-empty
} catch {} // eslint-disable-line no-empty
this.queues.delete(guild.id);
return prev;
@ -171,9 +171,9 @@ class Player extends EventEmitter<PlayerEvents> {
const playlist = !data.playlist
? null
: new Playlist(this, {
...data.playlist,
tracks: []
});
...data.playlist,
tracks: []
});
const tracks = data.data.map(
(m) =>
@ -279,13 +279,13 @@ class Player extends EventEmitter<PlayerEvents> {
author:
spotifyPlaylist.type !== "playlist"
? {
name: spotifyPlaylist.artists[0]?.name ?? "Unknown Artist",
url: spotifyPlaylist.artists[0]?.external_urls?.spotify ?? null
}
name: spotifyPlaylist.artists[0]?.name ?? "Unknown Artist",
url: spotifyPlaylist.artists[0]?.external_urls?.spotify ?? null
}
: {
name: spotifyPlaylist.owner?.display_name ?? spotifyPlaylist.owner?.id ?? "Unknown Artist",
url: spotifyPlaylist.owner?.external_urls?.spotify ?? null
},
name: spotifyPlaylist.owner?.display_name ?? spotifyPlaylist.owner?.id ?? "Unknown Artist",
url: spotifyPlaylist.owner?.external_urls?.spotify ?? null
},
tracks: [],
id: spotifyPlaylist.id,
url: spotifyPlaylist.external_urls?.spotify ?? query,
@ -469,4 +469,4 @@ class Player extends EventEmitter<PlayerEvents> {
}
}
export { Player };
export { Player };

View file

@ -0,0 +1,62 @@
import { DiscordGatewayAdapterCreator, DiscordGatewayAdapterLibraryMethods } from "@discordjs/voice";
import { VoiceChannel, Snowflake, Client, Constants, WebSocketShard, Guild, StageChannel } from "discord.js";
import { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from "discord-api-types/v8";
const adapters = new Map<Snowflake, DiscordGatewayAdapterLibraryMethods>();
const trackedClients = new Set<Client>();
function trackClient(client: Client) {
if (trackedClients.has(client)) return;
trackedClients.add(client);
client.ws.on(Constants.WSEvents.VOICE_SERVER_UPDATE, (payload: GatewayVoiceServerUpdateDispatchData) => {
adapters.get(payload.guild_id)?.onVoiceServerUpdate(payload);
});
client.ws.on(Constants.WSEvents.VOICE_STATE_UPDATE, (payload: GatewayVoiceStateUpdateDispatchData) => {
if (payload.guild_id && payload.session_id && payload.user_id === client.user?.id) {
adapters.get(payload.guild_id)?.onVoiceStateUpdate(payload);
}
});
}
const trackedGuilds = new Map<WebSocketShard, Set<Snowflake>>();
function cleanupGuilds(shard: WebSocketShard) {
const guilds = trackedGuilds.get(shard);
if (guilds) {
for (const guildID of guilds.values()) {
adapters.get(guildID)?.destroy();
}
}
}
function trackGuild(guild: Guild) {
let guilds = trackedGuilds.get(guild.shard);
if (!guilds) {
const cleanup = () => cleanupGuilds(guild.shard);
guild.shard.on("close", cleanup);
guild.shard.on("destroyed", cleanup);
guilds = new Set();
trackedGuilds.set(guild.shard, guilds);
}
guilds.add(guild.id);
}
export function VoiceAdapterCreator(channel: VoiceChannel | StageChannel): DiscordGatewayAdapterCreator {
return (methods) => {
adapters.set(channel.guild.id, methods);
trackClient(channel.client);
trackGuild(channel.guild);
return {
sendPayload(data) {
if (channel.guild.shard.status === Constants.Status.READY) {
channel.guild.shard.send(data);
return true;
}
return false;
},
destroy() {
return adapters.delete(channel.guild.id);
}
};
};
}

View file

@ -1,5 +1,5 @@
import { VoiceChannel, StageChannel, Collection, Snowflake } from "discord.js";
import { entersState, joinVoiceChannel, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice";
import { DiscordGatewayAdapterCreator, entersState, joinVoiceChannel, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice";
import { StreamDispatcher } from "./BasicStreamDispatcher";
class VoiceUtils {
@ -47,12 +47,13 @@ class VoiceUtils {
options?: {
deaf?: boolean;
maxTime?: number;
adapter?: DiscordGatewayAdapterCreator;
}
) {
let conn = joinVoiceChannel({
guildId: channel.guild.id,
channelId: channel.id,
adapterCreator: (channel.guild as any).voiceAdapterCreator, // eslint-disable-line @typescript-eslint/no-explicit-any
adapterCreator: options.adapter ?? (channel.guild as any).voiceAdapterCreator, // eslint-disable-line @typescript-eslint/no-explicit-any
selfDeaf: Boolean(options.deaf)
});

View file

@ -6,4 +6,5 @@ export { Queue } from "./Structures/Queue";
export { Track } from "./Structures/Track";
export { VoiceUtils } from "./VoiceInterface/VoiceUtils";
export { VoiceEvents, StreamDispatcher } from "./VoiceInterface/BasicStreamDispatcher";
export { VoiceAdapterCreator } from "./VoiceInterface/AdapterCreator";
export * from "./types/types";

View file

@ -453,4 +453,4 @@ export interface PlaylistJSON {
export interface DiscordPlayerInitOptions {
autoRegisterExtractor?: boolean;
ytdlOptions?: downloadOptions;
}
}

View file

@ -113,4 +113,4 @@ const FilterList = {
};
export default FilterList;
export { FilterList as AudioFilters };
export { FilterList as AudioFilters };

View file

@ -96,8 +96,8 @@ class Util {
}
static get noop() {
return () => { }; // eslint-disable-line @typescript-eslint/no-empty-function
return () => {}; // eslint-disable-line @typescript-eslint/no-empty-function
}
}
export { Util };
export { Util };