-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingService.cs
More file actions
executable file
·131 lines (119 loc) · 4.07 KB
/
LoggingService.cs
File metadata and controls
executable file
·131 lines (119 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ScratchBot
{
internal class LoggingService
{
public LoggingService(DiscordSocketClient _client, CommandService _command)
{
_client.Log += LogAsync;
_command.Log += LogAsync;
}
~LoggingService()
{
BotMain.instance.GetSock.Log -= LogAsync;
BotMain.instance.GetCommands.Log -= LogAsync;
}
private async Task LogAsync(LogMessage message)
{
bool _doWebhookLog = false;
bool _doFileLog = false;
switch (message.Severity)
{
case LogSeverity.Critical:
{
_doWebhookLog = true;
_doFileLog = true;
Console.ForegroundColor = ConsoleColor.DarkRed;
break;
}
case LogSeverity.Error:
{
_doWebhookLog = true;
_doFileLog = true;
Console.ForegroundColor = ConsoleColor.Red;
break;
}
case LogSeverity.Warning:
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
break;
}
case LogSeverity.Info:
{
Console.ForegroundColor = ConsoleColor.Cyan;
break;
}
case LogSeverity.Verbose:
{
Console.ForegroundColor = ConsoleColor.Green;
break;
}
case LogSeverity.Debug:
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
break;
}
default:
break;
}
string _msg;
if (message.Exception is CommandException _cmdEX)
{
_msg = $"\n[Command/{message.Severity}] {_cmdEX.Command.Aliases[0]}\n" +
$"Failed to execute in {_cmdEX.Context.Channel}.\n {_cmdEX}";
Console.WriteLine(_msg);
}
else
{
_msg = $"\n[General/{message.Severity}] ({message})";
Console.WriteLine(_msg);
}
if (_doWebhookLog)
{
await WebhookLog(_msg);
}
if (_doFileLog)
{
FileLogger.LogToFile(_msg);
}
Console.ResetColor();
await Task.CompletedTask;
}
public static Task WebTest(string _msg) => WebhookLog(_msg);
private static Task WebhookLog(string _msg)
{
if (!string.IsNullOrEmpty(BotMain.WebhookLink))
{
using WebClient _client = new();
NameValueCollection _data = new()
{
{ "username", "ScratchBot_WebHook" },
{ "content", _msg },
};
byte[] _outp = _client.UploadValues(BotMain.WebhookLink, _data);
if (Encoding.UTF8.GetString(_outp) == string.Empty)
{
return Task.CompletedTask;
}
else
{
FileLogger.LogToFile(Encoding.UTF8.GetString(_outp));
return Task.Delay(3);
}
}
else
{
FileLogger.LogToFile("Failed to send webhook msg due to not having the environment var set up.");
return Task.CompletedTask;
}
}
}
}