mirror of
https://github.com/warengroup/eximiabots-radiox.git
synced 2024-12-23 01:13:17 +00:00
Moved searchStations function into funcs folder
This commit is contained in:
parent
a3efdbf3cb
commit
c724ae502f
@ -39,6 +39,7 @@ class RadioClient extends Client {
|
||||
this.funcs.statisticsUpdate = require("./client/funcs/statisticsUpdate.js");
|
||||
this.funcs.saveState = require("./client/funcs/saveState.js");
|
||||
this.funcs.loadState = require("./client/funcs/loadState.js");
|
||||
this.funcs.searchStations = require("./client/funcs/searchStations.js");
|
||||
|
||||
console.log('RadioX ' + this.config.version);
|
||||
console.log('Internet Radio to your Discord guild');
|
||||
|
@ -123,7 +123,7 @@ module.exports = {
|
||||
content: client.messageEmojis["error"] + client.messages.tooShortSearch,
|
||||
ephemeral: true
|
||||
});
|
||||
const sstation = await searchStation(query, client);
|
||||
const sstation = await client.funcs.searchStation(query, client);
|
||||
if (!sstation)
|
||||
return interaction.reply({
|
||||
content: client.messageEmojis["error"] + client.messages.noSearchResults,
|
||||
@ -273,67 +273,4 @@ async function play(interaction, guild, client, url, Discord) {
|
||||
ephemeral: true
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function searchStation(key, client) {
|
||||
if (client.stations === null) return false;
|
||||
let foundStations = [];
|
||||
if (!key) return false;
|
||||
if (key == "radio") return false;
|
||||
|
||||
client.stations
|
||||
.filter(
|
||||
x => x.name.toUpperCase().includes(key.toUpperCase()) || x === key
|
||||
)
|
||||
.forEach(x =>
|
||||
foundStations.push({ station: x, name: x.name, probability: 100 })
|
||||
);
|
||||
|
||||
if (key.startsWith("radio ")) key = key.slice(6);
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
62
src/client/funcs/searchStations.js
Normal file
62
src/client/funcs/searchStations.js
Normal file
@ -0,0 +1,62 @@
|
||||
module.exports = function searchStation(key, client) {
|
||||
if (client.stations === null) return false;
|
||||
let foundStations = [];
|
||||
if (!key) return false;
|
||||
if (key == "radio") return false;
|
||||
|
||||
client.stations
|
||||
.filter(
|
||||
x => x.name.toUpperCase().includes(key.toUpperCase()) || x === key
|
||||
)
|
||||
.forEach(x =>
|
||||
foundStations.push({ station: x, name: x.name, probability: 100 })
|
||||
);
|
||||
|
||||
if (key.startsWith("radio ")) key = key.slice(6);
|
||||
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;
|
||||
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;
|
||||
}
|
@ -15,7 +15,7 @@ module.exports = {
|
||||
if(!state) return;
|
||||
if(!state.station || !state.channels.voice || !state.channels.text) return;
|
||||
|
||||
const sstation = await searchStation(state.station.name, client);
|
||||
const sstation = await client.funcs.searchStation(state.station.name, client);
|
||||
let url = sstation.stream[sstation.stream.default];
|
||||
let station = sstation;
|
||||
|
||||
@ -143,66 +143,3 @@ async function play(interaction, guild, client, url, Discord) {
|
||||
message.play = client.messages.play.replace("%radio.station.name%", radio.station.name);
|
||||
|
||||
}
|
||||
|
||||
function searchStation(key, client) {
|
||||
if (client.stations === null) return false;
|
||||
let foundStations = [];
|
||||
if (!key) return false;
|
||||
if (key == "radio") return false;
|
||||
|
||||
client.stations
|
||||
.filter(
|
||||
x => x.name.toUpperCase().includes(key.toUpperCase()) || x === key
|
||||
)
|
||||
.forEach(x =>
|
||||
foundStations.push({ station: x, name: x.name, probability: 100 })
|
||||
);
|
||||
|
||||
if (key.startsWith("radio ")) key = key.slice(6);
|
||||
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;
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user