Partial rewrite

This commit is contained in:
MatteZ02 2020-03-12 11:59:05 +02:00 committed by Christer Warén
parent b275e28abe
commit 695c5a1e40
11 changed files with 113 additions and 115 deletions

View File

@ -14,8 +14,25 @@ module.exports = {
const embed = new Discord.MessageEmbed() const embed = new Discord.MessageEmbed()
.setTitle("Now Playing") .setTitle("Now Playing")
.setColor(client.config.embedColor) .setColor(client.config.embedColor)
.setDescription(`**${radio.station.name}** \n Owner: ${radio.station.owner} \n\`${client.funcs.msToTime(completed, "hh:mm:ss")}\``) .setDescription(`**${radio.station.name}** \n Owner: ${radio.station.owner} \n\`${msToTime(completed, "hh:mm:ss")}\``)
.setFooter('EximiaBots by Warén Media'); .setFooter('EximiaBots by Warén Media');
return msg.channel.send(embed); return msg.channel.send(embed);
} }
}; };
function msToTime(duration, format) {
var seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
days = Math.floor((duration / (1000 * 60 * 60 * 24)) % 24);
days = (days < 10) ? "0" + days : days;
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
if (format === "hh:mm:ss") {
return `${hours}:${minutes}:${seconds}`;
} else if (format === "dd:hh:mm:ss") {
return `${days}:${hours}:${minutes}:${seconds}`;
}
}

View File

@ -34,7 +34,8 @@ module.exports = {
station = client.stations[number]; station = client.stations[number];
} }
} else { } else {
const sstation = await client.funcs.searchStation(args.slice(1).join(' '), client); if (args[1].length < 3) return msg.channel.send('Station must be over 2 characters!');
const sstation = await searchStation(args.slice(1).join(' '), client);
if (!sstation) return msg.channel.send('No stations found!'); if (!sstation) return msg.channel.send('No stations found!');
url = sstation.stream[sstation.stream.default]; url = sstation.stream[sstation.stream.default];
station = sstation station = sstation
@ -43,7 +44,7 @@ module.exports = {
if (radio) { if (radio) {
radio.connection.dispatcher.destroy(); radio.connection.dispatcher.destroy();
radio.station = station; radio.station = station;
client.funcs.play(msg.guild, client, url); play(msg.guild, client, url);
return; return;
} }
@ -61,7 +62,7 @@ module.exports = {
try { try {
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
construct.connection = connection; construct.connection = connection;
client.funcs.play(msg.guild, client, url); play(msg.guild, client, url);
} catch (error) { } catch (error) {
client.radio.delete(msg.guild.id); client.radio.delete(msg.guild.id);
client.debug_channel.send("Error with connecting to voice channel: " + error); client.debug_channel.send("Error with connecting to voice channel: " + error);
@ -69,3 +70,71 @@ module.exports = {
} }
} }
}; };
function play(guild, client, url) {
const radio = client.radio.get(guild.id);
const dispatcher = radio.connection
.play(url, { bitrate: 1024, passes: 10, volume: 1, highWaterMark: 1 << 25 })
.on("finish", () => {
radio.voiceChannel.leave();
client.radio.delete(guild.id);
return;
});
dispatcher.on('start', () => {
dispatcher.player.streamingData.pausedTime = 0;
});
dispatcher.on('error', error => {
console.error(error);
radio.voiceChannel.leave();
client.radio.delete(guild.id);
return radio.textChannel.send('An error has occured while playing radio!');
});
dispatcher.setVolume(radio.volume / 10);
radio.textChannel.send(`Start playing: ${radio.station.name}`);
radio.playing = true;
};
function searchStation(key, client) {
if (client.stations === null) return false;
let foundStations = [];
if (!key) return false;
if (key == 'radio') return false;
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;
};

View File

@ -7,9 +7,11 @@ module.exports = {
category: 'music', category: 'music',
execute(msg, args, client, Discord, prefix, command) { execute(msg, args, client, Discord, prefix, command) {
const radio = client.radio.get(msg.guild.id); const radio = client.radio.get(msg.guild.id);
if (client.funcs.check(client, msg, command)) {
radio.connection.dispatcher.destroy(); radio.connection.dispatcher.destroy();
radio.voiceChannel.leave(); radio.voiceChannel.leave();
client.radio.delete(msg.guild.id); client.radio.delete(msg.guild.id);
msg.channel.send('Stopped playback!'); msg.channel.send('Stopped playback!');
} }
}
}; };

