-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (109 loc) · 3.35 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
const { Plugin } = require('powercord/entities');
const { getModule } = require('powercord/webpack');
const { getMessages } = getModule([ 'getMessages' ], false);
const { getChannelId } = getModule([ 'getLastSelectedChannelId' ], false);
const { getCurrentUser } = getModule([ 'getCurrentUser' ], false);
module.exports = class purge extends Plugin {
startPlugin () {
powercord.api.commands.registerCommand({
command: 'purge',
description: 'Deletes your messages',
usage: '{c} number',
executor: this.handleCommand.bind(this)
});
}
pluginWillUnload () {
powercord.api.commands.unregisterCommand('purge');
}
async handleCommand (args) {
let messagesToDelete;
switch (args.length) {
case 0:
messagesToDelete = 2;
break;
case 1:
messagesToDelete = Number(args[0]) + 1;
break;
case 2:
messagesToDelete = Number(args[0]) + 1;
break;
default:
return {
send: false,
result:
'usage: {c} amount startFrom: messageid'
};
}
if (isNaN(messagesToDelete)) {
return {
send: false,
result:
'it gotta be a number lol'
};
}
const channelid = getChannelId();
const messages = getMessages(channelid)._array;
// check if messages exist
if (messages.length === 0) {
return {
send: false,
result:
'you don\'t have any messages lmao send some first'
};
}
// eslint-disable-next-line prefer-const
let messageArray = await getMessages(channelid)._array.filter(m => m.author.id === getCurrentUser().id);
// reverse order of messages because discord gives them oldest first
await messageArray.reverse();
// for debugging purposes: const message = getMessage(channelid, messageid);
// get messageID to start with
if (args[1]) {
if (args[1].length !== 18 || isNaN(args[1])) {
return {
send: false,
result: 'incorrect messageID'
};
}
const startMessage = messageArray.find(m => m.id === args[1]);
if (!startMessage) {
return {
send: false,
result: 'Incorrect messageID'
};
}
const startMessageIndex = messageArray.findIndex(m => m === startMessage);
messageArray = messageArray.slice(startMessageIndex);
}
if (!messageArray) {
return {
send: false,
result: 'can\'t find messages before that messageid'
};
}
if (messagesToDelete > messageArray.length + 1 || messagesToDelete <= 0) {
return {
send: false,
result:
`input number between 1 and ${messageArray.length}`
};
}
function yeet () {
// i went for as low as 350ms, it worked, added an extra 30 because i feel like 350 is too low lol
return new Promise((res) => {
setTimeout(async () => {
// eslint-disable-next-line prefer-arrow-callback
await require('powercord/webpack').messages.deleteMessage(channelid, messageArray[0].id);
await messageArray.shift();
res();
}, (Math.floor(Math.random() * 4) + 1) * 1000);
});
}
for (let i = 0; i < messagesToDelete - 1; i++) {
await yeet();
}
return {
send: false,
result: `deleted ${messagesToDelete - 1} messages`
};
}
};