Skip to content

Commit

Permalink
Removing debug lines for the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
CPULL committed Apr 27, 2022
1 parent 0cdcde8 commit eb475ed
Showing 1 changed file with 13 additions and 50 deletions.
63 changes: 13 additions & 50 deletions UPBot Code/Commands/Logs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,102 +18,63 @@ public class Logs : BaseCommandModule {
[Description("Allows to see and download guild logs (only admins)")]
public async Task LogsCommand(CommandContext ctx) {
if (ctx.Guild == null) return;
Utils.LogUserCommand(ctx);

try {

Utils.Log("Checking admin", null);

if (!Setup.HasAdminRole(ctx.Guild.Id, ctx.Member.Roles, false)) return;


Utils.Log("Checking if we post in GLOBAL", null);
Utils.Log("Checking if we post local", ctx.Guild.Name);

string logs = Utils.GetLogsPath(ctx.Guild.Name);

Utils.Log("Log path = " + logs, null);

using (var fs = new FileStream(logs, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
Utils.Log("filestream reader open", null);
using (var sr = new StreamReader(fs)) {
int num = 0;
Utils.Log("stream reader open", null);
while (!sr.EndOfStream) {
sr.ReadLine();
num++;
}
Utils.Log("read lines " + num, null);
}
}

Utils.Log("Quitting", null);

await Utils.DeleteDelayed(60, ctx.Message.RespondAsync(helpMsg));

} catch (Exception ex) {
Utils.Log("Error in LogsCommand: " + ex.Message, ctx.Guild.Name);
}
}


[Command("Logs")]
[Description("Allows to see and download guild logs (only admins)")]
public async Task LogsCommand(CommandContext ctx, [Description("How many lines to show or 'save' to save the logs")]string what) {
public async Task LogsCommand(CommandContext ctx, [Description("How many lines to show or 'save'")]string what) {
if (ctx.Guild == null) return;
Utils.Log("Checking if we post in GLOBAL", null);
Utils.LogUserCommand(ctx);
try {
Utils.Log("Checking admin", null);
if (!Setup.HasAdminRole(ctx.Guild.Id, ctx.Member.Roles, false)) return;

Utils.Log("Getting path", null);
string logs = Utils.GetLogsPath(ctx.Guild.Name);

Utils.Log("Checking if we get a numebr", null);
if (int.TryParse(what, out int num)) {
Utils.Log("Got number " + num, null);
if (num < 1) num = 1;
if (num > 25) num = 25;
Utils.Log("Checking logs", null);
if (logs == null) {
await Utils.DeleteDelayed(60, ctx.Message.RespondAsync($"There are no logs today for the guild **{ctx.Guild.Name}**"));
return;
}
Utils.Log("Checking done", null);

List<string> lines = new List<string>();

using (var fs = new FileStream(logs, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
Utils.Log("filestream reader open", null);
using (var sr = new StreamReader(fs)) {
Utils.Log("stream reader open", null);
while (!sr.EndOfStream) {
lines.Add(sr.ReadLine());
Utils.Log("adding a line", null);
}
}
}

Utils.Log("lines found " + lines.Count, null);
int start = lines.Count - num;
if (start < 0) start = 0;
string res = $"Last {num} lines of logs:\n```";
Utils.Log("preparing res start="+start, null);
while (start < lines.Count) {
res += lines[start] + "\n";
start++;
}
Utils.Log("trimming", null);
if (res.Length > 1990) res = res[0..1990] + "...\n";

res += "```";
Utils.Log("posting result", null);
await Utils.DeleteDelayed(60, ctx.Message.RespondAsync(res));
Utils.LogUserCommand(ctx);
return;
}

if (what.Equals("save", StringComparison.InvariantCultureIgnoreCase)) {
else if (what.Equals("save", StringComparison.InvariantCultureIgnoreCase)) {
if (logs == null) {
await Utils.DeleteDelayed(60, ctx.Message.RespondAsync($"There are no logs today for the guild **{ctx.Guild.Name}**"));
Utils.LogUserCommand(ctx);
return;
}

Expand All @@ -127,10 +88,11 @@ public async Task LogsCommand(CommandContext ctx, [Description("How many lines t
await Utils.DeleteDelayed(60, msg);
await Utils.DeleteFileDelayed(30, outfile);
await Utils.DeleteFolderDelayed(30, logsFolder);
Utils.LogUserCommand(ctx);
return;
}

if (what.Equals("saveall", StringComparison.InvariantCultureIgnoreCase)) {
else if (what.Equals("saveall", StringComparison.InvariantCultureIgnoreCase)) {
string logsFolder = Utils.GetAllLogsFolder(ctx.Guild.Name);

string outfile = logsFolder[0..^1] + ".zip";
Expand All @@ -142,11 +104,13 @@ public async Task LogsCommand(CommandContext ctx, [Description("How many lines t
await Utils.DeleteDelayed(60, msg);
await Utils.DeleteFileDelayed(30, outfile);
await Utils.DeleteFolderDelayed(30, logsFolder);
Utils.LogUserCommand(ctx);
return;
}

if (what.Equals("delete", StringComparison.InvariantCultureIgnoreCase)) {
else if (what.Equals("delete", StringComparison.InvariantCultureIgnoreCase)) {
await Utils.DeleteDelayed(60, ctx.Message.RespondAsync("You have to specify the full guild name after 'delete' (_case sensitive_) to confirm the delete of the logs."));
Utils.LogUserCommand(ctx);
return;
}

Expand All @@ -158,10 +122,9 @@ public async Task LogsCommand(CommandContext ctx, [Description("How many lines t
}
}

/*
[Command("LogsDelete")]
[Description("Allows to see and download guild logs (only admins)")]
public async Task LogsCommand(CommandContext ctx, [Description("How many lines to show or 'save' to save the logs")]string what, [Description("The name of the guild, case sensitive, to confirm the delete")]string guildname) {
public async Task LogsCommand(CommandContext ctx, [Description("How many lines to show or 'save'")]string what, [Description("The name of the guild, case sensitive, to confirm the delete")]string guildname) {
if (ctx.Guild == null) return;
try {
if (!Setup.HasAdminRole(ctx.Guild.Id, ctx.Member.Roles, false)) return;
Expand All @@ -186,6 +149,6 @@ public async Task LogsCommand(CommandContext ctx, [Description("How many lines t
Utils.Log("Error in LogsCommand: " + ex.Message, ctx.Guild.Name);
}
}
*/

const string helpMsg = "Specify how many lines (up to 25) or `save` to download the zipped logs of today, or `saveall` for a zip of all logs of the guild.\nUse `delete` _name of guild_ to delete all logs for the guild.";
}

0 comments on commit eb475ed

Please sign in to comment.