(sock, message, args, context)
| 148 | description: 'Convert between 100+ units — length, weight, speed, data, temperature and more', |
| 149 | usage: '.units <value> <from> to <to>\nExample: .units 100 km to miles', |
| 150 | async handler(sock, message, args, context) { |
| 151 | const { chatId, channelInfo } = context; |
| 152 | const input = args.join(' ').trim().toLowerCase(); |
| 153 | if (!input) { |
| 154 | return await sock.sendMessage(chatId, { |
| 155 | text: `📏 *Unit Converter*\n\n` + |
| 156 | `*Usage:* \`.units <value> <from> to <to>\`\n\n` + |
| 157 | `*Examples:*\n` + |
| 158 | `• \`.units 100 km to mi\`\n` + |
| 159 | `• \`.units 70 kg to lb\`\n` + |
| 160 | `• \`.units 37 c to f\`\n` + |
| 161 | `• \`.units 1 gb to mb\`\n` + |
| 162 | `• \`.units 60 mph to kph\`\n` + |
| 163 | `• \`.units 1 yr to day\`\n` + |
| 164 | `• \`.units 1 atm to psi\`\n` + |
| 165 | `• \`.units 500 kcal to kj\`\n\n` + |
| 166 | `*Categories:*\n` + |
| 167 | `📐 length · ⚖️ weight · 🌡️ temperature\n` + |
| 168 | `💨 speed · 💾 data · 📦 volume\n` + |
| 169 | `🗺️ area · ⏱️ time · 🔋 energy · 🌬️ pressure`, |
| 170 | ...channelInfo |
| 171 | }, { quoted: message }); |
| 172 | } |
| 173 | // Parse: <value> <from> to <to> OR <value> <from> <to> |
| 174 | const toIndex = args.findIndex((a) => a.toLowerCase() === 'to'); |
| 175 | let value, fromUnit, toUnit; |
| 176 | if (toIndex === 2 && args.length === 4) { |
| 177 | value = parseFloat(args[0]); |
| 178 | fromUnit = args[1].toLowerCase(); |
| 179 | toUnit = args[3].toLowerCase(); |
| 180 | } |
| 181 | else if (args.length === 3 && toIndex === -1) { |
| 182 | value = parseFloat(args[0]); |
| 183 | fromUnit = args[1].toLowerCase(); |
| 184 | toUnit = args[2].toLowerCase(); |
| 185 | } |
| 186 | else { |
| 187 | return await sock.sendMessage(chatId, { |
| 188 | text: `❌ Wrong format.\n\nUse: \`.units 100 km to mi\``, |
| 189 | ...channelInfo |
| 190 | }, { quoted: message }); |
| 191 | } |
| 192 | if (isNaN(value)) { |
| 193 | return await sock.sendMessage(chatId, { |
| 194 | text: `❌ Invalid number: \`${args[0]}\``, |
| 195 | ...channelInfo |
| 196 | }, { quoted: message }); |
| 197 | } |
| 198 | const res = convert(value, fromUnit, toUnit); |
| 199 | if (!res) { |
| 200 | const fromCat = UNIT_TO_CATEGORY[fromUnit]; |
| 201 | const toCat = UNIT_TO_CATEGORY[toUnit]; |
| 202 | if (!fromCat) { |
| 203 | return await sock.sendMessage(chatId, { |
| 204 | text: `❌ Unknown unit: \`${fromUnit}\`\n\nUse \`.units\` to see all supported units.`, |
| 205 | ...channelInfo |
| 206 | }, { quoted: message }); |
| 207 | } |
nothing calls this directly
no test coverage detected