Skip to content

Commit ee5cddb

Browse files
committed
message functions test
1 parent 7477c86 commit ee5cddb

19 files changed

Lines changed: 563 additions & 17 deletions

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"license": "GPL-3.0-only",
99
"private": false,
1010
"_moduleAliases": {
11-
"@lib": "dist/lib"
11+
"@lib": "src/lib/index.ts"
1212
},
1313
"scripts": {
1414
"build": "yarn clean && tsc",
@@ -18,15 +18,20 @@
1818
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts .",
1919
"prettier": "prettier .",
2020
"format": "prettier-eslint --eslint-config-path src/../.eslintrc.js --config src/../.prettierrc \"src/**/*.ts\"",
21-
"check-tsc": "tsc --noEmit"
21+
"check-tsc": "tsc --noEmit",
22+
"webstorm": "ts-node ./src/index.ts"
2223
},
2324
"dependencies": {
25+
"@ef-carbon/tspm": "^2.2.5",
2426
"@lavaclient/queue": "^2.0.4",
2527
"@lavaclient/spotify": "^3.1.0",
2628
"discord.js": "^14.3.0",
2729
"dotenv": "^10.0.0",
2830
"lavaclient": "^4.0.4",
29-
"module-alias": "^2.2.2"
31+
"module-alias": "^2.2.2",
32+
"ramda": "^0.28.0",
33+
"tsconfig-paths": "^4.1.2",
34+
"zod": "^3.20.6"
3035
},
3136
"devDependencies": {
3237
"@types/node": "^16.10.2",

src/functions2/echo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { CommonParams2, type EchoParams } from './types'
2+
3+
export async function Echo({ send, msg }: EchoParams): Promise<void> {
4+
await send(msg)
5+
}

src/functions2/join.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { type CommonParams2Validated } from './types'
2+
import { TextChannel, type VoiceBasedChannel, VoiceChannel } from 'discord.js'
3+
import { type Node, type Player } from 'lavaclient'
4+
import { type Bot, type MessageChannel } from '@lib'
5+
6+
export const createPlayer = async ({
7+
userTextChannel,
8+
userVc,
9+
bot
10+
}: {
11+
userTextChannel: MessageChannel
12+
userVc: VoiceBasedChannel
13+
bot: Bot
14+
}): Promise<Player<Node>> => {
15+
const player = bot.music.createPlayer(userVc.guild.id)
16+
player.queue.channel = userTextChannel
17+
player.connect(userVc.id)
18+
return player
19+
}
20+
21+
export async function Join({ userVc, bot, userTextChannel, send }: CommonParams2Validated): Promise<void> {
22+
await createPlayer({ userVc, bot, userTextChannel })
23+
await send(`Joined ${userVc.toString()}`)
24+
}

src/functions2/leave.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { type CommonParams2Validated } from './types'
2+
3+
export async function Leave({ bot, player, send }: CommonParams2Validated): Promise<void> {
4+
await send(`Left <#${player.channelId}>`)
5+
player.disconnect()
6+
await bot.music.destroyPlayer(player.guildId)
7+
}

src/functions2/nightcore.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { CommonParams2Validated, type NightcoreParams } from './types'
2+
3+
export async function Nightcore({
4+
send,
5+
speed: speedParam,
6+
pitch: pitchParam,
7+
rate: rateParam,
8+
player
9+
}: NightcoreParams): Promise<void> {
10+
const shouldEnable = !player.nightcore || speedParam != null || pitchParam != null || rateParam != null
11+
if (!shouldEnable) {
12+
await send("Nightcoren't")
13+
player.nightcore = false
14+
player.filters.timescale = undefined
15+
} else {
16+
player.nightcore = true
17+
const speed = speedParam ?? 1.125
18+
const pitch = pitchParam ?? 1.125
19+
const rate = rateParam ?? 1
20+
await send(`Nightcore enabled with speed ${speed}, pitch ${pitch}, and rate ${rate}`)
21+
player.filters.timescale = { speed, pitch, rate }
22+
}
23+
24+
await player.setFilters()
25+
}

src/functions2/pause.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { millisecondsToString } from '@lib'
2+
import { type CommonParams2Validated } from './types'
3+
4+
export async function Pause({ sendIfError, send, player }: CommonParams2Validated): Promise<void> {
5+
const current = player.queue.current
6+
if (current == null) {
7+
await sendIfError("I'm not playing anything bozo")
8+
return
9+
}
10+
await player.pause(true)
11+
const positionHumanReadable = millisecondsToString(player.position ?? 0)
12+
await send(`Paused ${current.title} at ${positionHumanReadable}`)
13+
}

src/functions2/ping.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { type CommonParams2 } from './types'
2+
3+
export async function Ping2({ send, bot }: CommonParams2): Promise<void> {
4+
await send(`Pong! **Heartbeat:** *${Math.round(bot.ws.ping)}ms*`)
5+
}

src/functions2/play.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { SpotifyItemType } from '@lavaclient/spotify'
2+
3+
import type { Addable } from '@lavaclient/queue'
4+
import { type PlayParams } from './types'
5+
6+
export const Play =
7+
({ next }: { next: boolean }) =>
8+
async ({
9+
userVc,
10+
send,
11+
sendIfError,
12+
userTextChannel,
13+
bot,
14+
query,
15+
guild,
16+
player,
17+
requesterId
18+
}: PlayParams): Promise<void> => {
19+
let tracks: Addable[] = []
20+
let msg = ''
21+
if (query !== '') {
22+
if (bot.music.spotify.isSpotifyUrl(query)) {
23+
const item = await bot.music.spotify.load(query)
24+
switch (item?.type) {
25+
case SpotifyItemType.Track: {
26+
const track = await item.resolveYoutubeTrack()
27+
tracks = [track]
28+
msg = `Queued track [**${item.name}**](${query}).`
29+
break
30+
}
31+
case SpotifyItemType.Artist: {
32+
tracks = await item.resolveYoutubeTracks()
33+
msg = `Queued the **Top ${tracks.length} tracks** for [**${item.name}**](${query}).`
34+
break
35+
}
36+
case SpotifyItemType.Album:
37+
case SpotifyItemType.Playlist: {
38+
tracks = await item.resolveYoutubeTracks()
39+
msg = `Queued **${tracks.length} tracks** from ${SpotifyItemType[item.type].toLowerCase()} [**${
40+
item.name
41+
}**](${query}).`
42+
break
43+
}
44+
default: {
45+
await sendIfError("Sorry, couldn't find anything :/")
46+
return
47+
}
48+
}
49+
} else {
50+
const results = await bot.music.rest.loadTracks(/^https?:\/\//.test(query) ? query : `ytsearch:${query}`)
51+
52+
switch (results.loadType) {
53+
case 'LOAD_FAILED':
54+
case 'NO_MATCHES': {
55+
await sendIfError('uh oh something went wrong')
56+
return
57+
}
58+
case 'PLAYLIST_LOADED': {
59+
tracks = results.tracks
60+
msg = `Queued playlist [**${results.playlistInfo.name}**](${query}), it has a total of **${tracks.length}** tracks.`
61+
break
62+
}
63+
case 'TRACK_LOADED':
64+
case 'SEARCH_RESULT': {
65+
const [track] = results.tracks
66+
tracks = [track]
67+
msg = `Queued [**${track.info.title}**](${track.info.uri})`
68+
break
69+
}
70+
}
71+
}
72+
}
73+
/* create a player and/or join the member's userVc. */
74+
if (!player?.connected) {
75+
player ??= bot.music.createPlayer(guild.id)
76+
player.queue.channel = userTextChannel
77+
player.connect(userVc.id, { deafened: true })
78+
}
79+
80+
/* reply with the queued message. */
81+
const started = player.playing || player.paused
82+
if (msg !== '') {
83+
// TODO: make it better (this checks if play was used to unpause)
84+
await send(msg, next != null ? 'At the top of the queue.' : '', started)
85+
player.queue.add(tracks, { requester: requesterId, next })
86+
} else {
87+
await send('Resumed playback')
88+
await player.pause(false)
89+
}
90+
/* do queue tings. */
91+
if (!started) {
92+
await player.queue.start()
93+
}
94+
}

src/functions2/queue.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { type CommonParams2Validated } from './types'
2+
3+
const formatIndex = (index: number, size: number): string =>
4+
(index + 1).toString().padStart(size.toString().length, '0')
5+
6+
export async function Queue({ player, send, guild }: CommonParams2Validated): Promise<void> {
7+
const size = player.queue.tracks.length
8+
const str = player.queue.tracks
9+
.map(
10+
(t, idx) =>
11+
`\`#${formatIndex(idx, size)}\` [**${t.title}**](${t.uri}) ${
12+
t.requester !== undefined ? `<@${t.requester}>` : ''
13+
}`
14+
)
15+
.join('\n')
16+
17+
await send(`Queue for **${guild.name}**`, str)
18+
}

src/functions2/remove.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { type RemoveParams } from './types'
2+
3+
export async function Remove({ player, sendIfError, send, index }: RemoveParams): Promise<void> {
4+
const removedTrack = player.queue.remove(index - 1)
5+
if (removedTrack == null) {
6+
await sendIfError('No tracks were removed.')
7+
return
8+
}
9+
10+
await send(`The track [**${removedTrack.title}**](${removedTrack.uri}) was removed.`)
11+
}

0 commit comments

Comments
 (0)