Skip to content

Commit

Permalink
4.0-dev-4
Browse files Browse the repository at this point in the history
Audioplayer now loads from handler.
Fixed requester display in audioplayer messages.
Fixed command /playing.
  • Loading branch information
AlexInCube committed Nov 13, 2024
1 parent da14c5f commit 3ade320
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 179 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aicbot",
"version": "4.0.0-dev-3",
"version": "4.0.0-dev-4",
"description": "Discord Bot for playing music",
"main": "build/main.js",
"scripts": {
Expand Down Expand Up @@ -31,7 +31,7 @@
"node-os-utils": "1.3.7",
"opusscript": "0.1.1",
"prism-media": "1.3.5",
"riffy": "1.0.7-rc.2",
"riffy": "https://pkg.pr.new/riffy-team/riffy@bcaaf04",
"sodium-native": "4.2.1",
"uuid": "10.0.0",
"zod": "3.23.8"
Expand Down
79 changes: 40 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 51 additions & 21 deletions src/audioplayer/AudioPlayersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import { generateLyricsEmbed } from './Lyrics.js';
import { getGuildOptionLeaveOnEmpty, setGuildOptionLeaveOnEmpty } from '../schemas/SchemaGuild.js';
import { addSongToGuildSongsHistory } from '../schemas/SchemaSongsHistory.js';

Check warning on line 22 in src/audioplayer/AudioPlayersManager.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'addSongToGuildSongsHistory' is defined but never used
import { PaginationList } from './PaginationList.js';
import { nodeResponse, Player, Queue, Riffy, Track } from 'riffy';
import { Node, nodeResponse, Player, Queue, Riffy, RiffyEventType, Track } from 'riffy';

Check warning on line 24 in src/audioplayer/AudioPlayersManager.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'Node' is defined but never used
import { LavaNodes } from '../LavalinkNodes.js';
import { clamp } from '../utilities/clamp.js';
import { formatMilliseconds } from '../utilities/formatMillisecondsToTime.js';
import { isValidURL } from '../utilities/isValidURL.js';

Check warning on line 28 in src/audioplayer/AudioPlayersManager.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'isValidURL' is defined but never used
import * as process from 'node:process';
import * as util from 'node:util';

Check warning on line 30 in src/audioplayer/AudioPlayersManager.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'util' is defined but never used

export const loggerPrefixAudioplayer = `Audioplayer`;

Expand All @@ -42,7 +45,8 @@ export class AudioPlayersManager {
if (guild) guild.shard.send(payload);
},
defaultSearchPlatform: 'ytmsearch',
restVersion: 'v4'
restVersion: 'v4',
multipleTrackHistory: true
});

