1
1
using System ;
2
2
using System . IO ;
3
+ using System . Net . Http ;
3
4
using System . Text ;
4
5
using System . Threading . Tasks ;
6
+ using System . Xml ;
5
7
using Discord ;
8
+ using Discord . WebSocket ;
6
9
using Gommon ;
7
10
using Humanizer ;
11
+ using Newtonsoft . Json ;
12
+ using Newtonsoft . Json . Linq ;
13
+ using RestSharp ;
8
14
using Volte . Core ;
9
15
using Volte . Core . Models ;
10
16
using Volte . Core . Models . EventArgs ;
@@ -15,8 +21,18 @@ namespace Volte.Services
15
21
{
16
22
public sealed class LoggingService : VolteEventService
17
23
{
24
+ private readonly DiscordShardedClient _client ;
25
+ private readonly HttpClient _http ;
26
+ private readonly object _lock ;
18
27
private const string LogFile = "data/Volte.log" ;
19
- private readonly object _lock = new object ( ) ;
28
+
29
+ public LoggingService ( DiscordShardedClient discordShardedClient ,
30
+ HttpClient httpClient )
31
+ {
32
+ _client = discordShardedClient ;
33
+ _http = httpClient ;
34
+ _lock = new object ( ) ;
35
+ }
20
36
21
37
public override Task DoAsync ( EventArgs args )
22
38
{
@@ -97,7 +113,7 @@ public void Verbose(LogSource src, string message)
97
113
/// </summary>
98
114
/// <param name="e">Exception to print.</param>
99
115
public void LogException ( Exception e )
100
- => Log ( LogSeverity . Error , LogSource . Volte , string . Empty , e ) ;
116
+ => Execute ( LogSeverity . Error , LogSource . Volte , string . Empty , e ) ;
101
117
102
118
private void Execute ( LogSeverity s , LogSource src , string message , Exception e )
103
119
{
@@ -122,6 +138,7 @@ private void Execute(LogSeverity s, LogSource src, string message, Exception e)
122
138
var toWrite = $ "{ Environment . NewLine } { e . Message } { Environment . NewLine } { e . StackTrace } ";
123
139
Append ( toWrite , Color . IndianRed ) ;
124
140
content . Append ( toWrite ) ;
141
+ LogExceptionInDiscord ( e ) ;
125
142
}
126
143
127
144
Console . Write ( Environment . NewLine ) ;
@@ -163,5 +180,31 @@ private void Append(string m, Color c)
163
180
LogSeverity . Debug => ( Color . SandyBrown , "DEBG" ) ,
164
181
_ => throw new ArgumentNullException ( nameof ( severity ) , "severity cannot be null" )
165
182
} ;
183
+
184
+ private void LogExceptionInDiscord ( Exception e )
185
+ {
186
+ if ( ! Config . GuildLogging . Enabled ) return ;
187
+ var guildLogging = Config . GuildLogging ;
188
+ var channel = _client . GetGuild ( guildLogging . GuildId ) ? . GetTextChannel ( guildLogging . ChannelId ) ;
189
+ if ( channel is null )
190
+ {
191
+ Error ( LogSource . Volte , "Invalid guild_logging.guild_id/guild_logging.channel_id configuration. Check your IDs and try again." ) ;
192
+ return ;
193
+ }
194
+
195
+ _ = Task . Run ( async ( ) =>
196
+ {
197
+ var response = await _http . PostAsync ( "https://paste.greemdev.net/documents" , new StringContent ( e . StackTrace , Encoding . UTF8 , "text/plain" ) ) ;
198
+ var respObj = JObject . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
199
+ var url = $ "https://paste.greemdev.net/{ respObj . GetValue ( "key" ) } .cs";
200
+ var embed = new EmbedBuilder ( )
201
+ . WithErrorColor ( )
202
+ . WithTitle ( $ "Exception at { DateTimeOffset . UtcNow . FormatDate ( ) } , { DateTimeOffset . UtcNow . FormatFullTime ( ) } UTC")
203
+ . AddField ( "Exception Type" , e . GetType ( ) , true )
204
+ . AddField ( "Exception Message" , e . Message , true )
205
+ . WithDescription ( $ "View the full Stack Trace [here]({ url } ).") ;
206
+ await embed . SendToAsync ( channel ) ;
207
+ } ) ;
208
+ }
166
209
}
167
210
}
0 commit comments