Logo OmniDevvKANA Web
Kembali ke katalog plugin

user

kalkulator

Hitung ekspresi matematika sederhana dengan aman

Safely calculate a simple math expression

plugins/user/kalkulator.js

Lokasi file di repo kana-md

2

Perintah termasuk alias

2.0 KB

Ukuran file sumber

4/11/2026

Update terakhir, commit 44f4010

Daftar perintah

Ketuk tombol salin untuk menyalin perintah, lalu tempel di chat WhatsApp bot KANA.

.kalkulator.kalk

Cara pakai

.kalkulator <ekspresi>

Contoh

.kalkulator (12+8)*3

Kode sumber plugin

Unduh .js

Cara pasang: simpan file ini ke folder plugins/user/ di bot KANA kamu, lalu restart bot. Pemilik source code lain bisa mengambil update plugin terbaru dari halaman ini kapan saja.

import { sendButtons } from '../../src/ui/buttons.js'
import { evaluateExpression, formatValue } from '../../src/services/calculator.js'

export const config = {
  name: 'kalkulator',
  alias: ['kalk'],
  category: 'user',
  description: { id: 'Hitung ekspresi matematika sederhana dengan aman', en: 'Safely calculate a simple math expression' },
  usage: '.kalkulator <ekspresi>',
  example: '.kalkulator (12+8)*3',
  cooldown: 3,
}

export const i18n = {
  id: {
    kalkulator: {
      usage: 'Tulis ekspresinya yaa, contoh: .kalkulator (12+8)*3. Bisa pakai + - * / % ^ dan kurung~ 🧮',
      result: '🧮 {expr} = {value}\n\n{mood}',
      divZero: 'Ehh, ada pembagian dengan nol tuh, itu nggak bisa yaa (╥﹏╥)',
      invalid: 'Ekspresinya belum bener nih. Pakai angka, + - * / % ^, titik desimal, dan kurung aja yaa~',
      btnMenu: 'Menu User',
    },
  },
  en: {
    kalkulator: {
      usage: 'Write the expression, example: .kalkulator (12+8)*3. You can use + - * / % ^ and parentheses~ 🧮',
      result: '🧮 {expr} = {value}\n\n{mood}',
      divZero: 'Eh, there is a division by zero there, that is not allowed (╥﹏╥)',
      invalid: 'The expression is not right yet. Use only numbers, + - * / % ^, decimal points, and parentheses~',
      btnMenu: 'User Menu',
    },
  },
}

export const test = { input: '.kalkulator (12+8)*3', expect: /= 60/i }

export async function handler(ctx) {
  const expr = ctx.text.trim()
  if (!expr) return ctx.reply(ctx.t('kalkulator.usage'))
  const result = evaluateExpression(expr)
  if (!result.ok) {
    return ctx.reply(ctx.t(result.reason === 'div-zero' ? 'kalkulator.divZero' : 'kalkulator.invalid'))
  }
  const body = ctx.t('kalkulator.result', { expr, value: formatValue(result.value), mood: ctx.say('senang') })
  return sendButtons(ctx.sock, ctx.chat, {
    title: 'KANA',
    body,
    footer: ctx.t('menu.footer'),
    replies: [{ text: ctx.t('kalkulator.btnMenu'), id: '.menu user' }],
    textFallback: body,
  })
}