this.setupEvents();
Expand All @@ -51,7 +55,7 @@ export class AudioPlayersManager {
async play(
voiceChannel: VoiceBasedChannel,
textChannel: TextChannel,
query: string,
query: string | nodeResponse,
member: GuildMember
): Promise<void> {
try {
Expand All @@ -64,13 +68,14 @@ export class AudioPlayersManager {

const player = this.playersManager.get(textChannel.guild.id);

const resolve: nodeResponse = await this.riffy.resolve({ query, requester: member.id });
const resolve: nodeResponse | undefined =
typeof query === 'string' ? await this.resolve(query, member.id) : query;
if (!resolve) return;

if (resolve.loadType === 'playlist') {
if (!resolve.playlistInfo) return;

for (const track of resolve.tracks) {
track.info.requester = member;
riffyPlayer.queue.add(track);
}

Expand Down Expand Up @@ -105,7 +110,6 @@ export class AudioPlayersManager {
} else if (resolve.loadType === 'search' || resolve.loadType === 'track') {
const track = resolve.tracks.shift();
if (!track) return;
track.info.requester = member;

if (ENV.BOT_MAX_SONGS_HISTORY_SIZE > 0) {
//await addSongToGuildSongsHistory(textChannel.guild.id, track);
Expand Down Expand Up @@ -134,6 +138,24 @@ export class AudioPlayersManager {
}
}

async resolve(query: string, memberId: string): Promise<nodeResponse | undefined> {
const resolve: nodeResponse = await this.riffy.resolve({ query, requester: memberId });

if (resolve.loadType === 'playlist') {
if (!resolve.playlistInfo) return undefined;

for (const track of resolve.tracks) {
track.info.requester = memberId;
}
} else if (resolve.loadType === 'search' || resolve.loadType === 'track') {
const track = resolve.tracks[0];
if (!track) return undefined;
track.info.requester = memberId;
}

return resolve;
}

async stop(guildId: string): Promise<void> {
const riffyPlayer = this.riffy.players.get(guildId);
if (!riffyPlayer) return;
Expand Down Expand Up @@ -241,14 +263,16 @@ export class AudioPlayersManager {

// TODO: Implement Jump in audioplayer
async jump(guild: Guild, position: number): Promise<Track | undefined> {
const riffyPlayer = this.riffy.players.get(guild.id);
if (!riffyPlayer) return;

try {
const queue = riffyPlayer.queue;
if (queue) {
//return riffyPlayer.seek() jump(guild, clamp(position, 1, queue.songs.length));
}
const riffyPlayer = this.riffy.players.get(guild.id);
if (!riffyPlayer) return;
const jumpTrack = riffyPlayer.queue.at(position);
riffyPlayer.queue.splice(
0 /* At Position */,
clamp(position, 1, riffyPlayer.queue.length - 1) /* Tracks to jump */
);
riffyPlayer.stop();
return jumpTrack;
} catch (e) {
if (ENV.BOT_VERBOSE_LOGGING) loggerError(e);
}
Expand Down Expand Up @@ -351,21 +375,27 @@ export class AudioPlayersManager {
}

private setupEvents() {
this.riffy.on('nodeConnect', async (node) => {
this.riffy.on(RiffyEventType.NodeConnect, async (node) => {
loggerSend(`Node ${node.name} has connected.`, loggerPrefixAudioplayer);
});

this.riffy.on('nodeError', async (node, error) => {
loggerSend(`Node ${node.name} encountered an error: ${error.message}`, loggerPrefixAudioplayer);
this.riffy.on(RiffyEventType.NodeError, async (node, error) => {
// @ts-expect-error When Lavalink node found the error, we have field "code" in class "error"
if (error.code === 'ECONNREFUSED') {
loggerSend(`Node ${node.name} failed to connect: ${error.message}`, loggerPrefixAudioplayer);
process.exit(1);
} else {
loggerSend(`Node ${node.name} encountered an error: ${error.message}`, loggerPrefixAudioplayer);
}
});

if (ENV.BOT_VERBOSE_LOGGING) {
this.riffy.on('debug', async (message) => {
this.riffy.on(RiffyEventType.Debug, async (message) => {
loggerSend(`Riffy Debug: ${message}`, loggerPrefixAudioplayer);
});
}

this.riffy.on('playerCreate', async (riffyPlayer) => {
this.riffy.on(RiffyEventType.PlayerCreate, async (riffyPlayer) => {
const guildTextChannel = this.client.channels.cache.get(riffyPlayer.textChannel) as GuildTextBasedChannel;
await this.playersManager.add(riffyPlayer.guildId, guildTextChannel, this.riffy);

Expand All @@ -376,18 +406,18 @@ export class AudioPlayersManager {
await player.setLeaveOnEmpty(await getGuildOptionLeaveOnEmpty(riffyPlayer.guildId));
});

this.riffy.on('playerDisconnect', async (riffyPlayer) => {
this.riffy.on(RiffyEventType.PlayerDisconnect, async (riffyPlayer) => {
await this.playersManager.remove(riffyPlayer.guildId);
});

this.riffy.on('trackStart', async (riffyPlayer, track, payload) => {
this.riffy.on(RiffyEventType.TrackStart, async (riffyPlayer, track, payload) => {

Check warning on line 413 in src/audioplayer/AudioPlayersManager.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'track' is defined but never used
const player = this.playersManager.get(riffyPlayer.guildId);
if (player) {
await player.setState('playing');
}
});

this.riffy.on('queueEnd', async (riffyPlayer) => {
this.riffy.on(RiffyEventType.QueueEnd, async (riffyPlayer) => {
await this.playersManager.get(riffyPlayer.guildId)?.setState('waiting');
});
}
Expand Down
Loading

0 comments on commit 3ade320

Please sign in to comment.