This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
151 lines (134 loc) · 5.47 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
const {
Plugin
} = require('powercord/entities');
const {
getModule,
channels: {
getChannelId,
},
FluxDispatcher: Dispatch,
} = require('powercord/webpack');
const Settings = require('./components/settings');
let listenerStatus = false;
class quickChatReply extends Plugin {
constructor() {
super()
this.keyPressHandler = this._keyPressHandler.bind(this);
this.createReply = this._createReply.bind(this);
this.overhaulCurrentChannel = this._overhaulCurrentChannel.bind(this);
this.overhaulCurrentReply = this._overhaulCurrentReply.bind(this);
this.messageUpdater = this._messageUpdater.bind(this);
this.noification_Popup = this.settings.get('Notification-Popup', true);
}
filter(matchfrom, matchto) {
return Object.keys(matchfrom).every((value,) => {
return typeof matchfrom[value]==='string'?matchfrom[value].toUpperCase()===matchto[value].toUpperCase():matchfrom[value]===matchto[value] });
};
_keyPressHandler(keyInput) {
if ( !this.message.channel ) {
const c = getChannelId();
this.message.channel = c;
this.cache.channel = this.fetch.channel( c );
}
if ( this.filter(this.settings.get('replyNext'), keyInput) ) {
const l = this.fetch.message( this.message.channel ).toArray().length;
if ( this.message.index+1 < l ) {
this.message.index++;
this.createReply();
this.newMessageListen(true);
}
else if ( this.noification_Popup&&!powercord.api.notices.toasts['quickChatReply-Max!Reply?Limit']&&this.message.index+1===l ) {
powercord.api.notices.sendToast(
'quickChatReply-Max!Reply?Limit',
{
header: 'Notice!',
timeout: 5000,
content: `You have reached the current loaded message limit of: ${l}.\nTo reply to furthur up messages please scroll up.`,
buttons: [{text:'Disable Forever',look:'filled',size:'small',onClick:()=>{this.settings.set('Notification-Popup', false); this.noification_Popup= !this.noification_Popup; powercord.api.notices.closeToast('quickChatReply-Max!Reply?Limit');}},{text:'Dismiss',look:'filled',size:'small',onClick:()=>{powercord.api.notices.closeToast('quickChatReply-Max!Reply?Limit');}},],
}
);
}
}
else if ( this.filter(this.settings.get('replyPrev'), keyInput) ) {
this.message.index--;
if (this.message.index < 0) {
this.message.index=-1;
this.removeReply();
}
else {
this.createReply();
}
}
}
_createReply() {
this.reply.create({
message: this.fetch.message(this.message.channel).toArray().reverse()[this.message.index],
channel: this.cache.channel,
shouldMention: this.settings.get('mention', true),
showMentionToggle: ((this.cache.channel.guild_id !== null) || false)
})
}
removeReply() {
this.reply.remove(this.message.channel);
}
_overhaulCurrentChannel(dat) {
if (this.message.channel !== dat.channelId) {
this.removeReply();
this.newMessageListen(false);
this.message = { index: -1, channel: dat.channelId };
this.cache.channel = this.fetch.channel(this.message.channel);
}
}
_overhaulCurrentReply(dat) {
if (dat.type === 'CREATE_PENDING_REPLY') {
this.message.index = this.fetch.message(this.message.channel).toArray().reverse().findIndex((e,)=>{return e.id===dat.message.id});
this.newMessageListen(true);
}
else if (dat.type === 'DELETE_PENDING_REPLY') {
this.newMessageListen(false);
this.message = { index: -1, channel: this.message.channel, };
}
}
newMessageListen( b ) {
if ( listenerStatus === b ) return;
if ( b ) Dispatch.subscribe('MESSAGE_CREATE', this.messageUpdater);
else if ( !b ) Dispatch.unsubscribe('MESSAGE_CREATE', this.messageUpdater);
listenerStatus=!listenerStatus;
}
_messageUpdater( m ) {
if ( m.channelId === this.message.channel )
this.message.index++;
}
startPlugin() {
const { createPendingReply, deletePendingReply } = getModule(['createPendingReply',], false);
const { getChannel } = getModule(['getChannel', 'hasChannel',], false);
const { getMessages } = getModule(['initialize', 'getRawMessages',], false);
this.cache = { channel: undefined, };
this.message = { index: -1, channel: undefined, };
this.fetch = { message: getMessages, channel: getChannel, };
this.reply = { create: createPendingReply, remove: deletePendingReply, };
/* Settings */
this.settings.set('replyNext', this.settings.get('replyNext', {ctrlKey: true, shiftKey: false, altKey: false, metaKey: false, key: 'ArrowUp'}));
this.settings.set('replyPrev', this.settings.get('replyPrev', {ctrlKey: true, shiftKey: false, altKey: false, metaKey: false, key: 'ArrowDown'}));
this.settings.set('mention', this.settings.get('mention', true));
powercord.api.settings.registerSettings(this.entityID, {
category: this.entityID,
label: 'Quick Chat Reply',
render: Settings,
});
/* Listeners */
window.addEventListener('keydown', this.keyPressHandler);
Dispatch.subscribe('CHANNEL_SELECT', this.overhaulCurrentChannel);
Dispatch.subscribe('CREATE_PENDING_REPLY', this.overhaulCurrentReply);
Dispatch.subscribe('DELETE_PENDING_REPLY', this.overhaulCurrentReply);
}
pluginWillUnload() {
window.removeEventListener('keydown', this.keyPressHandler);
Dispatch.unsubscribe('CHANNEL_SELECT', this.overhaulCurrentChannel);
Dispatch.unsubscribe('CREATE_PENDING_REPLY', this.overhaulCurrentReply);
Dispatch.unsubscribe('DELETE_PENDING_REPLY', this.overhaulCurrentReply);
Dispatch.unsubscribe('MESSAGE_CREATE', this.messageUpdater); // More of a double check system
powercord.api.settings.unregisterSettings(this.entityID);
}
}
module.exports = quickChatReply;