This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
98 lines (83 loc) · 3.27 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
const { getModule, React } = require('powercord/webpack');
const { inject, uninject } = require('powercord/injector');
const { Plugin } = require('powercord/entities');
const shouldDisplayNotifications = getModule([ 'shouldDisplayNotifications' ], false);
const Settings = require('./components/Settings');
const MsgContent = require('./components/MsgContent');
module.exports = class InAppNotifications extends Plugin {
async startPlugin() {
this.loadStylesheet('./style.css');
powercord.api.settings.registerSettings('ian-settings', {
category: this.entityID,
label: 'In App Notifications',
render: Settings
});
try {
const show = getModule([ 'makeTextChatNotification' ], false);
const transition = getModule([ 'transitionTo' ], false);
const { getGuild } = getModule([ 'getGuild' ], false);
const { ack } = getModule([ 'ack', 'ackCategory' ], false);
let toasts = [];
inject('ian', show, 'makeTextChatNotification', (args) => {
const [ channel, msg, author ] = args;
const onPing = this.settings.get('notifyPing', false);
const sticky = this.settings.get('sticky', false);
const timeMult = this.settings.get('timeMult', 1);
const toast = `ian-${(Math.random().toString(36) + Date.now()).substring(2, 7)}`;
toasts.push(toast);
const guild = getGuild(channel.guild_id);
const time = sticky ? null : timeMult * Math.min(Math.max(msg.content.split(' ').length * 0.5e3, 4e3), 10e3);
if (!msg.mentions.find(user => user.id === getModule([ 'getCurrentUser' ], false).getCurrentUser().id)
&& onPing) {
return args;
}
powercord.api.notices.sendToast(toast, {
header: `${author.username} ${msg.referenced_message ? 'replied' : ''} ${guild ? `in ${guild.name}` : 'in DM\'s'}`,
timeout: time,
image: `https://cdn.discordapp.com/avatars/${author.id}/${author.avatar}.png`,
imageClassName: 'ian-pfp',
content: React.createElement(MsgContent, { msg }),
buttons: [ {
text: toasts.length > 1 ? 'Dismiss all' : 'Dismiss',
look: 'ghost',
size: 'small',
onClick: () => {
toasts.forEach((id) => powercord.api.notices.closeToast(id));
}
}, {
text: 'Mark as read',
look: 'ghost',
size: 'small',
onClick: () => ack(channel.id)
}, {
text: `Jump to ${guild ? `#${channel.name}` : 'DM\'s'}`,
look: 'outlined',
size: 'small',
onClick: () => transition.transitionTo(`/channels/${guild ? guild.id : '@me'}/${channel.id}/${msg.id}`)
} ]
});
return args;
}, true);
inject('ian-desktop-blocker', shouldDisplayNotifications, 'shouldDisplayNotifications', (args) => {
const blockDesktop = this.settings.get('blockDesktop', false);
if (blockDesktop && document.hasFocus()) {
return false;
}
return args;
}, true);
powercord.api.notices.on('toastLeaving', (toastID) => {
if (toastID.startsWith('ian-')) {
toasts = toasts.filter((id) => id !== toastID);
}
});
} catch (error) {
this.error(`There seems to have been a problem with the in app notifications. Please report this to the developer.\n\n${error}`);
}
}
pluginWillUnload() {
uninject('ian');
uninject('ian-desktop-blocker');
uninject('ian-embed-test');
powercord.api.settings.unregisterSettings('ian-settings');
}
};