-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
126 lines (102 loc) · 3.64 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
/* eslint-disable semi */
const { open } = require('powercord/modal')
const { Plugin } = require('powercord/entities')
const { inject, uninject } = require('powercord/injector')
const { getModule, channels, messages, React, constants: { Permissions } } = require('powercord/webpack')
const channelObj = getModule(['getChannel', 'getDMFromUserId'], false)
const { getGuild } = getModule(['getGuild'], false);
const { default: { getMember } } = getModule(m => m.default && m.default.getMember, false);
const Modal = require('./components/modal.jsx')
module.exports = class doNotSlowmode extends Plugin {
async startPlugin() {
this._injectMessageSent()
await this.import('getCurrentUser')
powercord.api.settings.registerSettings('DNSM!', {
category: this.entityID,
label: 'Do Not Slowmode Me',
render: require('./components/Settings.jsx')
})
}
_injectMessageSent() {
inject('dontSlowmodeMe', messages, 'sendMessage', (args) => {
// Avoid Unnecessary Checks if there is no Slowmode
if (!this.cooldownTime()) return args;
const guildID = channelObj.getChannel(channels.getChannelId()).guild_id;
let permissions = this.getPermissionsRaw(
getGuild(guildID),
this.getCurrentUser().id
);
let parsedPermissions = this.parseBitFieldPermissions(permissions);
// There is no need to show a notice if the user has perms that bypass the slowmode
if (parsedPermissions['MANAGE_MESSAGES'] || parsedPermissions['MANAGE_CHANNELS']) return args;
if (this.cooldownTime() < this.settings.get('slowmodeTrigger', '600')) return args;
console.log(args)
if (!args[1]?.__DNSM_afterWarn) {
open(() => React.createElement(Modal, {
slowmode: this.cooldownTime(),
channel: channels.getChannelId(),
message: args[1],
other: args[2],
reply: args[3]
}));
return false;
}
return args;
}, true);
}
parseBitFieldPermissions(allowed) {
const permissions = {};
for (const perm of Object.keys(Permissions)) {
if (!perm.startsWith('all')) {
if (BigInt(allowed) & BigInt(Permissions[perm])) {
permissions[perm] = true;
}
}
}
return permissions
};
getPermissionsRaw(guild, user_id) {
if (!guild) return false;
let permissions = 0n;
const member = getMember(guild.id, user_id);
if (guild && member) {
if (guild.ownerId === user_id) {
permissions = BigInt(Permissions.ADMINISTRATOR);
} else {
/* @everyone is not inlcuded in the member's roles */
permissions |= BigInt(guild.roles[guild.id]?.permissions);
for (const roleId of member.roles) {
const rolePerms = guild.roles[roleId]?.permissions;
if (rolePerms !== undefined) {
permissions |= BigInt(rolePerms);
}
}
}
/* If they have administrator they have every permission */
if (
(BigInt(permissions) & BigInt(Permissions.ADMINISTRATOR)) ===
BigInt(Permissions.ADMINISTRATOR)
) {
return Object.values(Permissions).reduce(
(a, b) => BigInt(a) | BigInt(b),
0n
);
}
}
return permissions;
}
cooldownTime() {
const channel = channels.getChannelId();
return channelObj.getChannel(channel).rateLimitPerUser
}
async import(filter, functionName = filter) {
if (typeof filter === 'string') {
filter = [filter];
}
this[functionName] = (await getModule(filter))[functionName];
}
pluginWillUnload() {
uninject('dontSlowmodeMe')
powercord.api.settings.unregisterSettings('DNSM!')
}
}