onePingPerDM: add settings (#1802)

Co-authored-by: V <vendicated@riseup.net>
Co-authored-by: Dea <dea-banana@riseup.net>
This commit is contained in:
Dea 2023-10-21 16:42:37 +00:00 committed by GitHub
parent fada76ec81
commit e4485165d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,20 +4,44 @@
* SPDX-License-Identifier: GPL-3.0-or-later * SPDX-License-Identifier: GPL-3.0-or-later
*/ */
import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { ChannelStore, ReadStateStore } from "@webpack/common"; import { ChannelStore, ReadStateStore, UserStore } from "@webpack/common";
import { Message } from "discord-types/general"; import { MessageJSON } from "discord-types/general";
const enum ChannelType { const enum ChannelType {
DM = 1, DM = 1,
GROUP_DM = 3 GROUP_DM = 3
} }
const settings = definePluginSettings({
channelToAffect: {
type: OptionType.SELECT,
description: "Select the type of DM for the plugin to affect",
options: [
{ label: "Both", value: "both_dms", default: true },
{ label: "User DMs", value: "user_dm" },
{ label: "Group DMs", value: "group_dm" },
]
},
allowMentions: {
type: OptionType.BOOLEAN,
description: "Receive audio pings for @mentions",
default: false,
},
allowEveryone: {
type: OptionType.BOOLEAN,
description: "Receive audio pings for @everyone and @here in group DMs",
default: false,
},
});
export default definePlugin({ export default definePlugin({
name: "OnePingPerDM", name: "OnePingPerDM",
description: "If unread messages are sent by a user in DMs multiple times, you'll only receive one audio ping. Read the messages to reset the limit", description: "If unread messages are sent by a user in DMs multiple times, you'll only receive one audio ping. Read the messages to reset the limit",
authors: [Devs.ProffDea], authors: [Devs.ProffDea],
settings,
patches: [{ patches: [{
find: ".getDesktopType()===", find: ".getDesktopType()===",
replacement: [{ replacement: [{
@ -29,11 +53,19 @@ export default definePlugin({
replace: "sound:!$self.isPrivateChannelRead(arguments[0]?.message)?undefined:$1" replace: "sound:!$self.isPrivateChannelRead(arguments[0]?.message)?undefined:$1"
}] }]
}], }],
isPrivateChannelRead(message: Message) { isPrivateChannelRead(message: MessageJSON) {
const channelType = ChannelStore.getChannel(message.channel_id)?.type; const channelType = ChannelStore.getChannel(message.channel_id)?.type;
if (channelType !== ChannelType.DM && channelType !== ChannelType.GROUP_DM) { if (channelType !== ChannelType.DM && channelType !== ChannelType.GROUP_DM) {
return false; return false;
} }
if (
(channelType === ChannelType.DM && settings.store.channelToAffect === "group_dm") ||
(channelType === ChannelType.GROUP_DM && settings.store.channelToAffect === "user_dm") ||
(settings.store.allowMentions && message.mentions.some(m => m.id === UserStore.getCurrentUser().id)) ||
(settings.store.allowEveryone && message.mention_everyone)
) {
return true;
}
return ReadStateStore.getOldestUnreadMessageId(message.channel_id) === message.id; return ReadStateStore.getOldestUnreadMessageId(message.channel_id) === message.id;
}, },
}); });