diff --git a/.gitignore b/.gitignore index 7033b56..2991299 100644 --- a/.gitignore +++ b/.gitignore @@ -2,14 +2,11 @@ node_modules package-lock.json -# Tests -test - # Compiled files lib # Yarn logs yarn*.log -# Demo -demo +# example +example/config.ts \ No newline at end of file diff --git a/example/config.example.ts b/example/config.example.ts new file mode 100644 index 0000000..1e2b5af --- /dev/null +++ b/example/config.example.ts @@ -0,0 +1,3 @@ +export const config = { + token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +}; \ No newline at end of file diff --git a/example/index.ts b/example/index.ts new file mode 100644 index 0000000..1f39dad --- /dev/null +++ b/example/index.ts @@ -0,0 +1,36 @@ +import { Client } from "discord.js"; +import { Player } from "../src/index"; +import { config } from "./config"; + +const client = new Client({ + intents: ['GUILD_VOICE_STATES', 'GUILD_MESSAGES', 'GUILDS'] +}); +const player = new Player(client); + +player.on("trackStart", (queue, track) => console.log(`Now playing: ${track.title} in ${queue.guild.name}!`)); + +client.on("ready", () => console.log("Bot is online!")); + +client.on("message", async message => { + if (!client.application.owner) await client.application.fetch(); + if (message.author.id !== client.application.owner.id) return; + + if (message.content.startsWith("!np") && message.guild.me.voice.channelID) { + const conn = player.getQueue(message.guild.id); + if (!conn) return; + return void message.channel.send(`Now Playing: **${conn.current.title}** (Played **${Math.floor(conn.connection.streamTime / 1000)} seconds**)`); + } + if (message.content.startsWith("!p") && message.member.voice.channelID) { + const queue = player.createQueue(message.guild); + const song = await player.search(message.content.slice(2).trim(), message.author).then(x => x[0]); + + if (!queue.connection) { + queue.connect(message.member.voice.channel) + .then(async q => { + await q.play(song); + }); + } + } +}); + +client.login(config.token); \ No newline at end of file diff --git a/package.json b/package.json index 4a97273..b9bb6e8 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "lib/" ], "scripts": { - "test": "cd test && ts-node index.ts", + "test": "cd example && ts-node index.ts", "build": "tsc", "format": "prettier --write \"src/**/*.ts\"", "lint": "tslint -p tsconfig.json", diff --git a/src/Player.ts b/src/Player.ts index 264021c..023db0a 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -41,14 +41,14 @@ class DiscordPlayer extends EventEmitter { // @todo: add extractors const qt = QueryResolver.resolve(query); switch (qt) { - case QueryType.YOUTUBE: { - const videos = await YouTube.search(qt, { + case QueryType.YOUTUBE_SEARCH: { + const videos = await YouTube.search(query, { type: "video" }); - return videos.map( - (m) => - new Track(this, { + return videos.map((m) => { + (m as any).source = "youtube"; + return new Track(this, { title: m.title, description: m.description, author: m.channel?.name, @@ -60,8 +60,10 @@ class DiscordPlayer extends EventEmitter { duration: m.durationFormatted, raw: m }) - ); + }); } + default: + return []; } } diff --git a/src/Structures/Track.ts b/src/Structures/Track.ts index bb9f4fd..e6c3183 100644 --- a/src/Structures/Track.ts +++ b/src/Structures/Track.ts @@ -1,4 +1,4 @@ -import { User } from "discord.js"; +import { User, Util } from "discord.js"; import { Player } from "../Player"; import { RawTrackData } from "../types/types"; import { Queue } from "./Queue"; @@ -14,7 +14,7 @@ class Track { public views!: number; public requestedBy!: User; public fromPlaylist!: boolean; - public raw!: RawTrackData; + public readonly raw!: RawTrackData; /** * Track constructor @@ -94,7 +94,7 @@ class Track { } private _patch(data: RawTrackData) { - this.title = data.title ?? ""; + this.title = Util.escapeMarkdown(data.title ?? ""); this.author = data.author ?? ""; this.url = data.url ?? ""; this.thumbnail = data.thumbnail ?? ""; diff --git a/src/index.ts b/src/index.ts index 60a36d7..cdbeb79 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,9 @@ export { AudioFilters } from "./utils/AudioFilters"; +export { ExtractorModel } from "./Structures/ExtractorModel"; +export { Playlist } from "./Structures/Playlist"; +export { Player } from "./Player"; +export { Queue } from "./Structures/Queue"; +export { Track } from "./Structures/Track"; export { PlayerError } from "./utils/PlayerError"; export { VoiceUtils } from "./VoiceInterface/VoiceUtils"; export { VoiceEvents, StreamDispatcher } from "./VoiceInterface/BasicStreamDispatcher";