View File

@ -9,6 +9,14 @@ module.exports = {
const commandName = args[0].toLowerCase(); const commandName = args[0].toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)) || client.commandAliases.get(commandName); const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)) || client.commandAliases.get(commandName);
if (!command && msg.content !== `${prefix}`) return; if (!command && msg.content !== `${prefix}`) return;
client.funcs.exe(msg, args, client, Discord, prefix, command); const permissions = msg.channel.permissionsFor(msg.client.user);
if (!permissions.has('EMBED_LINKS')) return msg.channel.send('I cannot send embeds (Embed links).');
try {
command.uses++;
command.execute(msg, args, client, Discord, prefix, command);
} catch (error) {
msg.reply(`Error!`);
console.error(error);
}
} }
} }

View File

@ -15,11 +15,9 @@ module.exports = class extends Client {
this.radio = new Map(); this.radio = new Map();
this.funcs = {}; this.funcs = {};
this.dispatcher = {}; this.dispatcher = {};
this.config = require('../config.js'); this.config = require('./config.js');
fs.readdirSync(path.join(__dirname, 'funcs')).forEach(filename => { this.funcs.check = require('./check.js');
this.funcs[filename.slice(0, -3)] = require(`./funcs/${filename}`);
});
const commandFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands')).filter(f => f.endsWith('.js')); const commandFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands')).filter(f => f.endsWith('.js'));
for (const file of commandFiles) { for (const file of commandFiles) {
@ -39,7 +37,7 @@ module.exports = class extends Client {
require(`${events}voiceStateUpdate`).execute(this, oldState, newState); require(`${events}voiceStateUpdate`).execute(this, oldState, newState);
}); });
this.on('error', (error) => { this.on('error', (error) => {
client.channels.fetch(client.config.debug_channel).send('Error: ' + error); console.error(error);
}); });
this.login(this.config.token).catch(err => console.log('Failed to login: ' + err)); this.login(this.config.token).catch(err => console.log('Failed to login: ' + err));

View File

@ -3,8 +3,7 @@ require('dotenv/config');
module.exports = { module.exports = {
//credentials //credentials
token: "", token: process.env.DISCORD_TOKEN,
//support //support
supportGuild: "https://discord.gg/rRA65Mn", supportGuild: "https://discord.gg/rRA65Mn",
devId: [ devId: [

View File

@ -1,11 +0,0 @@
module.exports = function (msg, args, client, Discord, prefix, command) {
const permissions = msg.channel.permissionsFor(msg.client.user);
if (!permissions.has('EMBED_LINKS')) return msg.channel.send('I cannot send embeds (Embed links).');
try {
command.uses++;
command.execute(msg, args, client, Discord, prefix, command);
} catch (error) {
msg.reply(`Error!`);
console.error(error);
}
};

View File

@ -1,17 +0,0 @@
module.exports = function msToTime(duration, format) {
var seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
days = Math.floor((duration / (1000 * 60 * 60 * 24)) % 24);
days = (days < 10) ? "0" + days : days;
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
if (format === "hh:mm:ss") {
return `${hours}:${minutes}:${seconds}`;
} else if (format === "dd:hh:mm:ss") {
return `${days}:${hours}:${minutes}:${seconds}`;
}
}

View File

@ -1,29 +0,0 @@
module.exports = async function (guild, client, url) {
const radio = client.radio.get(guild.id);
const dispatcher = radio.connection
.play(url, { bitrate: 1024, passes: 10, volume: 1, highWaterMark: 1 << 25 })
.on("finish", () => {
radio.voiceChannel.leave();
client.radio.delete(guild.id);
return;
});
dispatcher.on('start', () => {
dispatcher.player.streamingData.pausedTime = 0;
});
dispatcher.on('error', error => {
console.error(error);
radio.voiceChannel.leave();
client.radio.delete(guild.id);
return radio.textChannel.send('An error has occured while playing radio!');
});
dispatcher.setVolume(radio.volume / 10);
radio.textChannel.send(`Start playing: ${radio.station.name}`);
radio.playing = true;
}

View File

@ -1,38 +0,0 @@
module.exports = function (key, client) {
if (client.stations === null) return false;
let foundStations = [];
if (!key) return false;
if (key == 'radio') return false;
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;
};