-
Notifications
You must be signed in to change notification settings - Fork 533
/
botInfo.plugin.coffee
218 lines (180 loc) · 6.86 KB
/
botInfo.plugin.coffee
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#META { "name": "botInfo" } *//
class botInfo
getName: -> "Bot Info"
getDescription: -> "Shows bots' infos from `discordbots.org`. Depends on samogot's Discord Internals Library: https://git.io/v7Sfp"
getAuthor: -> "square"
getVersion: -> "1.0.3"
load: ->
UserPopoutComponent = UserStore = PrivateChannelActions = cancels = BotInfoComponent = ExternalLinkComponent = React = DI = null
infoCache = {}
request = require "request"
start: ->
cancels = []
DI = window.DiscordInternals
minDI = "1.6"
if not (0 <= DI?.versionCompare DI.version, minDI)
alert "DiscordInternals #{minDI} or higher not found. Please update or install it. (https://git.io/v7Sfp)"
return
minDI = "1.8"
if not (0 <= DI?.versionCompare DI.version, minDI)
log "DiscordInternals #{minDI} or higher not found. Update recommended! (https://git.io/v7Sfp)"
BdApi.injectCSS "bot-info", css
{Filters, Renderer, React, WebpackModules} = DI
defineBotInfoComponent() if not BotInfoComponent?
UserPopoutComponent ?= await getUserPopoutComponent()
UserStore ?= WebpackModules.findByUniqueProperties ["getUser", "getCurrentUser"]
PrivateChannelActions ?= WebpackModules.findByUniqueProperties ["openPrivateChannel", "ensurePrivateChannel"]
ExternalLinkComponent ?= WebpackModules.find Filters.byCode /\.trusted\b/
cancels.push Renderer.patchRender UserPopoutComponent, [
selector: className: "body-3iLsc4"
method: "prepend"
content: (_this) -> <BotInfoComponent user={_this.props.user} />
]
return
stop: ->
BdApi.clearCSS "bot-info"
c() for c in cancels
return
log = (msg) ->
console.log msg
return
getUserPopoutComponent = -> new Promise (resolve) ->
observer = new MutationObserver ([{addedNodes}]) ->
for {firstChild} in addedNodes when firstChild?.classList?.contains "userPopout-3XzG_A"
userPopout = firstChild
break
if userPopout?
observer.disconnect()
component = DI.getInternalInstance(userPopout).return.stateNode.constructor
component.displayName ?= "UserPopout"
resolve component
return
observer.observe document.querySelector("#app-mount > .popouts-3dRSmE"), childList: true
return
defineBotInfoComponent = ->
class BotInfoComponent extends React.Component
@displayName: "BotInfo"
handleOnClick: =>
@setState ({collapsed}) -> collapsed: !collapsed
retry: (e) =>
e.stopPropagation()
delete infoCache[@props.user.id]
@setState loading: true
@componentWillMount()
return
defaultLinkProps =
rel: "norefferer"
target: "_blank"
constructor: (props) ->
super props
@state =
loading: !infoCache[props.user.id]?
collapsed: true
componentWillMount: ->
{bot, id} = @props.user
return if not bot or infoCache[id]?
info = await new Promise (resolve) -> request "https://discordbots.org/api/bots/#{id}", (e, {statusCode}, msg) ->
return resolve error: e if e
return resolve try JSON.parse (msg if statusCode is 200 or statusCode is 404) \
catch e then error: e
infoCache[id] = info
@setState loading: false
return
render: ->
user = @props.user
return null if not user.bot
info = infoCache[user.id]
{loading, collapsed} = @state
(<div className="botInfo">
<div className="botInfo-inner bodyTitle-Y0qMQz marginBottom8-AtZOdT size12-3R0845 weightBold-2yjlgw">Bot Info</div>
<div className="botInfo-inner endBodySection-Rf4s-7 marginBottom20-32qID7">
{if loading
<span className="loading">loading...</span>
else if info? and not info.error?
if collapsed
<div className="desc shortdesc" onClick={@handleOnClick}>
{info.shortdesc}
<span className="more">more...</span>
</div>
else [
<div className="desc" onClick={@handleOnClick}>{info.longdesc}</div>
info.invite and <ExternalLinkComponent className="invite" key="invite" href={info.invite} title="Invite"/>
info.github and <ExternalLinkComponent className="github" key="github" href={info.github} title="Github"/>
info.website and <ExternalLinkComponent className="website" key="website" href={info.website} title="Website"/>
info.owners?.length and <div className="owners">
{"Owner#{if info.owners.length > 1 then "s" else ""}: "}
{info.owners.map (owner, i) -> [
<FakeMentionComponent key={owner} userId={owner} />
i + 1 < info.owners.length and ", "
]}
</div>
]
else
<div className="error">
{"Error: #{info?.error ? "unknown"} "}
<button type="button" className="addButton-pcyyf6 weightMedium-2iZe9B" onClick={@retry}>Retry</button>
</div>
}
</div>
</div>)
usernames = {}
class FakeMentionComponent extends React.Component
@displayName: "FakeMention"
constructor: (props) ->
super props
@state = username: UserStore.getUser(props.userId)?.username ? usernames[props.userId]
handleOnClick = (id, e) ->
e.stopPropagation()
ownId = UserStore.getCurrentUser().id
try await PrivateChannelActions.openPrivateChannel ownId, id
return
componentWillMount: ->
if !@state.username?
username = await new Promise (resolve) => request "https://discordbots.org/api/users/#{@props.userId}", (e, {statusCode}, msg) =>
if statusCode is 200
try return resolve JSON.parse(msg).username
return resolve "<@!#{@props.userId}>"
@setState {username}
usernames[@props.userId] = username
return
render: ->
<span className="mention" onClick={handleOnClick.bind null, @props.userId}>
{@state.username ? "<@!#{@props.userId}>"}
</span>
return
css =
"""
.botInfo {
color: #99aab5;
font-size: 13px;
}
.theme-dark .botInfo {
color: #b9bbbe;
}
.botInfo .desc {
cursor: pointer;
}
.botInfo .more {
margin-left: 4px;
opacity: 0.5;
}
.botInfo a {
color: #00b0f4;
margin-right: 4px;
}
.theme-dark .botInfo a {
color: #0096cf;
}
.botInfo button {
background-color: transparent;
border: 1px solid #dbdde1;
border-radius: 3px;
color: #737f8d;
font-size: 12px;
line-height: 12px;
}
.theme-dark .botInfo button {
border-color: #72767d;
color: #b9bbbe;
}
"""