Closed Gregory280 closed 2 years ago
I came up with a solution using regex. This way I can retrieve the argument just when its matching a command '/code' in the message.
when \/\code/
code = message.text.delete('^0-9')
if message.text == "/code #{code}"
# do something
end
And to prevent calling the command in any message that contains this regex I used a if statement. Its that a good way of doing things?
Hey @Gregory280,
Using regex is totally fine. You can go further and match code value right in when ...
statement:
case message.text
when /^\/code ([0-9]+)$/
puts "Code is #{$1}" # $1 refers matched group
else
puts "No match"
end
This work like a charm thanks.
I can't use this on other commands that requires words for arguments like "/meme programming". How would you recommend in this case?
Why not, just change regexp to match your needs. For /meme programming
something like this would work: ^\/meme\s+(\w+)$
Thanks. I got review regex.
What is the correct way to create commands that require arguments? I have stumble on creating a command that requires an argument to use through an API. I could successfully save the user input, but I think thats not the correct way.
Thats works but I'm saving something everytime that has a new message. And how works if multiple commands that requires arguments? Would I do the same ?