fix: some build & linter warnings

This commit is contained in:
ZavaruKitsu
2022-10-08 19:15:51 +03:00
parent 3f0d109fe2
commit a731e2a8d0
159 changed files with 2738 additions and 3742 deletions
+7 -5
View File
@@ -1,17 +1,19 @@
using System;
using SystemCommandsBot.config;
using SystemCommandsBot.forms;
using TelegramBotBase.Builder;
namespace SystemCommandsBot
{
class Program
internal class Program
{
public static config.Config BotConfig { get; set; }
public static Config BotConfig { get; set; }
static void Main(string[] args)
private static void Main(string[] args)
{
BotConfig = config.Config.load();
BotConfig = Config.Load();
if (BotConfig.ApiKey == null || BotConfig.ApiKey.Trim() == "")
{
@@ -21,7 +23,7 @@ namespace SystemCommandsBot
}
var bot = BotBaseBuilder.Create()
.QuickStart<forms.StartForm>(BotConfig.ApiKey)
.QuickStart<StartForm>(BotConfig.ApiKey)
.Build();
bot.Start();
@@ -1,28 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace SystemCommandsBot.commands
namespace SystemCommandsBot.commands
{
public class Commando
{
public int ID { get; set; }
public int Id { get; set; }
public String Title { get; set; }
public string Title { get; set; }
public String ShellCmd { get; set; }
public string ShellCmd { get; set; }
public bool Enabled { get; set; } = true;
public String Action { get; set; }
public string Action { get; set; }
public bool UseShell { get; set; } = true;
public int? MaxInstances { get; set; }
public String ProcName
public string ProcName
{
get;set;
}
+22 -21
View File
@@ -1,36 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using SystemCommandsBot.commands;
namespace SystemCommandsBot.config
{
public class Config
{
public String Password { get; set; }
public string Password { get; set; }
public String ApiKey { get; set; }
public string ApiKey { get; set; }
public List<commands.Commando> Commandos { get; set; }
public List<Commando> Commandos { get; set; }
public Config()
{
this.Commandos = new List<commands.Commando>();
Commandos = new List<Commando>();
}
public void loadDefaultValues()
public void LoadDefaultValues()
{
this.ApiKey = "";
this.Commandos.Add(new commands.Commando() { ID = 0, Enabled = true, Title = "Test Befehl", ShellCmd = "explorer.exe", Action = "start", MaxInstances = 2 });
ApiKey = "";
Commandos.Add(new Commando { Id = 0, Enabled = true, Title = "Test Befehl", ShellCmd = "explorer.exe", Action = "start", MaxInstances = 2 });
}
public static Config load()
public static Config Load()
{
try
{
return load(AppContext.BaseDirectory + "config\\default.cfg");
return Load(AppContext.BaseDirectory + "config\\default.cfg");
}
@@ -42,16 +43,16 @@ namespace SystemCommandsBot.config
}
public static Config load(String path)
public static Config Load(string path)
{
try
{
var cfg = Newtonsoft.Json.JsonConvert.DeserializeObject<Config>(File.ReadAllText(path)) as Config;
var cfg = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
return cfg;
}
catch (DirectoryNotFoundException ex)
catch (DirectoryNotFoundException)
{
DirectoryInfo di = new DirectoryInfo(path);
var di = new DirectoryInfo(path);
if (!Directory.Exists(di.Parent.FullName))
{
@@ -59,15 +60,15 @@ namespace SystemCommandsBot.config
}
var cfg = new Config();
cfg.loadDefaultValues();
cfg.save(path);
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch (FileNotFoundException ex)
catch (FileNotFoundException)
{
var cfg = new Config();
cfg.loadDefaultValues();
cfg.save(path);
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch(Exception ex)
@@ -77,11 +78,11 @@ namespace SystemCommandsBot.config
return null;
}
public void save(String path)
public void Save(string path)
{
try
{
File.WriteAllText(path, Newtonsoft.Json.JsonConvert.SerializeObject(this));
File.WriteAllText(path, JsonConvert.SerializeObject(this));
}
catch
{
+31 -31
View File
@@ -1,31 +1,29 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace SystemCommandsBot.forms
{
public class CmdForm : TelegramBotBase.Form.AutoCleanForm
public class CmdForm : AutoCleanForm
{
public DateTime ExpiresAt { get; set; }
public int? MessageId { get; set; }
public override async Task Load(MessageResult message)
public override Task Load(MessageResult message)
{
return Task.CompletedTask;
}
public override async Task Action(MessageResult message)
{
var btn = message.RawData;
int id = -1;
var id = -1;
if (!int.TryParse(btn, out id))
{
@@ -33,10 +31,10 @@ namespace SystemCommandsBot.forms
return;
}
var cmd = Program.BotConfig.Commandos.Where(a => a.Enabled && a.ID == id).FirstOrDefault();
var cmd = Program.BotConfig.Commandos.Where(a => a.Enabled && a.Id == id).FirstOrDefault();
if (cmd == null)
{
await this.Device.Send("Cmd nicht verfügbar.");
await Device.Send("Cmd nicht verfügbar.");
return;
}
@@ -46,14 +44,14 @@ namespace SystemCommandsBot.forms
{
case "start":
FileInfo fi = new FileInfo(cmd.ShellCmd);
var fi = new FileInfo(cmd.ShellCmd);
if (cmd.MaxInstances != null && cmd.MaxInstances >= 0)
{
if (Process.GetProcessesByName(cmd.ProcName).Count() >= cmd.MaxInstances)
{
await this.Device.Send("Anwendung läuft bereits.");
await Device.Send("Anwendung läuft bereits.");
await message.ConfirmAction("Anwendung läuft bereits.");
return;
@@ -61,14 +59,16 @@ namespace SystemCommandsBot.forms
}
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = cmd.ShellCmd;
psi.WorkingDirectory = fi.DirectoryName;
psi.UseShellExecute = cmd.UseShell;
var psi = new ProcessStartInfo
{
FileName = cmd.ShellCmd,
WorkingDirectory = fi.DirectoryName,
UseShellExecute = cmd.UseShell
};
Process.Start(psi);
await this.Device.Send(fi.Name + " wurde gestarted.");
await Device.Send(fi.Name + " wurde gestarted.");
await message.ConfirmAction(fi.Name + " wurde gestarted.");
@@ -76,9 +76,9 @@ namespace SystemCommandsBot.forms
case "kill":
FileInfo fi2 = new FileInfo(cmd.ShellCmd);
var fi2 = new FileInfo(cmd.ShellCmd);
String pros = fi2.Name.Replace(fi2.Extension, "");
var pros = fi2.Name.Replace(fi2.Extension, "");
var proc = Process.GetProcessesByName(pros).ToList();
@@ -94,7 +94,7 @@ namespace SystemCommandsBot.forms
}
}
await this.Device.Send(fi2.Name + " wurde beendet.");
await Device.Send(fi2.Name + " wurde beendet.");
await message.ConfirmAction(fi2.Name + " wurde beendet.");
@@ -102,9 +102,9 @@ namespace SystemCommandsBot.forms
case "restart":
FileInfo fi3 = new FileInfo(cmd.ShellCmd);
var fi3 = new FileInfo(cmd.ShellCmd);
String pros2 = fi3.Name.Replace(fi3.Extension, "");
var pros2 = fi3.Name.Replace(fi3.Extension, "");
var proc2 = Process.GetProcessesByName(pros2).ToList();
@@ -120,17 +120,19 @@ namespace SystemCommandsBot.forms
}
}
FileInfo fi4 = new FileInfo(cmd.ShellCmd);
var fi4 = new FileInfo(cmd.ShellCmd);
ProcessStartInfo psi2 = new ProcessStartInfo();
psi2.FileName = cmd.ShellCmd;
psi2.WorkingDirectory = fi4.DirectoryName;
var psi2 = new ProcessStartInfo
{
FileName = cmd.ShellCmd,
WorkingDirectory = fi4.DirectoryName
};
psi2.FileName = cmd.ShellCmd;
psi2.UseShellExecute = cmd.UseShell;
Process.Start(psi2);
await this.Device.Send(fi3.Name + " wurde neugestarted.");
await Device.Send(fi3.Name + " wurde neugestarted.");
await message.ConfirmAction(fi3.Name + " wurde neugestarted.");
@@ -151,15 +153,13 @@ namespace SystemCommandsBot.forms
public override async Task Render(MessageResult message)
{
if (this.MessageId == null)
if (MessageId == null)
{
var buttons = Program.BotConfig.Commandos.Where(a => a.Enabled).Select(a => new ButtonBase(a.Title, a.ID.ToString()));
var buttons = Program.BotConfig.Commandos.Where(a => a.Enabled).Select(a => new ButtonBase(a.Title, a.Id.ToString()));
ButtonForm bf = new ButtonForm();
var bf = new ButtonForm();
bf.AddSplitted(buttons, 1);
await this.Device.Send("Deine Optionen", bf);
return;
await Device.Send("Deine Optionen", bf);
}
+10 -11
View File
@@ -1,40 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace SystemCommandsBot.forms
{
public class StartForm : TelegramBotBase.Form.FormBase
public class StartForm : FormBase
{
public String Password { get; set; }
public string Password { get; set; }
public override async Task Load(MessageResult message)
public override Task Load(MessageResult message)
{
var inp = message.MessageText;
if (Program.BotConfig.Password == inp)
{
this.Password = inp;
Password = inp;
}
return Task.CompletedTask;
}
public override async Task Render(MessageResult message)
{
if (this.Password == null || this.Password.Trim() == "")
if (Password == null || Password.Trim() == "")
{
await this.Device.Send("Bitte gib dein Passwort an.");
await Device.Send("Bitte gib dein Passwort an.");
return;
}
var cmd = new forms.CmdForm();
var cmd = new CmdForm();
cmd.ExpiresAt = DateTime.Now.AddDays(14);
await this.NavigateTo(cmd);
await NavigateTo(cmd);
}