Skip to content

Commit c3471c2

Browse files
committed
Added one-way comm. Closes #176
1 parent e8f47d6 commit c3471c2

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

docs/commands.md

+6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ Enables profile picture change and status update notifications.
118118
Disables profile picture change and status update notifications.
119119
- Format: `disableChangeNotifications`
120120

121+
## oneWay
122+
Turns on one-way communication.
123+
- Format: `oneWay <discord|whatsapp|disabled>`
124+
- Examples:
125+
- `oneWay discord`: would only send messages coming from WhatsApp to Discord, but not the other way.
126+
121127
## autoSaveInterval
122128
Changes the auto save interval to the number of seconds you provide.
123129
- Format: `autoSaveInterval <seconds>`

src/discordHandler.js

+31
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ client.on('channelDelete', async (channel) => {
2929
});
3030

3131
client.on('whatsappMessage', async (message) => {
32+
if ((state.settings.oneWay >> 0 & 1) === 0) {
33+
return;
34+
}
35+
3236
let msgContent = '';
3337
const files = [];
3438
const webhook = await utils.discord.getOrCreateChannel(message.channelJid);
@@ -85,6 +89,10 @@ client.on('whatsappMessage', async (message) => {
8589
});
8690

8791
client.on('whatsappReaction', async (reaction) => {
92+
if ((state.settings.oneWay >> 0 & 1) === 0) {
93+
return;
94+
}
95+
8896
const channelId = state.chats[reaction.jid]?.channelId;
8997
const messageId = state.lastMessages[reaction.id];
9098
if (channelId == null || messageId == null) { return; }
@@ -99,6 +107,10 @@ client.on('whatsappReaction', async (reaction) => {
99107
});
100108

101109
client.on('whatsappCall', async ({ call, jid }) => {
110+
if ((state.settings.oneWay >> 0 & 1) === 0) {
111+
return;
112+
}
113+
102114
const webhook = await utils.discord.getOrCreateChannel(jid);
103115

104116
const name = utils.whatsapp.jidToName(jid);
@@ -306,6 +318,25 @@ const commands = {
306318
state.settings.lastMessageStorage = +params[0];
307319
await controlChannel.send(`Changed last message storage size to ${params[0]}.`);
308320
},
321+
async oneway(_message, params) {
322+
if (params.length !== 1) {
323+
await controlChannel.send("Usage: oneWay <discord|whatsapp|disabled>\nExample: oneWay whatsapp");
324+
return;
325+
}
326+
327+
if (params[0] === "disabled") {
328+
state.settings.oneWay = 0b11;
329+
await controlChannel.send(`Two way communication is enabled.`);
330+
} else if (params[0] === "whatsapp") {
331+
state.settings.oneWay = 0b10;
332+
await controlChannel.send(`Messages will be only sent to WhatsApp.`);
333+
} else if (params[0] === "discord") {
334+
state.settings.oneWay = 0b01;
335+
await controlChannel.send(`Messages will be only sent to Discord.`);
336+
} else {
337+
await controlChannel.send("Usage: oneWay <discord|whatsapp|disabled>\nExample: oneWay whatsapp");
338+
}
339+
},
309340
async unknownCommand(message) {
310341
await controlChannel.send(`Unknown command: \`${message.content}\`\nType \`help\` to see available commands`);
311342
},

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const storage = require('./storage.js');
77
const whatsappHandler = require('./whatsappHandler.js');
88

99
(async () => {
10-
const version = 'v0.10.23';
10+
const version = 'v0.10.24';
1111
state.logger = pino({ mixin() { return { version }; } }, pino.destination('logs.txt'));
1212
let autoSaver = setInterval(() => storage.save(), 5 * 60 * 1000);
1313
['SIGINT', 'uncaughtException', 'SIGTERM'].forEach((eventName) => process.on(eventName, async (err) => {

src/state.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
ChangeNotifications: false,
1717
autoSaveInterval: 5 * 60,
1818
lastMessageStorage: 500,
19+
oneWay: 0b11
1920
},
2021
dcClient: null,
2122
waClient: null,

src/whatsappHandler.js

+12
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ const connectToWhatsApp = async (retry = 1) => {
140140
});
141141

142142
client.ev.on('discordMessage', async ({ jid, message }) => {
143+
if ((state.settings.oneWay >> 1 & 1) === 0) {
144+
return;
145+
}
146+
143147
const content = {};
144148
const options = {};
145149

@@ -170,6 +174,10 @@ const connectToWhatsApp = async (retry = 1) => {
170174
});
171175

172176
client.ev.on('discordEdit', async ({ jid, message }) => {
177+
if ((state.settings.oneWay >> 1 & 1) === 0) {
178+
return;
179+
}
180+
173181
const key = {
174182
id: state.lastMessages[message.id],
175183
fromMe: message.webhookId == null || message.author.username === 'You',
@@ -190,6 +198,10 @@ const connectToWhatsApp = async (retry = 1) => {
190198
});
191199

192200
client.ev.on('discordReaction', async ({ jid, reaction, removed }) => {
201+
if ((state.settings.oneWay >> 1 & 1) === 0) {
202+
return;
203+
}
204+
193205
const key = {
194206
id: state.lastMessages[reaction.message.id],
195207
fromMe: reaction.message.webhookId == null || reaction.message.author.username === 'You',

0 commit comments

Comments
 (0)