cmbot
cmbot copied to clipboard
Modules writing to settings.json (or similar solution)
Is there a format in place for modules to write to settings.json or something similar?
e.g. for tracking a DJ's success (awesomes/lames/snags) over their entire set
Depending on the module, I suppose there's no actual need to write - only if the data needs to persist across sessions.
Custom events receive a 'cmbot' variable, and custom commands have access to the same variable as options.cmbot. In this variable is a 'settings' variable, i.e. cmbot.settings (or options.cmbot.settings, for custom commands), that stores persistent settings that the bot loads each time it runs. If you just add new properties to this object and then save it, your bot will persist that data. Like so (in the case of a custom command):
options.cmbot.settings.somevar = { foo: bar }; options.cmbot.saveSettings(); // This is a method that writes out the settings object to settings.json
For a custom event, use cmbot instead of options.cmbot.
I'd suggest you inspect the settings object before you start adding to it, to make sure you don't overwrite anything the core bot uses, i.e. console.log("Settings: ", options.cmbot.settings);
Take a look at the greeting.js example module - it does all this.
Let me know if you need more help!
greeting.js got me a long way until my script started throwing a type error; then I realized greeting.js is throwing the same error on line 23.
Cannot read property '4e0ac44ba3f751466f02xxxx' of undefined at Object.customEvents.event
if((new Date().getTime() - cmbot.settings.greeting_last[data.user[0].userid]) <= 1000_60_60)
It's clearly grabbing the userid, though.
Hmm weird, cmbot.settings.greeting_last should be an empty object, already defined on line 15. I'll have to take a closer look to see why it's undefined.