Skip to content

Commit

Permalink
feat: ✨ Add web API and remove useless Discord functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
TheManchineel committed Dec 14, 2023
1 parent 5a48b15 commit eed6703
Show file tree
Hide file tree
Showing 7 changed files with 715 additions and 15 deletions.
34 changes: 20 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const qrcode = require('qrcode-terminal');
const { Client, LocalAuth } = require('whatsapp-web.js');

const constants = require('./constants.js');
const { getRandomJoinMessage } = require('./join_messages.js');
const { nconf, getBrowserPath } = require('./config.js');
const constants = require('./constants.js');
const { Handlers } = require('./dispatchers.js');
const { WebAPI } = require('./web_api.js');

var wwversion

Expand Down Expand Up @@ -32,17 +34,6 @@ client.on('ready', () => {
console.log('Client is ready!');
});

client.initialize().then(
async () => {
wwversion = await client.getWWebVersion()
}).then(() => { console.log(`\n${constants.SPLASH}\n\nWhatsApp Web Version: ${wwversion}\nServer time: ${new Date().toISOString()}`) });

async function logMessage(message) {
const contact = await message.getContact();
const chat = await message.getChat();
console.log(`${new Date().toISOString()} ${contact.pushname} (${contact.id.user} @ ${message.from}): ${message.body}`);
};

function messageFilter(message) {
if (message.type !== 'chat') {
console.log(`Message of type ${message.type} from ${message.from} filtered out`);
Expand All @@ -67,6 +58,12 @@ async function errorHandler(error) {
console.log(`++++++++++++++ USER ENCOUNTERED ERROR AT ${timestamp}: ++++++++++++++\n${error}`);
}

async function logMessage(message) {
const contact = await message.getContact();
const chat = await message.getChat();
console.log(`${new Date().toISOString()} ${contact.pushname} (${contact.id.user} @ ${message.from}): ${message.body}`);
};

async function onMessage(message) {
if (messageFilter(message)) {
await logMessage(message);
Expand All @@ -86,7 +83,7 @@ async function onGroupJoin(groupNotification) {
const participant = groupNotification.id.participant;
const chat_id = chat.id._serialized;
if (nconf.get("CHAT_WHITELIST").includes(chat_id)) {
const replyMessage = `${getRandomJoinMessage()}\n\nCiao @${participant.split('@')[0]} 👋, benvenutə in ${chat.name}!\n${nconf.get("DISCORD_URL")} è il nostro server Discord`
const replyMessage = `${getRandomJoinMessage()}\n\nCiao @${participant.split('@')[0]} 👋, benvenutə in ${chat.name}!`
const mentions = { id: { _serialized: participant } };
await groupNotification.reply(replyMessage, { mentions: [mentions] });
}
Expand All @@ -102,4 +99,13 @@ client.on('group_join', onGroupJoin)

client.on('disconnected', () => {
console.log('Disconnected!');
});
});

client.initialize()
.then(
async () => {
wwversion = await client.getWWebVersion()
console.log(`\n${constants.SPLASH}\n\nWhatsApp Web Version: ${wwversion}\nServer time: ${new Date().toISOString()}`)
})
.then(() => new WebAPI(client, nconf)
);
3 changes: 2 additions & 1 deletion data/data.json.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"RATE_LIMIT_EVERYONE": 28800,
"CHAT_WHITELIST": [],
"DISCORD_URL": "https://discord.gg/1234",
"COMMAND_PREFIX": "/",
"BLACKLIST": [],
"BOT_ADMINS": [],
"API_PORT": 3000,
"API_KEY": "abcd1234",
"groups": {
"EXAMPLE": {
"members": [],
Expand Down
1 change: 1 addition & 0 deletions dispatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Handlers {
}
};
}
console.log("Handlers loaded");
}
async commandDispatcher(client, message, command, args, nconf) {
if (this.commands[command]) {
Expand Down
Loading

0 comments on commit eed6703

Please sign in to comment.