From 16763c4a3e5a3b0b21b3ba17a8ce288ad6b6d61c Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 14 Feb 2018 17:26:48 +0200 Subject: [PATCH] Some apis --- client/src/index.js | 3 +- client/src/modules/discordapi.js | 122 +++++++++++++++++++++++++++++++ client/src/modules/modules.js | 1 + client/src/modules/vendor.js | 2 + 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 client/src/modules/discordapi.js diff --git a/client/src/index.js b/client/src/index.js index e87fdfe8..e0dc0352 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -10,12 +10,13 @@ import { DOM, BdUI, Modals } from 'ui'; import BdCss from './styles/index.scss'; -import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings } from 'modules'; +import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings, DiscordApi } from 'modules'; import { ClientLogger as Logger, ClientIPC } from 'common'; class BetterDiscord { constructor() { + window.discordApi = DiscordApi; window.ClientIPC = ClientIPC; window.css = CssEditor; window.pm = PluginManager; diff --git a/client/src/modules/discordapi.js b/client/src/modules/discordapi.js new file mode 100644 index 00000000..37e7c169 --- /dev/null +++ b/client/src/modules/discordapi.js @@ -0,0 +1,122 @@ +import WebpackModules from './webpackmodules'; +import { $ } from 'vendor'; + +class Private { + static get token() { + return WebpackModules.getModuleByName('UserInfoStore').getToken(); + } +} + +class EndPoints { + static get base() { return '/api/v6' } + static get channels() { return `${this.base}/channels` } + + static channel(channelId) { + return `${this.channels}/${channelId}`; + } + + static messages(channelId) { + return `${this.channels}/${channelId}/messages`; + } +} + +class Ajax { + + static get authHeader() { + return { authorization: Private.token }; + } + + static async POST(endpoint, data, contentType = 'application/json') { + return new Promise((resolve, reject) => { + $.ajax({ + type: 'POST', + url: endpoint, + contentType, + headers: this.authHeader, + data: JSON.stringify(data), + success: e => resolve(e), + fail: e => reject(e) + }); + }); + } + + static async GET(endpoint, contentType = 'application/json') { + return new Promise((resolve, reject) => { + $.ajax({ + type: 'GET', + url: endpoint, + contentType, + headers: this.authHeader, + success: e => resolve(e), + fail: e => reject(e) + }); + }); + } +} + +class Map { + + constructor(data) { + this.data = data; + } + + find(filters) { + return this.data.find(item => { + for (let filter of filters) { + const key = Object.keys(filter)[0]; + const value = filter[key]; + if (item[key] !== value) return false; + } + return true; + }); + } + +} + +class Guild { + constructor(data) { + data.sendMessage = this.sendMessage; + return data; + } +} + + +class Channel { + + constructor(data) { + data.sendMessage = this.sendMessage; + return data; + } + + async sendMessage(content) { + try { + const result = await Ajax.POST(EndPoints.messages(this.id), { content }); + return result; + } catch (err) { + throw err; + } + } + +} + +export default class DiscordApi { + + static get channels() { + const channels = WebpackModules.getModuleByName('ChannelStore').getChannels(); + const returnChannels = []; + for (const [key, value] of Object.entries(channels)) { + returnChannels.push(new Channel(value)); + } + return new Map(returnChannels); + } + + static get guilds() { + const guilds = WebpackModules.getModuleByName('GuildStore').getGuilds(); + const returnGuilds = []; + for (const [key, value] of Object.entries(guilds)) { + returnGuilds.push(new Guild(value)); + } + return new Map(returnGuilds); + } + +} diff --git a/client/src/modules/modules.js b/client/src/modules/modules.js index 90f3e99c..a2d46722 100644 --- a/client/src/modules/modules.js +++ b/client/src/modules/modules.js @@ -11,3 +11,4 @@ export { default as ModuleManager } from './modulemanager'; export { default as EventListener } from './eventlistener'; export { default as SocketProxy } from './socketproxy'; export { default as EventHook } from './eventhook'; +export { default as DiscordApi } from './discordapi'; diff --git a/client/src/modules/vendor.js b/client/src/modules/vendor.js index b0439811..f2796b84 100644 --- a/client/src/modules/vendor.js +++ b/client/src/modules/vendor.js @@ -12,6 +12,8 @@ import WebpackModules from './webpackmodules'; import jQuery from 'jquery'; import lodash from 'lodash'; +export { jQuery, jQuery as $ }; + export default class { static get jQuery() {