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.