Music Library
In Seyfert, we can play music using an external library called kazagumo.
Installation
First of all, we have to install kazagumo
and shoukaku
. Both dependencies are extremely necessary to move forward within this guide.
pnpm add kazagumo shoukaku
Setup
For further information about the different options and events of kazagumo see the official bot example
1import { Client } from "seyfert";2import { Kazagumo } from "kazagumo";3import { type NodeOption, Connectors } from "shoukaku";4
5const client = new Client();6const nodes: NodeOption[] = [7 {8 name: "Node",9 url: "localhost:2333",10 auth: "youshallnotpass",11 secure: false12 }13];14
15//Basic configuration, perfect for this case16client.kazagumo = new Kazagumo(17 {18 defaultSearchEngine: "youtube",19 send: (guildId, payload) =>20 client.gateway.send(client.gateway.calculeShardId(guildId), payload)21 },22 new Connectors.Seyfert(client),23 nodes24);25
26//To see if the node is connected27client.kazagumo.shoukaku.on("ready", (name) =>28 console.log(`Lavalink ${name}: Ready!`)29);30
31declare module "seyfert" {32 interface Client {33 kazagumo: Kazagumo;34 }35}36
37client.start().then(() => client.uploadCommands());
1import {2 Command,3 Declare,4 Options,5 type CommandContext,6 createStringOption7} from "seyfert";8import { MessageFlags } from "seyfert/lib/types";9
10const options = {11 query: createStringOption({12 description: "Enter a song name or url.",13 required: true14 })15};16
17@Declare({18 name: "play",19 description: "Play music."20})21@Options(options)22export default class PlayCommand extends Command {23 async run(ctx: CommandContext<typeof options>) {24 const { options, client, guildId, channelId, member, author } = ctx;25 const { query } = options;26
27 if (!guildId || !member) return;28
29 const voice = member.voice();30 if (!voice)31 return ctx.write({32 content: "You need to be in a voice channel to play music.",33 flags: MessageFlags.Ephemeral34 });35
36 const botVoice = ctx.me()?.voice();37 if (botVoice && botVoice.channelId !== voice.channelId)38 return ctx.write({39 content: "You need to be in the same voice channel as me.",40 flags: MessageFlags.Ephemeral41 });42
43 const player = await client.kazagumo.createPlayer({44 guildId: guildId,45 textId: channelId,46 voiceId: voice.channelId,47 volume: 10048 });49
50 const result = await client.kazagumo.search(query, { requester: author });51 if (!result.tracks.length)52 return ctx.write({ content: "No results found!" });53
54 if (result.type === "PLAYLIST") player.queue.add(result.tracks);55 else player.queue.add(result.tracks[0]);56
57 if (!player.playing && !player.paused) player.play();58 return ctx.write({59 content:60 result.type === "PLAYLIST"61 ? `Queued ${result.tracks.length} from ${result.playlistName}`62 : `Queued ${result.tracks[0].title}`63 });64 }65}