music-start-pro
Version:
Music Start Pro is a discord bot that can play YouTube music by slash command.
224 lines (223 loc) • 8.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Queue = void 0;
var language_json_1 = require("./language.json");
var discord_js_1 = require("discord.js");
var entriesOfOnePage = 30;
var Queue = /** @class */ (function () {
function Queue() {
this._list = [];
this._searchResult = [];
this._index = 0;
this._searchQuery = null;
}
Object.defineProperty(Queue.prototype, "list", {
get: function () {
return this._list;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Queue.prototype, "len", {
get: function () {
return this._list.length;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Queue.prototype, "current", {
// return the current music info
get: function () {
return this._list[this._index];
},
enumerable: false,
configurable: true
});
Object.defineProperty(Queue.prototype, "page", {
// return current page
get: function () {
return Math.floor(this._index / entriesOfOnePage);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Queue.prototype, "pages", {
// return the number of pages
get: function () {
return Math.ceil(this.len / entriesOfOnePage);
},
enumerable: false,
configurable: true
});
Queue.prototype._genericIndex = function (index) {
if (this.len === 0)
return 0;
index = index % this.len;
return (index < 0) ? index + this.len : index;
};
Queue.prototype.genericPage = function (page) {
if (this.pages === 0)
return 0;
page = page % this.pages;
return (page < 0) ? page + this.pages : page;
};
Queue.prototype.isEmpty = function () {
return this.len === 0;
};
Queue.prototype.add = function (info) {
this._list.push(info);
};
Queue.prototype.next = function (num) {
return this.jump(this._index + num);
};
// @param index can be any integer.
Queue.prototype.jump = function (index) {
if (this.isEmpty())
return;
this._index = this._genericIndex(index);
};
// @param index can be any integer.
// @return true if success, vice versa.
Queue.prototype.remove = function (index, isNowPlaying) {
index = this._genericIndex(index);
if (index == this._index && isNowPlaying)
return false;
if (index <= this._index && this._index > 0) {
this._index--;
}
this._list.splice(index, 1);
return true;
};
Queue.prototype.swap = function (index1, index2) {
index1 = this._genericIndex(index1);
index2 = this._genericIndex(index2);
if (index1 == index2)
return;
if (index1 == this._index)
this._index = index2;
if (index2 == this._index)
this._index = index1;
var tmp = this._list[index1];
this._list[index1] = this._list[index2];
this._list[index2] = tmp;
};
Queue.prototype.removeDuplicate = function () {
var set = new Set(); // set of urls
var newList = new Array();
var newIndex = 0;
for (var i = 0; i < this.len; i++) {
if (!set.has(this._list[i].url)) {
newList.push(this._list[i]);
set.add(this._list[i].url);
if (this._list[this._index].url == this._list[i].url) {
newIndex = i;
}
}
}
this._list = newList;
this._index = newIndex;
};
Queue.prototype.search = function (query) {
this._searchResult.length = 0;
if (query !== null) {
this._searchQuery = query;
}
else {
query = this._searchQuery;
}
if (query === null)
return;
for (var i = 0; i < this.len; i++) {
if (this._list[i].title.match(query)) {
this._searchResult.push(i);
}
}
};
Queue.prototype.removeAll = function () {
this._list = new Array();
this._index = 0;
};
// O(nLgN + n)
Queue.prototype.sort = function () {
var currentURL = this._list[this._index].url;
this._list.sort(function (a, b) {
return a.title.localeCompare(b.title);
});
// fix current index after sorting
for (var i = 0; i < this.len; i++) {
if (this._list[i].url == currentURL) {
this._index = i;
break;
}
}
};
// O(2n)
Queue.prototype.shuffle = function () {
var currentURL = this._list[this._index].url;
// Knuth shuffle algorithm
for (var i = 0; i < this.len; i++) {
var j = ~~(Math.random() * i);
// swap i and j
var tmp = this._list[i];
this._list[i] = this._list[j];
this._list[j] = tmp;
}
// fix current index after shuffling
for (var i = 0; i < this.len; i++) {
if (this._list[i].url == currentURL) {
this._index = i;
break;
}
}
};
Queue.prototype.showListByPage = function (lang, page, isShowSearchList) {
if (isShowSearchList === void 0) { isShowSearchList = false; }
var content = '```yaml\n';
var pages = isShowSearchList ? Math.ceil(this._searchResult.length / entriesOfOnePage) : this.pages;
// when showing the current page, increasing one for the number of current page.
content += "page:\t".concat(page + 1, "/").concat(Math.max(pages, 1), "\n");
if (this.isEmpty() || (isShowSearchList && this._searchResult.length === 0)) {
content += language_json_1.messages.playlist_is_empty[lang];
}
var len = isShowSearchList ? this._searchResult.length : this.len;
for (var i = page * entriesOfOnePage; i < Math.min((page + 1) * entriesOfOnePage, len); i++) {
var j = isShowSearchList ? this._searchResult[i] : i;
if (j == this._index) {
content += '>' + "".concat(j).padStart(3, ' ') + ":\t".concat(this._list[j].title, "\n");
}
else {
content += "".concat(j).padStart(4, ' ') + ":\t".concat(this._list[j].title, "\n");
}
}
return content + '\n```';
};
Queue.prototype.showList = function (lang, page, isShowSearchList) {
if (isShowSearchList === void 0) { isShowSearchList = false; }
page = page !== null && page !== void 0 ? page : Math.floor(this._index / entriesOfOnePage);
var btnNext = new discord_js_1.ButtonBuilder()
.setCustomId(!isShowSearchList ? "next-".concat(page) : "nextSearch-".concat(page))
.setLabel('Next')
.setStyle(discord_js_1.ButtonStyle.Secondary);
var btnPre = new discord_js_1.ButtonBuilder()
.setCustomId(!isShowSearchList ? "pre-".concat(page) : "preSearch-".concat(page))
.setLabel('Previous')
.setStyle(discord_js_1.ButtonStyle.Secondary);
var btnRefresh = new discord_js_1.ButtonBuilder()
.setCustomId(!isShowSearchList ? 'refresh' : 'refreshSearch')
.setLabel('Refresh')
.setStyle(discord_js_1.ButtonStyle.Secondary);
return {
content: this.showListByPage(lang, page, isShowSearchList),
components: [new discord_js_1.ActionRowBuilder()
.addComponents(btnPre)
.addComponents(btnRefresh)
.addComponents(btnNext)
]
};
};
Queue.prototype.toList = function () {
return this._list;
};
return Queue;
}());
exports.Queue = Queue;