Skip to content

Commit

Permalink
Rewrite with distube!
Browse files Browse the repository at this point in the history
  • Loading branch information
boomermath committed Oct 18, 2021
1 parent f519378 commit 9a7b0de
Show file tree
Hide file tree
Showing 31 changed files with 1,903 additions and 893 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# bop-js
A small private music bot using [discord-player](https://discord-player.js.org) and [discord.js](https://discord.js.org) v13
A small private music bot using [distube](https://distube.js.org) and [discord.js](https://discord.js.org) v13

Create a .env file with a TOKEN variable.
Then build the project with `npm run build`
Expand Down
21 changes: 19 additions & 2 deletions lib/Client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { Player } from "discord-player";
import SpotifyPlugin from "@distube/spotify";
import SoundCloudPlugin from "@distube/soundcloud";
import DisTube from "distube";
import { Client, Intents } from "discord.js";
import { CommandStore, EventStore, InhibitorStore } from "./Stores";
import BopConsole from "./util/Console";

export default class BopClient extends Client {
public prefix = "bop!";
public player: Player = new Player(this, {
public player: DisTube = new DisTube(this, {
searchSongs: 1,
searchCooldown: 5,
leaveOnEmpty: false,
leaveOnFinish: true,
leaveOnStop: true,
nsfw: true,
emitNewSongOnly: true,
ytdlOptions: {
filter: "audioonly",
},
plugins: [new SoundCloudPlugin(), new SpotifyPlugin()],
});
public commands: CommandStore = new CommandStore(this, "../src/commands");
public events: EventStore = new EventStore(this, "../src/events");
Expand All @@ -28,6 +38,13 @@ export default class BopClient extends Client {
});
}

public hasMention(message: string) {
return (
message.startsWith(`<@!${this.user?.id}>`) ||
message.startsWith(`<@${this.user?.id}>`)
);
}

public async start(): Promise<void> {
this.events.setEmitters({ client: this, player: this.player });

Expand Down
4 changes: 2 additions & 2 deletions lib/Modules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Awaited, Message } from "discord.js";
import { Message } from "discord.js";
import { sep } from "path";
import BopClient from "./Client";

Expand Down Expand Up @@ -71,7 +71,7 @@ class Event extends Module {
this.once = options.once ?? false;
}

public main(...args: readonly unknown[]): Awaited<void> {
public main(...args: readonly unknown[]): void {
throw new Error("Not implemented");
}
}
Expand Down
60 changes: 25 additions & 35 deletions lib/util/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,36 @@ import { Console } from "console";
import { inspect } from "util";

export default class BopConsole extends Console {
constructor() {
super(process.stdout, process.stderr);
}

get timestamp(): string {
return new Date().toLocaleTimeString();
}
constructor() {
super(process.stdout, process.stderr);
}

public log(message: unknown): void {
this.write(message, ["inverse"]);
}
get timestamp(): string {
return new Date().toLocaleTimeString();
}

public success(message: unknown): void {
this.write(message, ["bgGreen", "bold"]);
}
public log(message: unknown): void {
this.write(message, ["inverse"]);
}

public error(message: unknown): void {
this.write(message, ["red", "underline"]);
}
public success(message: unknown): void {
this.write(message, ["bgGreen", "bold"]);
}

public info(message: unknown): void {
this.write(message, ["bgBlue", "dim"]);
}
public error(message: unknown): void {
this.write(message, ["red", "underline"], "error");
}

private write(message: unknown, colors: string[], type?: string): void {
const flattened = this._flatten(message);
const colorsTemplate = colors.join(".");
super[type === "error" ? "error" : "log"](
chalk`{${colorsTemplate} ${flattened} | ${this.timestamp}}`
);
}
public info(message: unknown): void {
this.write(message, ["bgBlue", "dim"]);
}

private _flatten(message: unknown): string {
if (Array.isArray(message)) {
return message.every((entry) => typeof entry === "string")
? message.join("\n")
: message.map((e) => inspect(e)).join("\n");
} else if (typeof message === "object") {
return inspect(message);
private write(message: unknown, colors: string[], type?: string): void {
const colorsTemplate = colors.join(".");
super[type === "error" ? "error" : "log"](
chalk`{${colorsTemplate} ${
typeof message === "string" ? message : inspect(message)
} | ${this.timestamp}}`
);
}
return String(message);
}
}
23 changes: 9 additions & 14 deletions lib/util/Embeds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Playlist, Track } from "discord-player";
import { Playlist, Song } from "distube";
import { MessageEmbed } from "discord.js";
export const EMBED_COLOR = "BLUE";

Expand All @@ -12,28 +12,23 @@ export class Notification extends MessageEmbed {
}

export class MusicEmbed extends MessageEmbed {
constructor(music: Playlist | Track, nowplaying = false) {
const author =
music instanceof Playlist
? music.tracks[0].requestedBy
: music.requestedBy;

constructor(music: Playlist | Song, nowplaying = false) {
const description =
music instanceof Track
? `${nowplaying ? "Now playing" : "Added"} **[${music.title}](${
music instanceof Song
? `${nowplaying ? "Now playing" : "Added"} **[${music.name}](${
music.url
})** by **${music.author}** | **[${music.duration}]**`
: `Added **${music.tracks.length}** tracks from **[${music.title}](${music.url})** by **[${music.author.name}](${music.author.url})**`;
})** by **[${music.uploader.name}](${music.uploader.url})**`
: `Added **${music.songs.length}** tracks from **[${music.name}](${music.url})**`;

super({
description: description,
description: `${description} | **[${music.formattedDuration}]**`,
color: EMBED_COLOR,
thumbnail: {
url: music.thumbnail,
},
author: {
name: author.username,
icon_url: author.avatarURL()!,
name: music.user?.username,
icon_url: music.user?.avatarURL() as string,
},
});
}
Expand Down
53 changes: 23 additions & 30 deletions lib/util/Util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AudioFilters } from "discord-player";
import { MessageActionRow, MessageButton, TextChannel } from "discord.js";

export interface QueueMetadata {
Expand All @@ -7,34 +6,28 @@ export interface QueueMetadata {
}

export default class Util {
public static toTitleCase(e: string): string {
return `${e.charAt(0).toUpperCase()}${e.slice(1)}`;
}
public static toTitleCase(e: string): string {
return `${e.charAt(0).toUpperCase()}${e.slice(1)}`;
}

public static get defaultFilters(): Record<string, boolean> {
const filters: Record<string, boolean> = {};
AudioFilters.names.map((f) => (filters[f] = false));
return filters;
}

public static buildDefaultActionRow(): MessageActionRow {
return new MessageActionRow().addComponents(
new MessageButton()
.setCustomId("resume")
.setLabel("Resume")
.setStyle("SUCCESS"),
new MessageButton()
.setCustomId("pause")
.setLabel("Pause")
.setStyle("DANGER"),
new MessageButton()
.setCustomId("nowplaying")
.setLabel("Now Playing")
.setStyle("PRIMARY"),
new MessageButton()
.setCustomId("queue")
.setLabel("Queue")
.setStyle("SECONDARY")
);
}
public static buildDefaultActionRow(): MessageActionRow {
return new MessageActionRow().addComponents(
new MessageButton()
.setCustomId("resume")
.setLabel("Resume")
.setStyle("SUCCESS"),
new MessageButton()
.setCustomId("pause")
.setLabel("Pause")
.setStyle("DANGER"),
new MessageButton()
.setCustomId("nowplaying")
.setLabel("Now Playing")
.setStyle("PRIMARY"),
new MessageButton()
.setCustomId("queue")
.setLabel("Queue")
.setStyle("SECONDARY")
);
}
}
Loading

0 comments on commit 9a7b0de

Please sign in to comment.