0

I need to have a command that asks a user questions, and records their response in pm, but I am unsure how could possibly do that on cinch thanks to its thread based commands. Something like this

!profile create
Bot: Please tell me your age
27
Bot: Thank you. Please tell me your gender.
Female
Bot: Okay. Please tell me your location. Where do you live?
Somewhere

Etc. I am stuck on how I would be able to do this, if at all with cinch.

KatGaea
  • 996
  • 1
  • 12
  • 31
  • To be honest, where to start as I cant find anything in cinch docs about a way for it to save state inbetween commands. I cant use variables as of course cinch runs each command as a thread that gets destroyed after the function returns – KatGaea Apr 21 '15 at 13:46
  • You can use instance-level variables in your plugins to save state. Cinch doesn't destroy the Plugin class until it's told to disconnect – Mario May 16 '15 at 05:19

1 Answers1

0

Use Unique Prefixes for Each Setting

Because Cinch is threaded, the easiest thing to do is make your key/value pairs idempotent. For example, running !setup could prompt the user to enter prefixed values, each of which is handled as a separate event rather than chained together. Consider the following:

# Reply to `!setup` with list of async prefixes.
on :message, /^!setup/ do |m|
  m.reply "Set age with '!age'"
  m.reply "Set location with '!loc'"
end

on :message, /^!age\s+(\d+)/ do |m|
  m.reply "Age: #{$1}"
end

on :message, /^!loc\s+(.*)/ do |m|
  m.reply "Location: #{$1.strip}"
end

While you could certainly chain your prompts by making each setting prompt for the next key/value pair, you'll make life easier on yourself and your users by responding to !setup with a list of asynchronous commands that can be handled in any order.

You still have to ensure that each event writes to a collection in a thread-safe manner, and that you serialize the collection at some point. In the meantime, this should definitely get you headed in the right direction.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199