Skip to content

Commit

Permalink
Merge branch 'player-client-adapter-wip' of https://github.com/androz…
Browse files Browse the repository at this point in the history
…2091/discord-player into player-client-adapter-wip
  • Loading branch information
mariusbegby committed Aug 12, 2024
2 parents 5d24cbc + 1fadd75 commit 8f9b329
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 29 deletions.
72 changes: 49 additions & 23 deletions packages/discord-player/src/clientadapter/ClientAdapterFactory.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,69 @@
import { Client as DiscordJsClient } from 'discord.js';
import { Client as ErisClient } from 'eris';
import type { Client as DiscordJsClient } from 'discord.js';
import type { Client as ErisClient } from 'eris';
import { DiscordJsClientAdapter } from './DiscordJsClientAdapter';
import { ClientType, IClientAdapter } from './IClientAdapter';
import { IClientAdapter } from './IClientAdapter';
import { Util } from '../utils/Util';
import { detectClientMode } from './ClientModeDetector';

export type SupportedClient = DiscordJsClient | ErisClient;

export class ClientAdapterFactory {
static createClientAdapter(client: SupportedClient): IClientAdapter {
try {
const clientType = this.getClientType(client);
const libType = detectClientMode(client)

switch (clientType) {
case ClientType.DiscordJs:
return new DiscordJsClientAdapter((client as DiscordJsClient));
case ClientType.Eris:
try {
switch(libType) {
case "discord.js":
return new DiscordJsClientAdapter(client as DiscordJsClient)
case "eris": {
Util.warn(
`You are using an Eris client, some things may not work correctly. This is currently under experimental support and it is still recommended to use a discord.js client.`,
'ExperimentalClientInstance'
);
// return new ErisClientAdapter((client as ErisClient));
throw new Error('Eris client is not supported yet');
default:
throw new Error('Unsupported client type');
// return new ErisClientAdapter(client as ErisClient)
throw new Error("Eris client not supported yet")
}
default: {
throw new Error("Unsupported client")
}
}
} catch (error) {
throw new Error(`Failed to create client adapter: ${error}`);
throw new Error(`Failed to create client adapter\n\n${error}`)
}
}

private static getClientType(client: SupportedClient): ClientType {
if (client instanceof DiscordJsClient) {
return ClientType.DiscordJs;
}
/** old code in case I f it up */

if (client instanceof ErisClient) {
return ClientType.Eris;
}
// try {
// const clientType = this.getClientType(client);

return ClientType.Unknown;
// switch (clientType) {
// case ClientType.DiscordJs:
// return new DiscordJsClientAdapter((client as DiscordJsClient));
// case ClientType.Eris:
// Util.warn(
// `You are using an Eris client, some things may not work correctly. This is currently under experimental support and it is still recommended to use a discord.js client.`,
// 'ExperimentalClientInstance'
// );
// // return new ErisClientAdapter((client as ErisClient));
// throw new Error('Eris client is not supported yet');
// default:
// throw new Error('Unsupported client type');
// }
// } catch (error) {
// throw new Error(`Failed to create client adapter: ${error}`);
// }
}

/** old code just in case */
// private static getClientType(client: SupportedClient): ClientType {
// if (client instanceof DiscordJsClient) {
// return ClientType.DiscordJs;
// }

// if (client instanceof ErisClient) {
// return ClientType.Eris;
// }

// return ClientType.Unknown;
// }
}
62 changes: 62 additions & 0 deletions packages/discord-player/src/clientadapter/ClientModeDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { type SupportedClient } from "./ClientAdapterFactory";
import { ClientType } from "./IClientAdapter";

export interface ValidPackagesStructure {
name: ClientType;
test: () => boolean;
testClient: (client: SupportedClient) => boolean
}

export const VALID_PACKAGES: ValidPackagesStructure[] = [
{
name: "discord.js",
test() {
try {
require("discord.js")
return true
} catch {
return false
}
},
testClient(client: SupportedClient) {
try {
const { Client } = require("discord.js")

Check failure on line 23 in packages/discord-player/src/clientadapter/ClientModeDetector.ts

View workflow job for this annotation

GitHub Actions / ESLint

Require statement not part of import statement

Check failure on line 23 in packages/discord-player/src/clientadapter/ClientModeDetector.ts

View workflow job for this annotation

GitHub Actions / ESLint

Require statement not part of import statement

return client instanceof Client
} catch {
return false
}
}
},
{
name: "eris",
test() {
try {
require("eris")
return true
} catch {
return false
}
},
testClient(client) {
try {
const { Client } = require("eris")

Check failure on line 43 in packages/discord-player/src/clientadapter/ClientModeDetector.ts

View workflow job for this annotation

GitHub Actions / ESLint

Require statement not part of import statement

Check failure on line 43 in packages/discord-player/src/clientadapter/ClientModeDetector.ts

View workflow job for this annotation

GitHub Actions / ESLint

Require statement not part of import statement

return client instanceof Client
} catch {
return false
}
},
}
]

export function detectClientMode(client: SupportedClient): ClientType {
for(const pkg of VALID_PACKAGES) {
const isValid = pkg.test()
const isInstance = pkg.testClient(client)

if(isValid && isInstance) return pkg.name
}

return "unknown"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class DiscordJsClientAdapter implements IClientAdapter {
private client: Client;
private name = 'discord.js';

public clientType: ClientType = ClientType.DiscordJs;
public clientType: ClientType = ClientType.DiscordJS;

constructor(client: Client) {
this.client = client;
Expand Down
12 changes: 7 additions & 5 deletions packages/discord-player/src/clientadapter/IClientAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ type Channel = {
type: ChannelType;
}

export enum ClientType {
DiscordJs = 'DjsClient',
Eris = 'ErisClient',
Unknown = 'Unknown'
}
export const ClientType = {
DiscordJS: "discord.js",
Eris: "eris",
Unknown: "unknown"
} as const

export type ClientType = typeof ClientType[keyof typeof ClientType]

export interface IClientAdapter {
clientType: ClientType;
Expand Down

0 comments on commit 8f9b329

Please sign in to comment.