Last active
February 3, 2025 13:53
-
-
Save SoursopID/d8e572cd4aeb761569b7fd8c2cd16419 to your computer and use it in GitHub Desktop.
Contoh sederhana pembuatan handler bebasis pattern / cmd yg didaftarkan.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright(C) 2025 SoursopID | |
// This Source Code Form is subject to the terms of the Mozilla Public | |
// License, v. 2.0. If a copy of the MPL was not distributed with this | |
// file, You can obtain one at <https://mozilla.org/MPL/2.0/>. | |
const DEFAULT_PREFIX = "/."; | |
// Kumpulan command yang terdaftar | |
let commands = {}; | |
// Daftarin command baru | |
function NewCMD(pattern, prefix, func) { | |
if (prefix) { | |
for (let i in DEFAULT_PREFIX) { | |
let p = DEFAULT_PREFIX[i] + pattern; | |
commands[p.toLowerCase()] = func; | |
} | |
} else { | |
commands[pattern.toLowerCase()] = func; | |
} | |
} | |
// Handler event message mencari cmd/pattern yg cocok di dalam variabel commands | |
function HandleMessage(mm) { | |
if (commands[mm.pattern?.toLowerCase()]) { | |
commands[mm.pattern?.toLowerCase()](mm); | |
return; | |
} | |
} | |
module.exports = { NewCMD, HandleMessage }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright(C) 2025 SoursopID | |
// This Source Code Form is subject to the terms of the Mozilla Public | |
// License, v. 2.0. If a copy of the MPL was not distributed with this | |
// file, You can obtain one at <https://mozilla.org/MPL/2.0/>. | |
const { NewCMD, HandleMessage } = require("./cmd-handler.js"); | |
// Contoh mendaftarkan command baru | |
// 1. cmd/pattern nya "hello" | |
// 2. prefixnya true (jadi akan menggunakan prefix default) | |
// maka bakal jadi ".hello" sama "/hello" | |
// 3. function yg akan dijalankan ketika command ".hello" atau "/hello" ditemukan | |
NewCMD("hello", true, function (mm) { | |
console.log("Hello, " + mm.pushName + "!", "\nwith args: ", mm.args); | |
}); | |
// Contoh isi event upsert yg udah di serialize | |
const newMM = { | |
pushName: "John", | |
pattern: ".hello", | |
args: "argsA argsB argsC" | |
} | |
// Kirim ke handler | |
HandleMessage(newMM); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment