Bots Home
|
Create an App
test roxi's tip manager4
Author:
dopender
Description
Source Code
Launch Bot
Current Users
Created by:
Dopender
(function (factory) { typeof define === 'function' && define.amd ? define(factory) : factory(); }((function () { 'use strict'; const defaultTheme = { DEFAULT: { BG: '#FFF', TEXT: '#0F0F0F', WEIGHT: 'normal', }, }; const SETTINGS_PREFIX = 'app_settings_'; const SETTINGSKEY_DEFAULT_BG = `${SETTINGS_PREFIX}DEFAULT_BG`; const SETTINGSKEY_DEFAULT_TEXT = `${SETTINGS_PREFIX}DEFAULT_TEXT`; const SETTINGSKEY_DEFAULT_WEIGHT = `${SETTINGS_PREFIX}DEFAULT_WEIGHT`; const themeSettings = [ { name: SETTINGSKEY_DEFAULT_BG, type: 'str', defaultValue: defaultTheme.DEFAULT.BG, label: 'Default app bg', }, { name: SETTINGSKEY_DEFAULT_TEXT, type: 'str', defaultValue: defaultTheme.DEFAULT.TEXT, label: 'Default app text', }, { name: SETTINGSKEY_DEFAULT_WEIGHT, type: 'str', defaultValue: defaultTheme.DEFAULT.WEIGHT, label: 'Default app weight', }, ]; class BotApp { constructor(cb) { this._cb = cb; this._modules = []; this._settings = themeSettings; this._mishMashSettings = (s) => s; this._theme = Object.assign({}, defaultTheme); } get settings() { return this._cb.settings } get modules() { return this._modules } get theme() { return this._theme } set theme(theme) { this._theme = Object.assign(this._theme, theme); } addSettingsMishMasher(func) { this._mishMashSettings = func; return this } addModules(modules) { const app = this; modules.forEach((Module) => { app._modules.push(new Module(app)); }); return app } buildApp() { const app = this; this.modules.forEach((_module) => { app._settings.push(_module.Settings); }); this._cb.settings_choices = this._settings; } initModules() { this.modules.forEach((_module) => { _module.init(); }); } start() { // this.sendNotice(`this ${this.theme}`) // this.initApp() this.buildApp(); this._cb.onStart(() => { this.initModules(); }); this.handleMessages(); this.handleTips(); } handleMessages() { this._cb.onMessage((message) => { message = this.onCommand(message); message = this.onMessage(message); return message }); } onCommand(message) { // x-spam not checked outside of loop because // message might be spammed by other bots but this // bot should still process it, for (let mod of this.modules) { if (message['X-Spam']) { break } if (mod.CommandParser && mod.CommandParser.isCommand(message)) { message = mod.CommandCommandParser.onCommand(message); } } return message } onMessage(message) { if (message['X-Spam']) { return message } for (let mod of this.modules) { if (message['X-Spam']) { break } if (mod.onMessage && typeof mod.onMessage === 'function') { message = mod.CommandCommandParser.onMessage(message); } } return message } handleTips() { this._cb.onTip((tip) => { for (let mod of this.modules) { if (mod.onTip && typeof mod.onTip === 'function') { mod.onTip(tip); } } }); } sendNotice(msg, forUser = '', theme = null) { this._cb.chatNotice(this.theme); if (theme === null || theme === undefined) { theme = this.theme.DEFAULT; } this._cb.sendNotice(msg, forUser, theme.BG, theme.TEXT, theme.WEIGHT); } } class BotModule { constructor(app) { this.app = app; } get Commands() { return {} } get CommandParser() { return null } get Settings() { return [] } get Theme() { return {} } init() {} sendNotice(msg, forUser = '', theme = null) { if (theme === null || theme === undefined) { theme = this.Theme.DEFAULT; } this.app.sendNotice(msg, forUser, theme); } } // themes const THEME = { DEFAULT: { BG: '#fff1ec', TEXT: '#ff00cc', WEIGHT: 'bold', }, ALT: { DEFAULT: { BG: '#ff00cc', TEXT: '#fff1ec', WEIGHT: 'bolder', }, }, }; const SHOW_MENU = 'tipmenu'; class CommandParser { constructor(app, module) { this.app = app; this.module = module; } get CMD_PREFIX() { return '/' } get Commands() { return { SHOW_MENU } } clean(cmd) { if (cmd.length < 1) { return cmd } if (cmd.startsWith(this.CMD_PREFIX)) { cmd = cmd.substring(1, cmd.length); } return cmd } isCommand(cmd) { return Object.keys(this.Commands).some( (key) => this.Commands[key] === this.clean(cmd) ) } onCommand(cmd, forUser) { if (!this.isCommand(cmd)) { return false } switch (this.clean(cmd)) { case SHOW_MENU: this.module.print(forUser); break } } } const ENV = { totalMenuItems: 20, }; const L = { ES: { ADMIN: { LABELS: { TIP_ITEM: 'Articulo #$no', }, }, }, }; const ITEM_PREFIX = 'tipitem_'; const getSettings = () => { const tipItemsSettings = [ { name: getTipId(1), type: 'str', defaultValue: '10--Kiss Me', label: getTipLabel(1), required: false, }, ]; for (let i = 2; i <= ENV.totalMenuItems; i++) { tipItemsSettings.push({ name: getTipId(i), type: 'str', label: getTipLabel(i), required: false, }); } return tipItemsSettings }; function getTipId(id) { return `${ITEM_PREFIX}_${id}` } function getTipLabel(suff) { return L.ES.ADMIN.LABELS.TIP_ITEM.replace('$no', suff) } class TipMenu extends BotModule { constructor(app) { super(app); this._cmdParser = new CommandParser(app, this); } get Commands() { return this._cmdParser.Commands } get CommandParser() { return this._cmdParser } get Settings() { return getSettings() } get Theme() { return THEME } get Menu() { return this._menu } init() { this._menu = {}; this.constructMenu(); this.print(); } /** * contruct Menu object from bot settings */ constructMenu() { const itemKeys = this.getMenuItemKeys(); for (let itemKey of itemKeys) { const [amount, task] = this.app.settings[itemKey].split('--'); this._menu[amount] = task; } } /** * print Menu */ print(forUser = '') { this.sendNotice(`:heart7 Roxi's Tip Menu :heart7`, forUser, this.Theme.ALT); const msg = this.toString() + `\n\nType /${this.Commands.SHOW_MENU} to display tip menu.`; this.sendNotice(msg, forUser); } getMenuItemKeys() { const itemKeys = Object.keys(this.app.settings).filter( (key) => key.startsWith(ITEM_PREFIX) && this.app.settings[key] ); return itemKeys } } // prototypes TipMenu.prototype.toString = function () { let msg = ''; for (let tip in this.Menu) { msg += `(${tip}) ${this.Menu[tip]}\n`; } msg = msg.substring(0, msg.length - 1); return msg }; // eslint-disable-next-line no-undef new BotApp(cb).addModules([TipMenu]).start(); })));
© Copyright Chaturbate 2011- 2024. All Rights Reserved.