Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Commit

Permalink
Script execution and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Krajancic authored and Ben Krajancic committed Oct 22, 2018
1 parent c992701 commit 79a1f2d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 27 deletions.
4 changes: 3 additions & 1 deletion ApplicationInterfaces/ApplicationInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public void StartTickThreadLoop()
try {
tick();
} catch (Exception e)
{ }
{
Console.WriteLine(e);
}
}
}

Expand Down
20 changes: 13 additions & 7 deletions ApplicationInterfaces/HTTP_Discord/HttpInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override void SendPrivateMessage(object sender, MessageEventArgs messaged
Username = prevusername;
} catch (Exception e)
{

Console.WriteLine(e);
}
}

Expand Down Expand Up @@ -112,7 +112,7 @@ public override void tick()

catch (Exception e)
{

Console.WriteLine(e);
}
}
}
Expand All @@ -137,9 +137,9 @@ void GetLatestMessages(string chatroom)
Last_Message[chatroom] = ChannelMessages[0].id;
}
}
catch
catch (Exception e)
{

Console.WriteLine(e);
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ void SendMessageThroughWebhook(string message, string URL)
}
catch (Exception e)
{

Console.WriteLine(e);
}
}

Expand Down Expand Up @@ -221,12 +221,15 @@ public List<MessageEventArgs> GetChannelMessages(Dictionary<string, string> all_
{
try
{

foreach (var response in GetChannelMessages(channel.Key))
{
responses.Add(response);
}
}
catch { }
catch (Exception e) {
Console.WriteLine(e);
}
}

return responses;
Expand Down Expand Up @@ -263,14 +266,15 @@ public bool CheckUserIsAdmin(string author) {
}
catch (Exception e)
{

Console.WriteLine(e);
}
return false;
}

public List<MessageEventArgs> GetChannelMessages(string channel)
{
try {

System.Threading.Thread.Sleep(125);
var client = new RestClient("https://discordapp.com/api/v6/channels/" + channel + "/messages?after=" + Last_Message[channel]);
var request = new RestRequest(Method.GET);
Expand Down Expand Up @@ -308,6 +312,8 @@ public List<MessageEventArgs> GetChannelMessages(string channel)
}
catch (Exception e)
{
Console.WriteLine("Channel: " + channel);
Console.WriteLine(e);
return new List<MessageEventArgs>();
}
}
Expand Down
3 changes: 1 addition & 2 deletions UserHandlers/VBot/VBot/Modules/AdminModule/AdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public override void OnAllModulesLoaded()
filefromarray.Data = CommandList;
filefromarray.TableKey = module.GetType().Name.ToString();
HtmlHandler.HandleCommand(filefromarray);



}
}

Expand Down
49 changes: 40 additions & 9 deletions UserHandlers/VBot/VBot/Modules/AdminModule/Commands/RunScript.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;

namespace SteamBotLite
{
Expand All @@ -9,25 +11,54 @@ private class RunScript : BaseCommand
// Command to query if a server is active
private AdminModule module;

public RunScript(ModuleHandler bot, AdminModule module) : base(bot, "!RunUpdateScript")
public RunScript(ModuleHandler bot, AdminModule module) : base(bot, "!runscript")
{
this.module = module;
}

protected override string exec(MessageEventArgs Msg, string param)
{
Process proc = new Process
string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "/scripts";
if (Directory.Exists(filepath) == false)
{
StartInfo = new ProcessStartInfo
try
{
FileName = "../../update.sh"
Directory.CreateDirectory(filepath);
return "The directory didn't exist, so it has been created";
}
};
catch (Exception e)
{
return "The directory doesn't exist, and trying to make it caused an error!";
}
}

proc.Start();
if (param.ToLower().Equals("!runscript"))
{
Console.WriteLine("Param is empty?");
DirectoryInfo d = new DirectoryInfo(filepath);

Process.GetCurrentProcess().Kill();
return "Script should've ran";
String output = "";
foreach (var file in d.GetFiles("*.sh"))
{
output += file.Name + ", ";
}
output.Substring(0, output.Length - 2);
return "Scripts allowed: " + output;
}
else
{
Console.WriteLine("Not empty");
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = filepath + '/' + param
}
};
proc.Start();

return "Executed script: " + param;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,21 @@ public override void OnAllModulesLoaded()

public async void StartWebServer(string prefix)
{
Console.WriteLine("Website Loding");
listener = new HttpListener();
listener.Prefixes.Add(prefix);
listener.Start();
while (true)
{
var data = await listener.GetContextAsync();
ResponseMethod(data);
Console.WriteLine("Website Loading");
try
{
listener = new HttpListener();
listener.Prefixes.Add(prefix);
listener.Start();
while (true)
{
var data = await listener.GetContextAsync();
ResponseMethod(data);
}

} catch (Exception e)
{

}
Console.WriteLine("Website Loaded");

Expand Down

0 comments on commit 79a1f2d

Please sign in to comment.