#!/usr/bin/env ruby
# ./Emery.rb
# pkill Emery
require 'discordrb'
require 'tmpdir'

puts "---\nEmery PID is #{Process.pid}\nTo kill Emery simply {kill #{Process.pid}}\n---"


def emery_loadModules()
	$commands = {}
	puts '---'
	# Yes this gets redefined. Why? Because I like having all my variables in one place but it also needs to be cleared again
	File.foreach("modules/modules.txt", chop: true) { |line|
	# For every line in the file, do below
		modPath = "./modules/" + line.delete("\n") + "/main.rb"
		mod = line.delete("\n")
		if(File.exist?(modPath))
			puts "Load success in #{mod}!"
			load modPath
			if eval("defined? " + "#{mod}_getCommand")  == "method"
				name, command = send("#{mod}_getCommand")
				$commands[name] = command
			else
				puts "Load error in #{mod}!"
			end
		end
	}
	puts '---'
end

token = File.read("token.txt")
$bot = Discordrb::Bot.new token: "#{token}"
$queue = {}
$voicebots = {}
$commands = {}
$bang = '!'
#find a way for this to be server-specific with a fallback option

emery_loadModules()

$bot.message() do |event|
	message = event.content
	server = event.server
	author = event.author
	if message.start_with?('!')
		message = message.sub("!", '')
		if $commands.key?(message.split.first.downcase)
			send("#{$commands[message.split.first.downcase]}", event)
		end
	end
end

$bot.run