diff --git a/index.mjs b/index.mjs index df436c2..d0ef6b7 100644 --- a/index.mjs +++ b/index.mjs @@ -5,7 +5,7 @@ import EventEmitter from "events"; const clients = []; -const cuffeo = class cuffeo extends EventEmitter { +export default class cuffeo extends EventEmitter { constructor(cfg) { super(); for (let srv in cfg) { @@ -36,5 +36,3 @@ const cuffeo = class cuffeo extends EventEmitter { }); } }; - -export { cuffeo, clients }; diff --git a/package.json b/package.json index 7b5417a..372c231 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,6 @@ "author": "Flummi", "license": "ISC", "dependencies": { + "flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies" } } diff --git a/src/clients/discord.mjs b/src/clients/discord.mjs new file mode 100644 index 0000000..f7ee12e --- /dev/null +++ b/src/clients/discord.mjs @@ -0,0 +1,77 @@ +import Discord from "discord.js"; +import EventEmitter from "events"; + +export class discord extends EventEmitter { + constructor(options) { + super(); + this.options = options || {}; + this.token = options.token || null; + this.set = this.options.set || "all"; + this.network = "discord"; + + this.bot = new Discord.Client(); + this.bot.login(this.token); + + this.server = { + set: this.set, + channel: new Map(), + user: new Map(), + me: {} + }; + + this.bot.on("ready", () => { + this.server.me = { + nickname: this.bot.user.username, + username: this.bot.user.username, + account: this.bot.user.id.toString(), + prefix: `${this.bot.user.username}!${this.bot.user.id.toString()}`, + id: this.bot.user.id.toString() + }; + }); + + this.bot.on("message", msg => { + if(msg.author.id !== this.server.me.id) + this.emit("data", ["message", this.reply(msg)]); + }); + } + reply(tmp) { + return { + type: "discord", + network: "Discord", + channel: tmp.channel.name, + channelid: tmp.channel.id, + user: { + prefix: `${tmp.author.username}!${tmp.author.id}`, + nick: tmp.author.username, + username: tmp.author.username, + account: tmp.author.id.toString() + }, + message: tmp.content, + time: ~~(Date.now() / 1000), + self: this.server, + reply: msg => this.send(tmp, this.format(msg)), + replyAction: msg => this.send(tmp, this.format(`*${msg}*`), "normal"), + replyNotice: msg => this.send(tmp, this.format(msg)) + }; + } + send(r, msg, mode = "blah") { + switch(mode) { + case "normal": + r.channel.send(msg); + break; + default: + r.reply(msg); + break; + } + } + sendmsg(mode, recipient, msg) { + this.bot.channels.get(recipient).send(msg); + } + format(msg) { + return msg.toString() + .replace(/\[b\](.*?)\[\/b\]/g, "**$1**") // bold + .replace(/\[i\](.*?)\[\/i\]/g, "*$1*") // italic + .replace(/\[color=(.*?)](.*?)\[\/color\]/g, "$2") + ; + } +}; diff --git a/src/clients/tg.mjs b/src/clients/tg.mjs index 69cd041..0b3397e 100644 --- a/src/clients/tg.mjs +++ b/src/clients/tg.mjs @@ -1,4 +1,4 @@ -import fetch from "../inc/fetch"; +import fetch from "flumm-fetch-cookies"; import EventEmitter from "events"; export class tg extends EventEmitter { diff --git a/src/inc/fetch.mjs b/src/inc/fetch.mjs deleted file mode 100644 index 1a7c2c7..0000000 --- a/src/inc/fetch.mjs +++ /dev/null @@ -1,31 +0,0 @@ -import http from "http"; -import https from "https"; -import url from "url"; -import querystring from "querystring"; - -export default (a, options = {}, link = url.parse(a)) => new Promise((resolve, reject) => { - options = {...{ - hostname: link.hostname, - path: link.path, - method: "GET" - }, ...options}; - let body = ""; - if(options.method === "POST") { - body = querystring.stringify(options.body); - delete options.body; - options.headers = { - "Content-Type": "application/x-www-form-urlencoded", - "Content-Length": Buffer.byteLength(body) - } - } - const post = (link.protocol === "https:"?https:http).request(options, (res, data = "") => res - .setEncoding("utf8") - .on("data", chunk => data += chunk) - .on("end", () => resolve({ - text: () => data, - json: () => { try { return JSON.parse(data); } catch(err) { return "no json D:"; } }, - buffer: () => new Buffer.from(data) - })) - ).on("error", err => reject(err)); - post.end(body); -});