-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'player-client-adapter-wip' of https://github.com/androz…
…2091/discord-player into player-client-adapter-wip
- Loading branch information
Showing
4 changed files
with
119 additions
and
29 deletions.
There are no files selected for viewing
72 changes: 49 additions & 23 deletions
72
packages/discord-player/src/clientadapter/ClientAdapterFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
packages/discord-player/src/clientadapter/ClientModeDetector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / ESLint
|
||
|
||
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 GitHub Actions / ESLint
|
||
|
||
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters