Created
September 26, 2016 20:19
-
-
Save thomasjslone/99533a1feb54151e4c4e52ffc406ac27 to your computer and use it in GitHub Desktop.
Super Simple Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bot | |
def initialize(tasks,repeat,delay,auto) | |
@tasks = tasks.to_a ## this should be an array of strings containing ruby code | |
@delay = delay.to_f ## enter a number of seconds i.e 2.25 | |
@repeat = repeat.to_s ## tells the bot to either stop after the operation or loop it | |
@main_thread = false | |
@feedback = [] ## this is where task execution feedback goes so you can see the results of each operation | |
if auto | |
start_main | |
end | |
end | |
def start_main ## this is the main starter, never start by calling 'main' or main will not be put in a thread and you will have to wait for the operation to finish | |
if @main_thread == nil or @main_thread == false | |
@main_thread = Thread.new { main } | |
true | |
else | |
false | |
end | |
end | |
def stop_main ## stop main thread | |
if @main_thread != nil and @main_thread != false | |
@main = false | |
@main_thread.kill | |
@main_thread = nil | |
true | |
else | |
false | |
end | |
end | |
def main ## heres the guts of the main execution operation | |
tasks = @tasks.to_a ## get list of tasks incase it has updated | |
@main = true | |
exceptions = [] | |
feedback = [] | |
while @main | |
tasks.each do |task| | |
begin | |
feedback << eval(task.to_s) ## each task is a ruby code, we just have to execute the ruby code and compile the feedback and exceptions into a nice little list | |
rescue => exception | |
exceptions << exception.to_s | |
end | |
end | |
if @repeat.to_s == "false" | |
break | |
end | |
@feedback << Time.now.to_s.split(' ')[0..1].join('.').split("-").join('.').split(':').join('.') + " ; " + feedback.join("\n").to_s + "\nEXCEPTIONS:\n" + exceptions.join("\n").to_s ## here we package that feedback nicely with a time stamp and record it | |
sleep @delay | |
end | |
@main = false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment