diff --git a/examples/bot_api.rb b/examples/bot_api.rb index 0d2d955f5e76b39f1d5b03fe2d9368cbe524fa6c..9fefa4a14ea4aad8be026962cd1f2a60586fada7 100755 --- a/examples/bot_api.rb +++ b/examples/bot_api.rb @@ -3,7 +3,7 @@ # An example of a lightweight bot using the bot DSL # -# This bot will implement a subset of the maubot ping/echo module +# This bot will implement an intersection of the maubot ping/echo module # It showcases required and optional parameters, as well as limitations on commands require 'matrix_sdk/bot' @@ -13,17 +13,6 @@ module Utils; end set :bot_name, 'examplebot' -command :spam, only: :dm, desc: 'Spams a bunch of nonsense' do |message_count = nil| - message_count ||= 5 - message_count_i = message_count.to_i - raise ArgumentError, 'Message count must be an integer' if message_count_i.to_s != message_count.to_s - - spam = message_count_i.times.map { rand(10..30).times.map { rand(65..91).chr }.join } - spam.each do |msg| - room.send_notice(msg) - end -end - command(:thumbsup, desc: 'Gives you a thumbs up', only: -> { room.user_can_send? client.mxid, 'm.reaction' }) do room.send_event 'm.reaction', { 'm.relates_to': { @@ -34,6 +23,16 @@ command(:thumbsup, desc: 'Gives you a thumbs up', only: -> { room.user_can_send? } end +command :multiply, only: :dm, desc: 'Performs a multiplication of two numbers' do |num_a, num_b| + num_rex = /^-?\d+(\.\d+)?$/ + raise ArgumentError, 'Both arguments must be numbers' unless num_rex.match?(num_a) && num_rex.match?(num_b) + + num_a_f = num_a.to_f + num_b_f = num_b.to_f + + room.send_notice("#{num_a} * #{num_b} = #{(num_a_f * num_b_f).round(2)}") +end + command :echo, desc: 'Echoes the given message back as an m.notice' do |message| break if message.nil? # Don't echo empty requests