From 4b3b798ddc8b0a7c51d484a8dca42355f9478e37 Mon Sep 17 00:00:00 2001 From: MatteZ02 <47610069+MatteZ02@users.noreply.github.com> Date: Tue, 16 Jun 2020 21:51:11 +0300 Subject: [PATCH] New config system and error logging --- index.js | 16 ++++++-- package.json | 3 +- src/bot.js | 13 ++++++- src/struct/config/config.js | 63 ++++++++++++++++++++++++++++++- src/struct/config/emojis.js | 21 ----------- src/struct/config/messages.js | 4 +- src/struct/config/queueConfig.js | 18 --------- src/struct/config/streamConfig.js | 13 ------- src/struct/funcs/play.js | 21 ++++++----- 9 files changed, 101 insertions(+), 71 deletions(-) delete mode 100644 src/struct/config/emojis.js delete mode 100644 src/struct/config/queueConfig.js delete mode 100644 src/struct/config/streamConfig.js diff --git a/index.js b/index.js index a557dd64..59afcfbd 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const config = require("./src/struct/config/config.js"); +const DiscordWebhook = require("discord-webhook-node"); if (config.devMode) { console.log("- dev mode- "); @@ -6,9 +7,7 @@ if (config.devMode) { config.shards = 1; } -const { - ShardingManager -} = require("discord.js"); +const { ShardingManager } = require("discord.js"); const manager = new ShardingManager("./src/bot.js", { token: config.token, respawn: config.respawn, @@ -19,4 +18,13 @@ console.log("- Launching shards -"); manager.spawn(config.shards, config.shardDelay, config.shardTimeout); manager.on("shardCreate", (shard) => console.log(`- Launched shard ${shard.id} -`) -); \ No newline at end of file +); + +const webhookClient = new DiscordWebhook.Webhook(config.webhookUrl); + +const oldConsole = {}; +oldConsole.log = console.log; +console.log = function (arg) { + oldConsole.log(arg); + webhookClient.send(arg); +}; diff --git a/package.json b/package.json index 9d83b638..04951f93 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "cookie-parser": "^1.4.5", "cors": "^2.8.5", "dblapi.js": "^2.4.0", + "discord-webhook-node": "^1.0.7", "discord.js": "^12.2.0", "dotenv": "^8.2.0", "erlpack": "github:discordapp/erlpack", @@ -46,4 +47,4 @@ "ytdl-core": "^3.1.0", "zlib-sync": "^0.1.6" } -} \ No newline at end of file +} diff --git a/src/bot.js b/src/bot.js index f0066a36..6deda9c1 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,2 +1,11 @@ -const MusicClient = require('./struct/client.js'); -const client = new MusicClient({}); \ No newline at end of file +const MusicClient = require("./struct/client.js"); +const DiscordWebhook = require("discord-webhook-node"); +const client = new MusicClient({}); +const webhookClient = new DiscordWebhook.Webhook(client.config.webhookUrl); + +const oldConsole = {}; +oldConsole.log = console.log; +console.log = function (arg) { + oldConsole.log(arg); + webhookClient.send(arg); +}; diff --git a/src/struct/config/config.js b/src/struct/config/config.js index 68d702e4..7eeb6bfc 100644 --- a/src/struct/config/config.js +++ b/src/struct/config/config.js @@ -14,6 +14,7 @@ module.exports = { spotify_refresh_token: process.env.SPOTIFY_REFRESH_TOKEN, lastfm_api_key: process.env.LASTFM_API_KEY, lastfm_secret: process.env.LASTFM_SECRET, + webhookUrl: process.env.WEBHOOK_URL, port: 8888, redirectUri: "http://localhost:8888/callback/", testServer: "489111553321336832", @@ -23,7 +24,7 @@ module.exports = { embedColor: "#b50002", invite: "https://discordapp.com/oauth2/authorize?client_id=607266889537945605&permissions=3427328&scope=bot", supportServer: "https://discord.gg/rvHuJtB", - devMode: false, + devMode: true, api: false, saveDB: true, respawn: true, @@ -39,4 +40,62 @@ module.exports = { djrole: null, startPlaying: true, bass: 1, -}; \ No newline at end of file +}; + +module.exports.streamConfig = { + ytdlOptions: { + filter: "audio", + highWaterMark: 1 << 25, + volume: false, + requestOptions: { + maxRedirects: 4 + } + }, + options: { + seek: null, + bitrate: 1024, + volume: 1, + type: "converted", + }, +} + +module.exports.queueConfig = { + textChannel: null, + voiceChannel: null, + connection: null, + songs: [], + volume: null, + bass: null, + nigthCore: false, + playing: false, + paused: false, + looping: false, + songLooping: false, + votes: 0, + voters: [], + votesNeeded: null, + time: 0, + endReason: null, +} + +module.exports.emojis = { + garbage: "🗑️ ", + green_check_mark: "<:green_check_mark:674265384777416705> ", + loading: " ", + loudSound: ":loud_sound: ", + megaPhone: "📣 ", + notes: "", + pause: "<:pause:674685548610322462> ", + previous: "<:reverse:705012312142119012> ", + redx: "<:redx:674263474704220182> ", + repeat: "<:repeat1:674685561377914892> ", + repeatSong: "<:repeatsong:674685573419761716> ", + resume: "<:resume:674685585478254603> ", + shuffle: "<:shuffle:674685595980791871> ", + signal: ":signal_strength: ", + skip: "<:skip:674685614221688832> ", + speaker: ":speaker: ", + stop: "<:stop:674685626108477519> ", + stopWatch: ":stopwatch: ", + volumeHigh: "<:volumehigh:674685637626167307> ", +} \ No newline at end of file diff --git a/src/struct/config/emojis.js b/src/struct/config/emojis.js deleted file mode 100644 index c5bab43a..00000000 --- a/src/struct/config/emojis.js +++ /dev/null @@ -1,21 +0,0 @@ -module.exports = { - garbage: "🗑️ ", - green_check_mark: "<:green_check_mark:674265384777416705> ", - loading: " ", - loudSound: ":loud_sound: ", - megaPhone: "📣 ", - notes: "", - pause: "<:pause:674685548610322462> ", - previous: "<:reverse:705012312142119012> ", - redx: "<:redx:674263474704220182> ", - repeat: "<:repeat1:674685561377914892> ", - repeatSong: "<:repeatsong:674685573419761716> ", - resume: "<:resume:674685585478254603> ", - shuffle: "<:shuffle:674685595980791871> ", - signal: ":signal_strength: ", - skip: "<:skip:674685614221688832> ", - speaker: ":speaker: ", - stop: "<:stop:674685626108477519> ", - stopWatch: ":stopwatch: ", - volumeHigh: "<:volumehigh:674685637626167307> ", -}; \ No newline at end of file diff --git a/src/struct/config/messages.js b/src/struct/config/messages.js index 73fe2626..a9b55878 100644 --- a/src/struct/config/messages.js +++ b/src/struct/config/messages.js @@ -1,4 +1,6 @@ -const emojis = require("./emojis.js"); +const { + emojis +} = require("./config.js"); module.exports = { emojis: emojis, diff --git a/src/struct/config/queueConfig.js b/src/struct/config/queueConfig.js deleted file mode 100644 index 976bdd5f..00000000 --- a/src/struct/config/queueConfig.js +++ /dev/null @@ -1,18 +0,0 @@ -module.exports = { - textChannel: null, - voiceChannel: null, - connection: null, - songs: [], - volume: null, - bass: null, - nigthCore: false, - playing: false, - paused: false, - looping: false, - songLooping: false, - votes: 0, - voters: [], - votesNeeded: null, - time: 0, - endReason: null, -}; diff --git a/src/struct/config/streamConfig.js b/src/struct/config/streamConfig.js deleted file mode 100644 index 622a6395..00000000 --- a/src/struct/config/streamConfig.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = { - ytdlOptions: { - filter: "audio", - highWaterMark: 1 << 25, - volume: false, - }, - options: { - seek: null, - bitrate: 1024, - volume: 1, - type: "converted", - }, -}; \ No newline at end of file diff --git a/src/struct/funcs/play.js b/src/struct/funcs/play.js index 587d1310..1128d3d9 100644 --- a/src/struct/funcs/play.js +++ b/src/struct/funcs/play.js @@ -1,10 +1,9 @@ module.exports = async function (guild, song, client, seek, play) { - const { - Readable: ReadableStream - } = require("stream"); + console.log("test"); + const { Readable: ReadableStream } = require("stream"); const Discord = require("discord.js"); const ytdl = require("ytdl-core"); - const streamConfig = require("../config/streamConfig.js"); + const { streamConfig } = require("../config/config.js"); const prism = require("prism-media"); const queue = client.queue.get(guild.id); if (!song) { @@ -25,9 +24,10 @@ module.exports = async function (guild, song, client, seek, play) { streamConfig.options.seek = seek; let input = song.url; - if (song.type === "ytdl") input = ytdl(song.url, streamConfig.ytdlOptions) - //.on('info', (info, format) => console.log(format)) - .on('error', err => console.log(err)); + if (song.type === "ytdl") + input = ytdl(song.url, streamConfig.ytdlOptions) + //.on('info', (info, format) => console.log(format)) + .on("error", (err) => console.log(err)); const ffmpegArgs = [ "-analyzeduration", @@ -80,10 +80,13 @@ module.exports = async function (guild, song, client, seek, play) { const embed = new Discord.MessageEmbed() .setTitle(`${client.messages.startPlaying}**${song.title}**`) .setDescription( - `Song duration: \`${client.funcs.msToTime(queue.songs[0].info.lengthSeconds * 1000, "hh:mm:ss")}\`` + `Song duration: \`${client.funcs.msToTime( + queue.songs[0].info.lengthSeconds * 1000, + "hh:mm:ss" + )}\`` ) .setColor(client.config.embedColor); queue.textChannel.send(embed); } queue.playing = true; -}; \ No newline at end of file +};