Cleaned up command parsing

This commit is contained in:
Matthieu Grieger 2014-11-20 12:13:22 -08:00
parent 5d95dbe218
commit 24bd000c20

View file

@ -52,13 +52,13 @@ class MumbleDJ
end end
end end
# Sets various callbacks that can be triggered during the connection. # Safely disconnects the bot from the server.
def set_callbacks def disconnect
@client.on_text_message do |message| @client.disconnect
parse_message(message)
end
end end
private
# Parses messages looking for commands, and calls the appropriate # Parses messages looking for commands, and calls the appropriate
# methods to complete each requested command. # methods to complete each requested command.
def parse_message(message) def parse_message(message)
@ -74,35 +74,25 @@ class MumbleDJ
case @command case @command
when ADD_ALIAS when ADD_ALIAS
if has_permission?(ADMIN_ADD, @sender) if has_permission?(ADMIN_ADD, @sender)
if OUTPUT_ENABLED add(@sender, @argument)
puts("#{@sender} has added a song to the queue.")
end
if song_add_successful?(@argument, @sender)
@client.text_channel(@client.me.current_channel.name, "<b>#{@sender}</b> has added a song to the queue.")
else
@client.text_user(@sender, "The URL you provided was not valid.")
end
else else
@client.text_user(@sender, NO_PERMISSION_MSG) @client.text_user(@sender, NO_PERMISSION_MSG)
end end
when SKIP_ALIAS when SKIP_ALIAS
if has_permission?(ADMIN_SKIP, @sender) if has_permission?(ADMIN_SKIP, @sender)
if OUTPUT_ENABLED skip(@sender)
puts("#{@sender} has voted to skip the current song.")
end
@song_queue.get_current_song.add_skip(@sender)
else else
@client.text_user(@sender, NO_PERMISSION_MSG) @client.text_user(@sender, NO_PERMISSION_MSG)
end end
when VOLUME_ALIAS when VOLUME_ALIAS
puts("Volume command request.") if has_permission?(ADMIN_VOLUME, @sender)
volume(@sender, @argument)
else
@client.text_user(@sender, NO_PERMISSION_MSG)
end
when MOVE_ALIAS when MOVE_ALIAS
if has_permission?(ADMIN_MOVE, @sender) if has_permission?(ADMIN_MOVE, @sender)
begin move(@sender, @argument)
@client.join_channel(@argument)
rescue Mumble::ChannelNotFound
@client.text_user(@sender, "The channel you provided does not exist.")
end
else else
@client.text_user(@sender, NO_PERMISSION_MSG) @client.text_user(@sender, NO_PERMISSION_MSG)
end end
@ -124,6 +114,13 @@ class MumbleDJ
end end
end end
# Sets various callbacks that can be triggered during the connection.
def set_callbacks
@client.on_text_message do |message|
parse_message(message)
end
end
# Checks message sender against ADMINS array to verify if they have # Checks message sender against ADMINS array to verify if they have
# permission to use a specific command. # permission to use a specific command.
def has_permission?(admin_command, sender) def has_permission?(admin_command, sender)
@ -132,8 +129,33 @@ class MumbleDJ
end end
end end
# Safely disconnects the bot from the server. def add(sender, url)
def disconnect if OUTPUT_ENABLED
@client.disconnect puts("#{@sender} has added a song to the queue.")
end
if song_add_successful?(url, @sender)
@client.text_channel(@client.me.current_channel.name, "<b>#{@sender}</b> has added a song to the queue.")
else
@client.text_user(@sender, "The URL you provided was not valid.")
end
end
def skip(sender)
if OUTPUT_ENABLED
puts("#{@sender} has voted to skip the current song.")
end
@song_queue.get_current_song.add_skip(@sender)
end
def volume(sender, vol)
end
def move(sender, channel)
begin
@client.join_channel(channel)
rescue Mumble::ChannelNotFound
@client.text_user(sender, "The channel you provided does not exist.")
end
end end
end end