-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (74 loc) · 3.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { Plugin } = require('powercord/entities');
const { React, getModule, getAllModules } = require('powercord/webpack');
const { forceUpdateElement, getOwnerInstance, waitFor } = require('powercord/util');
const { inject, uninject } = require('powercord/injector');
const { get } = require('powercord/http');
const { TabBar } = require('powercord/components');
const DiscordRep = require('./components/DiscordRep');
const Settings = require('./components/Settings');
module.exports = class PowerRep extends Plugin {
async startPlugin() {
if(!this.settings.get("apikey"))
this.settings.set("apikey", "");
this.classes = {
...await getModule(['headerInfo', 'nameTag']),
...await getAllModules(['modal', 'inner'])[1],
header: (await getModule(['iconBackgroundTierNone', 'container'])).header
};
Object.keys(this.classes).forEach(
key => this.classes[key] = `.${this.classes[key].split(' ')[0]}`
);
powercord.api.settings.registerSettings('powerrep', {
category: this.entityID,
label: 'DiscordRep',
render: Settings
})
this.loadStylesheet('style.css');
this._patchUserProfile();
}
pluginWillUnload() {
powercord.api.settings.unregisterSettings("powerrep");
uninject('discordrep-user-tab-bar');
uninject('discordrep-user-body');
uninject('discordrep-user-header');
}
async fetchRep(id) {
return await get(`https://discordrep.com/api/v3/rep/${id}`)
.set("Authorization", this.settings.get("apikey"))
.then(r => {this.log(r.body);return r.body});
}
async _patchUserProfile() {
const { classes } = this;
const instance = getOwnerInstance((await waitFor([
classes.modal, classes.headerInfo, classes.nameTag
].join(' '))).parentElement);
const { tabBarItem } = await getModule(['tabBarItem']);
const UserProfileBody = instance._reactInternalFiber.return.type;
const _this = this;
inject('discordrep-user-tab-bar', UserProfileBody.prototype, 'renderTabBar', function (_, res) {
const { user } = this.props;
//Don't bother rendering if there's no tab bar or user
if (!res || !user) return res;
//Create discord.bio tab bar item
const repTab = React.createElement(TabBar.Item, {
key: 'DISCORDREP',
className: tabBarItem,
id: 'DISCORDREP'
}, 'Reputation');
//Add the discord.bio tab bar item to the list
res.props.children.props.children.push(repTab)
return res;
});
inject('discordrep-user-body', UserProfileBody.prototype, 'render', function (_, res) {
const { children } = res.props;
const { section, user } = this.props;
const fetchRep = (id) => _this.fetchRep(id);
const getSetting = (setting, defaultValue) => _this.settings.get(setting, defaultValue);
if (section !== 'DISCORDREP') return res;
const body = children.props.children[1];
body.props.children = [];
body.props.children.push(React.createElement(DiscordRep, { id: user.id, fetchRep, getSetting }));
return res;
});
}
}