From f0ea93bd439fa4a2dd8eec601dd19f7123d7819e Mon Sep 17 00:00:00 2001 From: ahoZiorce Date: Mon, 2 Jul 2018 18:28:34 +0200 Subject: [PATCH] Done event channelCreate --- package-lock.json | 5 +++++ package.json | 1 + src/cmd/events.js | 33 ++++++++++++++++++++++++++++++++- src/cmd/main.js | 1 + src/configManager.js | 1 + src/hastebin.js | 16 ++++++++++++++++ 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/hastebin.js diff --git a/package-lock.json b/package-lock.json index fc71985..2578d47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -486,6 +486,11 @@ "simple-concat": "1.0.0" } }, + "snekfetch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-4.0.4.tgz", + "integrity": "sha512-dyycG9fvwtSJqKPfMVOpXt+60qvMGe7vWLwOJDiSJaiAx+hs2EnFChG2bXCWn7ulz+zGzrHdN9/yeEb0YqEPww==" + }, "string-width": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", diff --git a/package.json b/package.json index beb9ea9..b5ffc89 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "bufferutil": "^3.0.5", "eris": "^0.8.6", "level": "^4.0.0", + "snekfetch": "^4.0.4", "uws": "^10.148.1" } } diff --git a/src/cmd/events.js b/src/cmd/events.js index ec6de17..bdab00b 100644 --- a/src/cmd/events.js +++ b/src/cmd/events.js @@ -1,6 +1,37 @@ const commandH = require('../commandHandler'); const bot = require('../botClient').bot; +const dbEI = require('../dbEventInterface'); +const hastebin = require('../hastebin'); +const configM = require('../configManager'); exports.loadModule = function loadModule () { - + bot.on('channelCreate', async channel => { + if (channel.type !== 0 && channel.type !== 2) return; + // Add support for categories + // Also, add independent support for each type of channels + // Also, parse json with circular object support + try { + let a = await dbEI.getEvent(channel.guild.id, 'channelCreate'); + if (a.event.d === true) { + let type = ""; + if (channel.type === 0) { + type = "text"; + } + else { + type = "voice"; + } + let mention = channel.mention; + if (channel.type === 2) mention.shift(); + let hb = ""; + if (a.event.msg.includes("$hastebin")) { + hb = await hastebin(configM.config.hastebinServer, 'Channel ' + channel.name + ' data as JSON\n\n------------------\n\n' + JSON.stringify(channel)); + } + let finalMessage = a.event.msg.replace('$type', type).replace('$mention', mention).replace('$id', channel.id).replace('$timestamp', channel.createdAt).replace('$hastebin', hb).replace('$name', channel.name); + bot.createMessage(a.event.c === 'f' ? a.fallbackChannelId : a.event.c, finalMessage); + } + } + catch(e) { + console.log(e); + } + }); }; \ No newline at end of file diff --git a/src/cmd/main.js b/src/cmd/main.js index 77a8f63..0a8e50f 100644 --- a/src/cmd/main.js +++ b/src/cmd/main.js @@ -47,6 +47,7 @@ exports.loadModule = function loadModule () { bot.createMessage(message.channel.id, `Event channel set to <#${channelId}>. The fallback channel for that even has been overriden. To bind again the event to the fallback channel, execute \`event ${match[1]} channel fallback\``); } }); + // Change the endpoint below, put something that couldn't interfere with the other commandH.endpoint('^event(-set)? (.+) (.+)$', async (match, message) => { let newState = true; if (match[3]) { diff --git a/src/configManager.js b/src/configManager.js index 4a5419f..1d74405 100644 --- a/src/configManager.js +++ b/src/configManager.js @@ -12,6 +12,7 @@ module.exports.loadConfig = function loadConfig(configPath) { let builder = { token: "YOUR BOT TOKEN HERE", game: "BOT GAME HERE", + hastebinServer: "https://hastebin.com", }; fs.appendFileSync(configPath, JSON.stringify(builder)); return false; diff --git a/src/hastebin.js b/src/hastebin.js new file mode 100644 index 0000000..4c5df2b --- /dev/null +++ b/src/hastebin.js @@ -0,0 +1,16 @@ +const snekfetch = require('snekfetch'); + +module.exports = async function hastebin(hastebin, msg) { + try { + let res = await snekfetch.post(hastebin + '/documents').send(msg); + if (res.body.key) { + return `${hastebin}/${res.body.key}`; + } + else { + return ''; + } + } + catch (e) { + return ''; + } +} \ No newline at end of file