Class: Mumbletune::Message
- Inherits:
-
Object
- Object
- Mumbletune::Message
- Defined in:
- lib/mumbletune/messages.rb
Class Attribute Summary collapse
-
.template ⇒ Object
Returns the value of attribute template.
Instance Attribute Summary collapse
-
#argument ⇒ Object
instance methods.
-
#client ⇒ Object
instance methods.
-
#command ⇒ Object
instance methods.
-
#sender ⇒ Object
instance methods.
-
#text ⇒ Object
instance methods.
-
#words ⇒ Object
instance methods.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(client, data) ⇒ Message
constructor
A new instance of Message.
- #respond(message) ⇒ Object
-
#respond_all(message) ⇒ Object
send to entire channel.
Constructor Details
#initialize(client, data) ⇒ Message
Returns a new instance of Message.
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mumbletune/messages.rb', line 144 def initialize(client, data) @client = client @sender = client.users[data[:actor]] # users are stored by their session ID @me = client.me @text = data[:message] @words = @text.split @command = words[0] @argument = words[1...words.length].join(" ") end |
Class Attribute Details
.template ⇒ Object
Returns the value of attribute template.
12 13 14 |
# File 'lib/mumbletune/messages.rb', line 12 def template @template end |
Instance Attribute Details
#argument ⇒ Object
instance methods
142 143 144 |
# File 'lib/mumbletune/messages.rb', line 142 def argument @argument end |
#client ⇒ Object
instance methods
142 143 144 |
# File 'lib/mumbletune/messages.rb', line 142 def client @client end |
#command ⇒ Object
instance methods
142 143 144 |
# File 'lib/mumbletune/messages.rb', line 142 def command @command end |
#sender ⇒ Object
instance methods
142 143 144 |
# File 'lib/mumbletune/messages.rb', line 142 def sender @sender end |
#text ⇒ Object
instance methods
142 143 144 |
# File 'lib/mumbletune/messages.rb', line 142 def text @text end |
#words ⇒ Object
instance methods
142 143 144 |
# File 'lib/mumbletune/messages.rb', line 142 def words @words end |
Class Method Details
.parse(client, data) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mumbletune/messages.rb', line 16 def self.parse(client, data) = Message.new(client, data) begin case .text when /^play/i if .argument.length > 0 # user wants to play something if .words.last =~ /now/i play_now = true .argument = .words[1....words.length-1].join(" ") else play_now = false end collection = Mumbletune.resolve(.argument) # handle unknown argument return .respond "I couldn't find what you wanted me to play. :'(" unless collection # associate the collection with a user collection.user = .sender.name # add these tracks to the queue Mumbletune.player.add_collection collection, (play_now) ? true : false if play_now .respond_all "#{.sender.name} is playing #{collection.description} RIGHT NOW." else .respond_all "#{.sender.name} added #{collection.description} to the queue." end else # user wants to unpause if Mumbletune.player.paused? Mumbletune.player.play .respond "Unpaused." else .respond "Not paused." end end when /^pause$/i paused = Mumbletune.player.pause response = (paused) ? "Paused." : "Unpaused." .respond response when /^unpause$/i if Mumbletune.player.paused? Mumbletune.player.play .respond "Unpaused." else .respond "Not paused." end when /^next$/i if Mumbletune.player.any? Mumbletune.player.next current = Mumbletune.player.current_track .respond_all "#{.sender.name} skipped to #{current.artist.name} - #{current.name}" if current else .respond "We're at the end of the queue. Try adding something to play!" end when /^clear$/i Mumbletune.player.clear_queue .respond_all "#{.sender.name} cleared the queue." when /^undo$/i removed = Mumbletune.player.undo if .sender.name == removed.user .respond_all "#{.sender.name} removed #{removed.description}." else .respond_all "#{.sender.name} removed #{removed.description} at #{removed.user} added." end when /^(what|queue)$/i queue = Mumbletune.player.queue current = Mumbletune.player.current_track template_queue = Array.new queue.each do |col| template_col = {description: col.description, tracks: Array.new} col.tracks.each { |t| template_col[:tracks] << {name: t.name, artist: t.artist.name, playing?: current == t, username: col.user} } template_queue << template_col end # Now, a template. rendered = Mustache.render Message.template[:queue], :queue => template_queue, :anything? => (queue.empty?) ? false : true .respond rendered when /^volume\?$/i .respond "The volume is #{Mumbletune.mumble.volume}." when /^volume/i if .argument.length == 0 .respond "The volume is #{Mumbletune.mumble.volume}." else Mumbletune.mumble.volume = .argument .respond "Now the volume is #{Mumbletune.mumble.volume}." end when /^help$/i rendered = Mustache.render Message.template[:commands] .respond rendered else # Unknown command was given. rendered = Mustache.render Message.template[:commands], :unknown => { :command => .text } .respond rendered end rescue => err # Catch any command that errored. .respond "Woah, an error occurred: #{err.}" unless Mumbletune.verbose puts "#{err.class}: #{err.}" puts err.backtrace end end end |
Instance Method Details
#respond(message) ⇒ Object
155 156 157 |
# File 'lib/mumbletune/messages.rb', line 155 def respond() @client.text_user(@sender.session, ) end |
#respond_all(message) ⇒ Object
send to entire channel
159 160 161 |
# File 'lib/mumbletune/messages.rb', line 159 def respond_all() # send to entire channel @client.text_channel(@me.channel_id, ) end |