Search Stations and large update

This commit is contained in:
MatteZ02
2020-03-09 13:17:47 +02:00
parent ecb23c8550
commit bdf2025558
7 changed files with 80 additions and 49 deletions

View File

@ -1,24 +1,6 @@
module.exports = async function (guild, client, station) {
module.exports = async function (guild, client, url) {
const radio = client.radio.get(guild.id);
let url = "";
if (isNaN(station)) {
radio.voiceChannel.leave();
return radio.textChannel.send('<:redx:674263474704220182> An error has occured while playing radio!');
}
if (station - 1 > client.stations.length - 1) {
radio.voiceChannel.leave();
return radio.textChannel.send('<:redx:674263474704220182> An error has occured while playing radio!');
}
url = client.stations[station - 1].stream[client.stations[station - 1].stream.default];
if (!url) {
radio.voiceChannel.leave();
return radio.textChannel.send('<:redx:674263474704220182> An error has occured while playing radio!');
}
const dispatcher = radio.connection
.play(url, { bitrate: 1024, passes: 10, volume: 1, highWaterMark: 1 << 25 })

View File

@ -0,0 +1,36 @@
module.exports = function (key, client) {
let foundStations = [];
if (!key) return false;
const probabilityIncrement = 100 / key.split(' ').length / 2;
for (let i = 0; i < key.split(' ').length; i++) {
client.stations.filter(x => x.name.toUpperCase().includes(key.split(' ')[i].toUpperCase()) || x === key).forEach(x => foundStations.push({ station: x, name: x.name, probability: probabilityIncrement }));
}
if (foundStations.length === 0) return false;
for (let i = 0; i < foundStations.length; i++) {
for (let j = 0; j < foundStations.length; j++) {
if (foundStations[i] === foundStations[j] && i !== j) foundStations.splice(i, 1);
}
}
for (let i = 0; i < foundStations.length; i++) {
if (foundStations[i].name.length > key.length) {
foundStations[i].probability -= (foundStations[i].name.split(' ').length - key.split(' ').length) * (probabilityIncrement * 0.5);
} else if (foundStations[i].name.length === key.length) {
foundStations[i].probability += (probabilityIncrement * 0.9);
}
for (let j = 0; j < key.split(' ').length; j++) {
if (!foundStations[i].name.toUpperCase().includes(key.toUpperCase().split(' ')[j])) {
foundStations[i].probability -= (probabilityIncrement * 0.5);
}
}
}
let highestProbabilityStation;
console.log('Stations found: ', foundStations);
for (let i = 0; i < foundStations.length; i++) {
if (!highestProbabilityStation || highestProbabilityStation.probability < foundStations[i].probability) highestProbabilityStation = foundStations[i];
if (highestProbabilityStation && highestProbabilityStation.probability === foundStations[i].probability) {
highestProbabilityStation = foundStations[i].station;
}
}
return highestProbabilityStation;
};