setup basic player and example

This commit is contained in:
Snowflake107 2021-06-12 00:24:21 +05:45
parent 181131f755
commit a823c6f173
7 changed files with 58 additions and 15 deletions

7
.gitignore vendored
View file

@ -2,14 +2,11 @@
node_modules
package-lock.json
# Tests
test
# Compiled files
lib
# Yarn logs
yarn*.log
# Demo
demo
# example
example/config.ts

View file

@ -0,0 +1,3 @@
export const config = {
token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
};

36
example/index.ts Normal file
View file

@ -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);

View file

@ -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",

View file

@ -41,14 +41,14 @@ class DiscordPlayer extends EventEmitter<PlayerEvents> {
// @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<PlayerEvents> {
duration: m.durationFormatted,
raw: m
})
);
});
}
default:
return [];
}
}

View file

@ -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 ?? "";

View file

@ -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";