MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / handler

Function handler

plugins/compliment.js:39–92  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

37 description: 'Send a random compliment to a user',
38 usage: '.compliment @user',
39 async handler(sock, message, args, context) {
40 const chatId = context.chatId || message.key.remoteJid;
41 try {
42 if (!message || !chatId) {
43 console.log('Invalid message or chatId:', { message, chatId });
44 return;
45 }
46 let userToCompliment;
47 if (message.message?.extendedTextMessage?.contextInfo?.mentionedJid?.length > 0) {
48 userToCompliment =
49 message.message.extendedTextMessage.contextInfo.mentionedJid[0];
50 }
51 else if (message.message?.extendedTextMessage?.contextInfo?.participant) {
52 userToCompliment =
53 message.message.extendedTextMessage.contextInfo.participant;
54 }
55 if (!userToCompliment) {
56 await sock.sendMessage(chatId, {
57 text: 'Please mention someone or reply to their message to compliment them!'
58 }, { quoted: message });
59 return;
60 }
61 const compliment = compliments[Math.floor(Math.random() * compliments.length)];
62 await new Promise(resolve => setTimeout(resolve, 1000));
63 await sock.sendMessage(chatId, {
64 text: `Hey @${userToCompliment.split('@')[0]}, ${compliment}`,
65 mentions: [userToCompliment]
66 }, { quoted: message });
67 }
68 catch (error) {
69 console.error('Error in compliment command:', error);
70 if (error?.data === 429) {
71 await new Promise(resolve => setTimeout(resolve, 2000));
72 try {
73 await sock.sendMessage(chatId, {
74 text: 'Please try again in a few seconds.'
75 }, { quoted: message });
76 }
77 catch (retryError) {
78 console.error('Error sending retry message:', retryError);
79 }
80 }
81 else {
82 try {
83 await sock.sendMessage(chatId, {
84 text: 'An error occurred while sending the compliment.'
85 }, { quoted: message });
86 }
87 catch (sendError) {
88 console.error('Error sending error message:', sendError);
89 }
90 }
91 }
92 }
93};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected