Some apis

This commit is contained in:
Jiiks 2018-02-14 17:26:48 +02:00
parent 12a5bd35d5
commit 16763c4a3e
4 changed files with 127 additions and 1 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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';

View File

@ -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() {