File size: 1,923 Bytes
d018dd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const queues = new Map();
const history = new Map();
// ✅ repeatAll snapshot — full queue saved when repeat all starts
const repeatAllSnapshots = new Map();

function getQueue(guildId) {
  if (!queues.has(guildId)) queues.set(guildId, []);
  return queues.get(guildId);
}

function add(guildId, song) {
  getQueue(guildId).push(song);
}

function next(guildId) {
  const q = getQueue(guildId);
  const song = q.shift();
  if (song) {
    if (!history.has(guildId)) history.set(guildId, []);
    history.get(guildId).push(song);
    if (history.get(guildId).length > 50) history.get(guildId).shift();
  }
  return song || null;
}

function getPrev(guildId) {
  const h = history.get(guildId) || [];
  if (h.length < 2) return null;
  return h[h.length - 2];
}

function clear(guildId) {
  queues.set(guildId, []);
  repeatAllSnapshots.delete(guildId);
}

function removeAt(guildId, index) {
  const q = getQueue(guildId);
  if (index < 0 || index >= q.length) return null;
  return q.splice(index, 1)[0];
}

function removeDuplicates(guildId) {
  const q = getQueue(guildId);
  const seen = new Set();
  const deduped = [];
  let removed = 0;
  for (const song of q) {
    if (!seen.has(song.url)) {
      seen.add(song.url);
      deduped.push(song);
    } else {
      removed++;
    }
  }
  queues.set(guildId, deduped);
  return removed;
}

// ✅ Save full queue snapshot for repeat all
function saveRepeatAllSnapshot(guildId, allSongs) {
  repeatAllSnapshots.set(guildId, allSongs.map(s => ({ ...s })));
}

// ✅ Restore snapshot when queue runs out in repeat all mode
function restoreRepeatAllSnapshot(guildId) {
  const snap = repeatAllSnapshots.get(guildId);
  if (!snap || snap.length === 0) return false;
  queues.set(guildId, snap.map(s => ({ ...s })));
  return true;
}

module.exports = { getQueue, add, next, getPrev, clear, removeAt, removeDuplicates, saveRepeatAllSnapshot, restoreRepeatAllSnapshot };