-
Notifications
You must be signed in to change notification settings - Fork 204
/
CallTimeCounter.plugin.js
140 lines (120 loc) · 4.93 KB
/
CallTimeCounter.plugin.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
/**
* @name CallTimeCounter
* @source https://github.com/QWERTxD/BetterDiscordPlugins/blob/main/CallTimeCounter/CallTimeCounter.plugin.js
* @description Shows how much time you are in a voice chat.
* @updateUrl https://raw.githubusercontent.com/QWERTxD/BetterDiscordPlugins/main/CallTimeCounter/CallTimeCounter.plugin.js
* @website https://github.com/QWERTxD/BetterDiscordPlugins/tree/main/CallTimeCounter
*/
const request = require("request");
const fs = require("fs");
const path = require("path");
const config = {
info: {
name: "CallTimeCounter",
authors: [
{
name: "QWERT"
}
],
version: "0.0.1",
description: "Shows how much time you are in a voice chat.",
},
changelog: [
{
title: "hello world",
type: "added",
items: [
"plugin"
]
}
],
defaultConfig: [ ]
};
module.exports = !global.ZeresPluginLibrary ? class {
constructor() {
this._config = config;
}
load() {
BdApi.showConfirmationModal("Library plugin is needed",
`The library plugin needed for AQWERT'sPluginBuilder is missing. Please click Download Now to install it.`, {
confirmText: "Download",
cancelText: "Cancel",
onConfirm: () => {
request.get("https://rauenzi.github.io/BDPluginLibrary/release/0PluginLibrary.plugin.js", (error, response, body) => {
if (error)
return electron.shell.openExternal("https://betterdiscord.net/ghdl?url=https://raw.githubusercontent.com/rauenzi/BDPluginLibrary/master/release/0PluginLibrary.plugin.js");
fs.writeFileSync(path.join(BdApi.Plugins.folder, "0PluginLibrary.plugin.js"), body);
});
}
});
}
start() { }
stop() { }
} : (([Plugin, Library]) => {
const { DiscordModules, WebpackModules, Patcher, PluginUtilities } = Library;
const { React, Dispatcher } = DiscordModules;
const PanelSubtext = WebpackModules.find(m => m?.default?.displayName === "PanelSubtext");
class Timer extends React.Component {
constructor (props) {
super(props);
this.connected = this.connected.bind(this);
this.state = {
startTime: 0,
delta: 0
};
}
connected (e) {
if (e.state && e.state === 'RTC_DISCONNECTED' && !e.hasOwnProperty('streamKey')) {
this.setState((prev) => (
prev.startTime = Date.now()));
}
}
componentDidMount () {
this.setState((prev) => (
prev.startTime = Date.now()));
Dispatcher.subscribe('RTC_CONNECTION_STATE', this.connected);
this.interval = setInterval(() => {
this.setState((prev) => (prev.delta = Math.round((Date.now() - prev.startTime) / 1000) * 1000));
}, 1000);
}
componentWillUnmount () {
Dispatcher.unsubscribe('RTC_CONNECTION_STATE', this.connected);
clearInterval(this.interval);
}
render () {
return React.createElement("div", { className: "voiceTimer" }, `Time elapsed: ${new Date(this.state.delta).toISOString().substr(11, 8)}`);
}
};
class plugin extends Plugin {
constructor() {
super();
this.getSettingsPanel = () => {
console.log(this.buildSettingsPanel().getElement());
return this.buildSettingsPanel().getElement();
};
}
onStart() {
this.patch();
PluginUtilities.addStyle("voicetimer", `
.voiceTimer {
text-decoration: none !important;
margin-top: 8px;
}
`)
}
onStop() {
Patcher.unpatchAll();
PluginUtilities.removeStyle("voicetimer");
}
patch() {
Patcher.after(PanelSubtext, "default", (_, [props], ret) => {
if (!props.className || !props.className.includes('channel')) return;
ret.props.children = [
props.children,
React.createElement(Timer, {className: "voiceTimer"})
]
})
}
}
return plugin;
})(global.ZeresPluginLibrary.buildPlugin(config));