-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
190 lines (159 loc) · 5.62 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const { Plugin } = require('powercord/entities');
const { React, getModule, getAllModules, i18n: { Messages } } = 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 DiscordBio = require('./components/DiscordBio');
const Settings = require('./components/Settings');
const i18n = require('./i18n');
module.exports = class Bio extends Plugin {
constructor () {
super();
this.ConnectedDiscordBio = this.settings.connectStore(DiscordBio)
}
async startPlugin() {
await powercord.api.i18n.loadAllStrings(i18n);
this.classes = {
...await getModule(['headerInfo', 'nameTag']),
...await getAllModules(['modal', 'inner'])[1],
...await getModule(['emptyIcon']),
header: (await getModule(['iconBackgroundTierNone', 'container'])).header
};
Object.keys(this.classes).forEach(
key => this.classes[key] = `.${this.classes[key].split(' ')[0]}`
);
powercord.api.settings.registerSettings('discord-bio', {
category: 'discord-bio',
label: 'discord.bio',
render: Settings
});
this.loadStylesheet('style.css');
this._patchUserProfile();
powercord.api.connections.registerConnection({
type: 'discord-bio',
name: 'discord.bio',
color: '#7289da',
icon: {
color: 'https://discord.com/assets/28174a34e77bb5e5310ced9f95cb480b.png',
white: 'https://discord.com/assets/e05ead6e6ebc08df9291738d0aa6986d.png'
},
enabled: true,
fetchAccount: async (id) => {
try {
if (!id) {
({
id
} = (await getModule(['getCurrentUser'])).getCurrentUser());
}
const bio = await this.fetchBio(id);
return ({
type: 'discord-bio',
id: bio.user.details.slug,
name: bio.discord.username,
verified: !!bio.user.details.verified
});
} catch (e) {
//Just ignore the error, probably just 404
}
},
getPlatformUserUrl: (account) => {
const slug = account.id;
return `https://dsc.bio/${encodeURIComponent(slug)}`;
},
onDisconnect: () => void 0
});
}
pluginWillUnload() {
uninject('discord-bio-user-load');
uninject('discord-bio-user-tab-bar');
uninject('discord-bio-user-body');
uninject('discord-bio-user-header');
powercord.api.connections.unregisterConnection('discord-bio');
powercord.api.settings.unregisterSettings('discord-bio');
forceUpdateElement(this.classes.header);
}
async fetchBio(id) {
return await get(`https://api.discord.bio/user/details/${id}`)
.then(r => r.body && r.body.payload);
}
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('discord-bio-user-load', UserProfileBody.prototype, 'componentDidMount', async function (_, res) {
const { user } = this.props;
if (!user || user.bot) return;
try {
const bio = await _this.fetchBio(user.id);
this.setState({ _dscBio: bio });
} catch (e) {
switch (e.statusCode) {
case 404: {
this.setState({
_dscBio: {
error: {
message: Messages.DSCBIO_NO_DISCORD_BIO_PROFILE,
icon: this.classes.emptyIconFriends,
}
}
});
break;
}
case 429: {
this.setState({
_dscBio: {
error: {
message: Messages.DSCBIO_DISCORD_BIO_RATELIMITED,
}
}
});
break;
}
default: {
_this.error('dsc.bio error:', e)
this.setState({
_dscBio: {
error: {
message: Messages.DSCBIO_DISCORD_BIO_UNKNOWN_ERROR,
}
}
});
break;
}
}
}
});
inject('discord-bio-user-tab-bar', UserProfileBody.prototype, 'renderTabBar', function (_, res) {
const { user } = this.props;
// Don't bother rendering if there's no tab bar, user or if the user is a bot
if (!res || !user || user.bot) return res;
// Create discord.bio tab bar item
const showSetting = _this.settings.get('show-bio-tab', 'always')
if (showSetting === 'never' || (showSetting === 'has-bio' && (!this.state._dscBio || this.state._dscBio.error))) {
return res;
}
const bioTab = React.createElement(TabBar.Item, {
key: 'DISCORD_BIO',
className: tabBarItem,
id: 'DISCORD_BIO'
}, 'Bio');
// Add the discord.bio tab bar item to the list
res.props.children.props.children.push(bioTab)
return res;
});
inject('discord-bio-user-body', UserProfileBody.prototype, 'render', function (_, res) {
const { children } = res.props;
const { section } = this.props;
if (section !== 'DISCORD_BIO') return res;
const body = children.props.children[1];
body.props.children = [];
body.props.children.push(React.createElement(_this.ConnectedDiscordBio, { bio: this.state._dscBio }));
return res;
});
}
}