teeworlds_network/examples/05_chatbot.rb

32 lines
827 B
Ruby
Raw Normal View History

2022-11-05 10:07:16 +00:00
#!/usr/bin/env ruby
2022-11-05 16:27:54 +00:00
# frozen_string_literal: true
2022-11-05 10:07:16 +00:00
# Reply to ! prefixed commands in chat
#
# ruby ./examples/05_chatbot.rb
#
# Then connect to localhost and write !ping in the chat
require_relative '../lib/teeworlds_client'
2022-11-05 10:07:16 +00:00
client = TeeworldsClient.new(verbose: false)
client.on_chat do |msg|
next if msg.message[0] != '!'
case msg.message[1..]
when 'ping' then client.send_chat('pong')
when 'whoami' then client.send_chat("You are: #{msg.author.name}")
when 'list' then client.send_chat(client.game_client.players.values.map(&:name).join(', '))
else client.send_chat('Unkown command! Commands: !ping, !whoami, !list')
2022-11-05 10:07:16 +00:00
end
end
# properly disconnect on ctrl+c
Signal.trap('INT') do
client.disconnect
end
# connect to localhost and block the current thread
client.connect('localhost', 8303, detach: false)