Skip to content

Commit

Permalink
improve lavalink reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
Blocksnmore committed Nov 22, 2024
1 parent bd4f598 commit e11dfcb
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 34 deletions.
8 changes: 4 additions & 4 deletions commands/music/forceskip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, CommandContext, Embed } from "harmony";
import { doPermCheck, queues } from "queue";
import { PlayerLoop } from "lavadeno";

export default class ForceSkip extends Command {
override name = "forceskip";
Expand Down Expand Up @@ -43,15 +44,14 @@ export default class ForceSkip extends Command {
const queue = queues.get(ctx.guild!.id)!;
if (
(await doPermCheck(ctx.member!, botState.channel!)) ||
queue.player.current.requestedBy == ctx.author.id
queue.player.current!.requestedBy as unknown as string == ctx.author.id
) {
if (queue.player.queue.size == 0) {
queue.deleteQueue();
} else {
const currentLoopState = queue.player.loop;
queue.player.setLoop("off");
// Skip for some reason ends the queue - This is a super jank workaround
queue.player.seek(queue.player.current.duration);
queue.player.setLoop(PlayerLoop.OFF);
await queue.player.skip();
queue.player.setLoop(currentLoopState);
}

Expand Down
7 changes: 4 additions & 3 deletions commands/music/loop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, CommandContext, Embed } from "harmony";
import { doPermCheck, queues } from "queue";
import { PlayerLoop } from "lavadeno";

export default class Loop extends Command {
override name = "loop";
Expand Down Expand Up @@ -43,12 +44,12 @@ export default class Loop extends Command {
const queue = queues.get(ctx.guild!.id)!;
if (await doPermCheck(ctx.member!, botState.channel)) {
const previousLoopType = queue.player.loop.toString();
const isLoopDisabled = queue.player.loop != "track";
const isLoopDisabled = queue.player.loop != PlayerLoop.TRACK;

if (isLoopDisabled) {
queue.player.setLoop("track");
queue.player.setLoop(PlayerLoop.TRACK);
} else {
queue.player.setLoop("off");
queue.player.setLoop(PlayerLoop.OFF);
}

await ctx.message.reply({
Expand Down
8 changes: 4 additions & 4 deletions commands/music/play.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
isMessageComponentInteraction,
} from "harmony";
import { doPermCheck, nodes, queues, ServerQueue } from "queue";
import { Track } from "lavadeno";
import { LilyTrack, Source } from "lavadeno";
import { emoji } from "emoji";
import { shuffleArray } from "tools";
import { getEmote } from "i18n";
Expand Down Expand Up @@ -86,7 +86,7 @@ export default class Play extends Command {

const { loadType, tracks } = await nodes.search({
query: ctx.argString,
source: isLink ? undefined : "youtube",
source: isLink ? undefined : Source.YOUTUBE,
requester: ctx.author.id,
});

Expand All @@ -105,7 +105,7 @@ export default class Play extends Command {
],
});
} else {
let songsToAdd: Track[] = [];
let songsToAdd: LilyTrack[] = [];

if (isLink) {
switch (loadType) {
Expand All @@ -118,7 +118,7 @@ export default class Play extends Command {
.split(" ")[0],
)
) {
const tracks: Track[] = [];
const tracks: LilyTrack[] = [];

for (const track of tracks) {
songsToAdd.push(track);
Expand Down
4 changes: 2 additions & 2 deletions commands/music/queue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export default class Queue extends Command {
emoji("nine"),
];
const queueEntries = [
queue.player.current,
...queue.player.queue.tracks,
queue.player.current!,
...queue.player.queue.values(),
];

await ctx.message.reply(undefined, {
Expand Down
7 changes: 4 additions & 3 deletions commands/music/queueloop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, CommandContext, Embed } from "harmony";
import { doPermCheck, queues } from "queue";
import { PlayerLoop } from "lavadeno";

export default class QueueLoop extends Command {
override name = "queueloop";
Expand Down Expand Up @@ -43,12 +44,12 @@ export default class QueueLoop extends Command {
const queue = queues.get(ctx.guild!.id)!;
if (await doPermCheck(ctx.member!, botState.channel)) {
const previousLoopType = queue.player.loop.toString();
const isLoopDisabled = queue.player.loop != "queue";
const isLoopDisabled = queue.player.loop != PlayerLoop.QUEUE;

if (isLoopDisabled) {
queue.player.setLoop("queue");
queue.player.setLoop(PlayerLoop.QUEUE);
} else {
queue.player.setLoop("off");
queue.player.setLoop(PlayerLoop.OFF);
}

await ctx.message.reply({
Expand Down
2 changes: 1 addition & 1 deletion commands/music/seek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class Seek extends Command {
});
} else {
queue.player.seek(position);
queue.player.current.position = position;
queue.player.current!.position = position;

if (queue.queueMessage != undefined) {
queue.queueMessage.edit(queue.nowPlayingMessage);
Expand Down
7 changes: 4 additions & 3 deletions commands/music/skip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, CommandContext, Embed } from "harmony";
import { doPermCheck, queues } from "queue";
import { PlayerLoop } from "lavadeno";

export default class Skip extends Command {
override name = "skip";
Expand Down Expand Up @@ -53,9 +54,9 @@ export default class Skip extends Command {
queue.deleteQueue();
} else {
const currentLoopState = queue.player.loop;
queue.player.setLoop("off");
queue.player.setLoop(PlayerLoop.OFF);
// Skip for some reason ends the queue - This is a super jank workaround
queue.player.seek(queue.player.current.duration);
await queue.player.skip();
queue.player.setLoop(currentLoopState);
}

Expand Down Expand Up @@ -102,7 +103,7 @@ export default class Skip extends Command {
ctx.member!,
botState.channel,
)) ||
queue.player.current.requestedBy ==
queue.player.current!.requestedBy as unknown as string ==
ctx.author.id
? "Use forceskip to skip without a vote"
: "",
Expand Down
2 changes: 1 addition & 1 deletion imports/lavadeno.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "npm:moonlink.js@4.0.2";
export * from "npm:lilylink@1.0.0";

// Non exported types

Expand Down
25 changes: 12 additions & 13 deletions imports/queue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
VoiceChannel,
type VoiceState,
} from "./harmony.ts";
import { Manager, Player, type Track, type TTrackEndType } from "./lavadeno.ts";
import { type LilyTrack, LilyManager, LilyPlayer } from "./lavadeno.ts";
import { formatMs } from "./tools.ts";
import { emoji } from "./emoji.ts";
import { getConfig } from "./settings.ts";
Expand All @@ -29,7 +29,7 @@ if (isNaN(nodesCount)) {
throw new Error("Invalid node count");
}

export const nodes = new Manager({
export const nodes = new LilyManager({
nodes: new Array(nodesCount).fill(undefined).map((_, i) => ({
host: Deno.env.get(`LAVALINK_${i + 1}_HOST`)!,
port: parseInt(Deno.env.get(`LAVALINK_${i + 1}_PORT`)!),
Expand All @@ -40,7 +40,6 @@ export const nodes = new Manager({
})),
options: {
clientName: "Bidome/1.0.0",
NodeLinkFeatures: true,
},
sendPayload: async (guildID: string, payload: unknown) => {
const guild = await client.guilds.resolve(guildID);
Expand All @@ -54,13 +53,13 @@ export const nodes = new Manager({
export const playerEventHandlers = new Map<
string,
{
trackStart: (track: Track) => Promise<void> | void;
trackStart: (track: LilyTrack) => Promise<void> | void;
playerMoved: (
oldChannel: string,
newChannel: string,
) => Promise<void> | void;
playerDisconnected: () => Promise<void> | void;
trackEnd: (track: Track, reason: TTrackEndType) => Promise<void> | void;
trackEnd: (track: LilyTrack, reason: string) => Promise<void> | void;
}
>();

Expand Down Expand Up @@ -117,7 +116,7 @@ export const doPermCheck = async (user: Member, channel: VoiceChannel) => {
};

export class ServerQueue {
public readonly player: Player;
public readonly player: LilyPlayer;
public readonly guildId: string;
public voteSkipUsers: string[] = [];
public volume = 100;
Expand All @@ -144,7 +143,7 @@ export class ServerQueue {
textChannelId: channel,
// autoPlay: true,
// autoLeave: true,
});
})!;

this.player.connect({
setDeaf: true,
Expand Down Expand Up @@ -229,7 +228,7 @@ export class ServerQueue {
].patch({ channel_id: this.channel, suppress: false });
}

public addSongs(songs: Track | Track[]) {
public addSongs(songs: LilyTrack | LilyTrack[]) {
if (Array.isArray(songs)) {
for (const song of songs) {
this.player.queue.add(song);
Expand Down Expand Up @@ -273,8 +272,8 @@ export class ServerQueue {
for (
const { duration } of [
this.player.current,
...this.player.queue.tracks,
]
...this.player.queue.values(),
].filter((song) => song != undefined)
) {
queueLength += duration;
}
Expand Down Expand Up @@ -335,9 +334,9 @@ export class ServerQueue {
name: "Progress",
value: `${
formatMs(
(this.player.current.position ?? 0) < 1000
(this.player.current!.position ?? 0) < 1000
? 1000
: this.player.current.position!,
: this.player.current!.position!,
)
}/${formatMs(song.duration)}`,
inline: true,
Expand Down Expand Up @@ -431,7 +430,7 @@ export const initLava = (bot: CommandClient) => {
return;
}
// The full payload is simulated because harmony strips it for some reason
nodes.packetUpdate({ t: evt, d: payload });
nodes.packetUpdate({ t: evt as "VOICE_STATE_UPDATE" | "VOICE_SERVER_UPDATE", d: payload });
});

nodes.init(bot.user!.id);
Expand Down

0 comments on commit e11dfcb

Please sign in to comment.