setup basic player and example
This commit is contained in:
parent
181131f755
commit
a823c6f173
7 changed files with 58 additions and 15 deletions
7
.gitignore
vendored
7
.gitignore
vendored
|
@ -2,14 +2,11 @@
|
||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
|
||||||
# Tests
|
|
||||||
test
|
|
||||||
|
|
||||||
# Compiled files
|
# Compiled files
|
||||||
lib
|
lib
|
||||||
|
|
||||||
# Yarn logs
|
# Yarn logs
|
||||||
yarn*.log
|
yarn*.log
|
||||||
|
|
||||||
# Demo
|
# example
|
||||||
demo
|
example/config.ts
|
3
example/config.example.ts
Normal file
3
example/config.example.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export const config = {
|
||||||
|
token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
||||||
|
};
|
36
example/index.ts
Normal file
36
example/index.ts
Normal 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);
|
|
@ -8,7 +8,7 @@
|
||||||
"lib/"
|
"lib/"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "cd test && ts-node index.ts",
|
"test": "cd example && ts-node index.ts",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"format": "prettier --write \"src/**/*.ts\"",
|
"format": "prettier --write \"src/**/*.ts\"",
|
||||||
"lint": "tslint -p tsconfig.json",
|
"lint": "tslint -p tsconfig.json",
|
||||||
|
|
|
@ -41,14 +41,14 @@ class DiscordPlayer extends EventEmitter<PlayerEvents> {
|
||||||
// @todo: add extractors
|
// @todo: add extractors
|
||||||
const qt = QueryResolver.resolve(query);
|
const qt = QueryResolver.resolve(query);
|
||||||
switch (qt) {
|
switch (qt) {
|
||||||
case QueryType.YOUTUBE: {
|
case QueryType.YOUTUBE_SEARCH: {
|
||||||
const videos = await YouTube.search(qt, {
|
const videos = await YouTube.search(query, {
|
||||||
type: "video"
|
type: "video"
|
||||||
});
|
});
|
||||||
|
|
||||||
return videos.map(
|
return videos.map((m) => {
|
||||||
(m) =>
|
(m as any).source = "youtube";
|
||||||
new Track(this, {
|
return new Track(this, {
|
||||||
title: m.title,
|
title: m.title,
|
||||||
description: m.description,
|
description: m.description,
|
||||||
author: m.channel?.name,
|
author: m.channel?.name,
|
||||||
|
@ -60,8 +60,10 @@ class DiscordPlayer extends EventEmitter<PlayerEvents> {
|
||||||
duration: m.durationFormatted,
|
duration: m.durationFormatted,
|
||||||
raw: m
|
raw: m
|
||||||
})
|
})
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { User } from "discord.js";
|
import { User, Util } from "discord.js";
|
||||||
import { Player } from "../Player";
|
import { Player } from "../Player";
|
||||||
import { RawTrackData } from "../types/types";
|
import { RawTrackData } from "../types/types";
|
||||||
import { Queue } from "./Queue";
|
import { Queue } from "./Queue";
|
||||||
|
@ -14,7 +14,7 @@ class Track {
|
||||||
public views!: number;
|
public views!: number;
|
||||||
public requestedBy!: User;
|
public requestedBy!: User;
|
||||||
public fromPlaylist!: boolean;
|
public fromPlaylist!: boolean;
|
||||||
public raw!: RawTrackData;
|
public readonly raw!: RawTrackData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Track constructor
|
* Track constructor
|
||||||
|
@ -94,7 +94,7 @@ class Track {
|
||||||
}
|
}
|
||||||
|
|
||||||
private _patch(data: RawTrackData) {
|
private _patch(data: RawTrackData) {
|
||||||
this.title = data.title ?? "";
|
this.title = Util.escapeMarkdown(data.title ?? "");
|
||||||
this.author = data.author ?? "";
|
this.author = data.author ?? "";
|
||||||
this.url = data.url ?? "";
|
this.url = data.url ?? "";
|
||||||
this.thumbnail = data.thumbnail ?? "";
|
this.thumbnail = data.thumbnail ?? "";
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
export { AudioFilters } from "./utils/AudioFilters";
|
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 { PlayerError } from "./utils/PlayerError";
|
||||||
export { VoiceUtils } from "./VoiceInterface/VoiceUtils";
|
export { VoiceUtils } from "./VoiceInterface/VoiceUtils";
|
||||||
export { VoiceEvents, StreamDispatcher } from "./VoiceInterface/BasicStreamDispatcher";
|
export { VoiceEvents, StreamDispatcher } from "./VoiceInterface/BasicStreamDispatcher";
|
||||||
|
|
Loading…
Reference in a new issue