Examples & Templates

Các ví dụ thực tế và templates để bắt đầu xây dựng Discord music bot với ZiPlayer một cách nhanh chóng và hiệu quả.

Basic Bot

Bot Discord cơ bản với chức năng phát nhạc

  • Play command
  • Error handling
  • Voice channel support

Advanced Bot

Bot với slash commands và nhiều tính năng

  • Slash commands
  • Queue management
  • Volume control
  • Shuffle

Full Featured Bot

Bot hoàn chỉnh với TTS, lyrics và UI đẹp

  • TTS notifications
  • Lyrics display
  • Rich embeds
  • Error recovery

Basic Music Bot

Bot Discord cơ bản với chức năng phát nhạc từ YouTube và SoundCloud.

1import { Client, GatewayIntentBits } from "discord.js";
2import { PlayerManager } from "ziplayer";
3import { YouTubePlugin, SoundCloudPlugin } from "@ziplayer/plugin";
4
5const client = new Client({
6  intents: [
7    GatewayIntentBits.Guilds,
8    GatewayIntentBits.GuildVoiceStates,
9    GatewayIntentBits.GuildMessages
10  ]
11});
12
13const manager = new PlayerManager({
14  plugins: [
15    new YouTubePlugin(),
16    new SoundCloudPlugin()
17  ]
18});
19
20client.on("ready", () => {
21  console.log("Bot is ready!");
22});
23
24client.on("messageCreate", async (message) => {
25  if (message.author.bot) return;
26  
27  if (message.content.startsWith("!play")) {
28    const query = message.content.slice(6);
29    const player = manager.create(message.guild.id);
30    
31    try {
32      await player.connect(message.member.voice.channel);
33      await player.play(query, message.author.id);
34      
35      message.reply(`Đang phát: ${query}`);
36    } catch (error) {
37      message.reply("Không thể phát nhạc: " + error.message);
38    }
39  }
40});
41
42client.login(process.env.DISCORD_TOKEN);

Tính năng

  • Lệnh !play để phát nhạc
  • Hỗ trợ YouTube và SoundCloud
  • Xử lý lỗi cơ bản
  • Auto-connect voice channel

Setup

  1. Cài đặt dependencies
  2. Tạo Discord bot token
  3. Thêm bot vào server
  4. Chạy bot và test

Advanced Music Bot

Bot với slash commands, queue management và nhiều tính năng nâng cao.

1import { Client, GatewayIntentBits, SlashCommandBuilder } from "discord.js";
2import { PlayerManager } from "ziplayer";
3import { YouTubePlugin, SoundCloudPlugin, SpotifyPlugin } from "@ziplayer/plugin";
4
5const client = new Client({
6  intents: [
7    GatewayIntentBits.Guilds,
8    GatewayIntentBits.GuildVoiceStates,
9    GatewayIntentBits.GuildMessages
10  ]
11});
12
13const manager = new PlayerManager({
14  plugins: [
15    new YouTubePlugin({ apiKey: process.env.YOUTUBE_API_KEY }),
16    new SoundCloudPlugin({ clientId: process.env.SOUNDCLOUD_CLIENT_ID }),
17    new SpotifyPlugin({
18      clientId: process.env.SPOTIFY_CLIENT_ID,
19      clientSecret: process.env.SPOTIFY_CLIENT_SECRET
20    })
21  ]
22});
23
24// Slash Commands
25const commands = [
26  new SlashCommandBuilder()
27    .setName("play")
28    .setDescription("Phát nhạc")
29    .addStringOption(option =>
30      option.setName("query")
31        .setDescription("Tên bài hát hoặc URL")
32        .setRequired(true)
33    ),
34  new SlashCommandBuilder()
35    .setName("skip")
36    .setDescription("Bỏ qua bài hiện tại"),
37  new SlashCommandBuilder()
38    .setName("queue")
39    .setDescription("Xem danh sách phát"),
40  new SlashCommandBuilder()
41    .setName("stop")
42    .setDescription("Dừng phát nhạc")
43];
44
45client.on("ready", async () => {
46  console.log("Bot is ready!");
47  await client.application.commands.set(commands);
48});
49
50client.on("interactionCreate", async (interaction) => {
51  if (!interaction.isChatInputCommand()) return;
52  
53  const player = manager.create(interaction.guild.id);
54  
55  switch (interaction.commandName) {
56    case "play":
57      const query = interaction.options.getString("query");
58      
59      try {
60        await player.connect(interaction.member.voice.channel);
61        await player.play(query, interaction.user.id);
62        
63        await interaction.reply(`🎵 Đang phát: ${query}`);
64      } catch (error) {
65        await interaction.reply("❌ Không thể phát nhạc: " + error.message);
66      }
67      break;
68      
69    case "skip":
70      if (player.queue.length > 0) {
71        player.queue.skip();
72        await interaction.reply("⏭️ Đã bỏ qua bài hiện tại");
73      } else {
74        await interaction.reply("❌ Không có bài nào trong queue");
75      }
76      break;
77      
78    case "queue":
79      const queue = player.queue;
80      if (queue.length === 0) {
81        await interaction.reply("📝 Queue trống");
82        return;
83      }
84      
85      const queueList = queue.map((track, index) => 
86        `${index + 1}. ${track.title}`
87      ).join("\n");
88      
89      await interaction.reply(`📝 Danh sách phát:\n${queueList}`);
90      break;
91      
92    case "stop":
93      player.stop();
94      await interaction.reply("⏹️ Đã dừng phát nhạc");
95      break;
96  }
97});
98
99client.login(process.env.DISCORD_TOKEN);

Slash Commands

  • /play - Phát nhạc
  • /skip - Bỏ qua bài
  • /queue - Xem danh sách
  • /stop - Dừng phát

Features

  • Rich embeds
  • Error handling
  • Event notifications
  • Multi-plugin support

🚀 Bắt đầu ngay!

Chọn một trong các ví dụ trên và bắt đầu xây dựng music bot của bạn.