@@ -31,8 +31,7 @@ namespace _01_basic_ping_bot | |||||
// It is recommended to Dispose of a client when you are finished | // It is recommended to Dispose of a client when you are finished | ||||
// using it, at the end of your app's lifetime. | // using it, at the end of your app's lifetime. | ||||
_client = new DiscordSocketClient(); | _client = new DiscordSocketClient(); | ||||
_client.Log += LogAsync; | |||||
_client.Ready += ReadyAsync; | _client.Ready += ReadyAsync; | ||||
_client.MessageReceived += MessageReceivedAsync; | _client.MessageReceived += MessageReceivedAsync; | ||||
} | } | ||||
@@ -47,12 +46,6 @@ namespace _01_basic_ping_bot | |||||
await Task.Delay(Timeout.Infinite); | await Task.Delay(Timeout.Infinite); | ||||
} | } | ||||
private Task LogAsync(LogMessage log) | |||||
{ | |||||
Console.WriteLine(log.ToString()); | |||||
return Task.CompletedTask; | |||||
} | |||||
// The Ready event indicates that the client has opened a | // The Ready event indicates that the client has opened a | ||||
// connection and it is now safe to access the cache. | // connection and it is now safe to access the cache. | ||||
private Task ReadyAsync() | private Task ReadyAsync() | ||||
@@ -35,9 +35,6 @@ namespace _02_commands_framework | |||||
{ | { | ||||
var client = services.GetRequiredService<DiscordSocketClient>(); | var client = services.GetRequiredService<DiscordSocketClient>(); | ||||
client.Log += LogAsync; | |||||
services.GetRequiredService<CommandService>().Log += LogAsync; | |||||
// Tokens should be considered secret data and never hard-coded. | // Tokens should be considered secret data and never hard-coded. | ||||
// We can read from the environment variable to avoid hardcoding. | // We can read from the environment variable to avoid hardcoding. | ||||
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); | await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); | ||||
@@ -50,13 +47,6 @@ namespace _02_commands_framework | |||||
} | } | ||||
} | } | ||||
private Task LogAsync(LogMessage log) | |||||
{ | |||||
Console.WriteLine(log.ToString()); | |||||
return Task.CompletedTask; | |||||
} | |||||
private ServiceProvider ConfigureServices() | private ServiceProvider ConfigureServices() | ||||
{ | { | ||||
return new ServiceCollection() | return new ServiceCollection() | ||||
@@ -38,7 +38,6 @@ namespace _03_sharded_client | |||||
// The ShardReady event is used instead, allowing for individual | // The ShardReady event is used instead, allowing for individual | ||||
// control per shard. | // control per shard. | ||||
client.ShardReady += ReadyAsync; | client.ShardReady += ReadyAsync; | ||||
client.Log += LogAsync; | |||||
await services.GetRequiredService<CommandHandlingService>().InitializeAsync(); | await services.GetRequiredService<CommandHandlingService>().InitializeAsync(); | ||||
@@ -65,11 +64,5 @@ namespace _03_sharded_client | |||||
Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!"); | Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!"); | ||||
return Task.CompletedTask; | return Task.CompletedTask; | ||||
} | } | ||||
private Task LogAsync(LogMessage log) | |||||
{ | |||||
Console.WriteLine(log.ToString()); | |||||
return Task.CompletedTask; | |||||
} | |||||
} | } | ||||
} | } |
@@ -21,7 +21,6 @@ namespace _03_sharded_client.Services | |||||
_services = services; | _services = services; | ||||
_commands.CommandExecuted += CommandExecutedAsync; | _commands.CommandExecuted += CommandExecutedAsync; | ||||
_commands.Log += LogAsync; | |||||
_discord.MessageReceived += MessageReceivedAsync; | _discord.MessageReceived += MessageReceivedAsync; | ||||
} | } | ||||
@@ -61,12 +60,5 @@ namespace _03_sharded_client.Services | |||||
// the command failed, let's notify the user that something happened. | // the command failed, let's notify the user that something happened. | ||||
await context.Channel.SendMessageAsync($"error: {result.ToString()}"); | await context.Channel.SendMessageAsync($"error: {result.ToString()}"); | ||||
} | } | ||||
private Task LogAsync(LogMessage log) | |||||
{ | |||||
Console.WriteLine(log.ToString()); | |||||
return Task.CompletedTask; | |||||
} | |||||
} | } | ||||
} | } |
@@ -34,60 +34,15 @@ namespace idn | |||||
static async Task Main(string[] args) | static async Task Main(string[] args) | ||||
{ | { | ||||
var token = File.ReadAllText("token.ignore"); | var token = File.ReadAllText("token.ignore"); | ||||
var client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Debug }); | |||||
var logQueue = new ConcurrentQueue<LogMessage>(); | |||||
var client = new DiscordSocketClient(new DiscordSocketConfig { }); | |||||
var logCancelToken = new CancellationTokenSource(); | var logCancelToken = new CancellationTokenSource(); | ||||
int presenceUpdates = 0; | int presenceUpdates = 0; | ||||
client.Log += msg => | |||||
{ | |||||
logQueue.Enqueue(msg); | |||||
return Task.CompletedTask; | |||||
}; | |||||
Console.CancelKeyPress += (_ev, _s) => | Console.CancelKeyPress += (_ev, _s) => | ||||
{ | { | ||||
logCancelToken.Cancel(); | logCancelToken.Cancel(); | ||||
}; | }; | ||||
var logTask = Task.Run(async () => | |||||
{ | |||||
var fs = new FileStream("idn.log", FileMode.Append); | |||||
var logStringBuilder = new StringBuilder(200); | |||||
string logString = ""; | |||||
byte[] helloBytes = Encoding.UTF8.GetBytes($"### new log session: {DateTime.Now} ###\n\n"); | |||||
await fs.WriteAsync(helloBytes); | |||||
while (!logCancelToken.IsCancellationRequested) | |||||
{ | |||||
if (logQueue.TryDequeue(out var msg)) | |||||
{ | |||||
if (msg.Message?.IndexOf("PRESENCE_UPDATE)") > 0) | |||||
{ | |||||
presenceUpdates++; | |||||
continue; | |||||
} | |||||
_ = msg.ToString(builder: logStringBuilder); | |||||
logStringBuilder.AppendLine(); | |||||
logString = logStringBuilder.ToString(); | |||||
Debug.Write(logString, "DNET"); | |||||
await fs.WriteAsync(Encoding.UTF8.GetBytes(logString)); | |||||
} | |||||
await fs.FlushAsync(); | |||||
try | |||||
{ | |||||
await Task.Delay(100, logCancelToken.Token); | |||||
} | |||||
finally { } | |||||
} | |||||
byte[] goodbyeBytes = Encoding.UTF8.GetBytes($"#!! end log session: {DateTime.Now} !!#\n\n\n"); | |||||
await fs.WriteAsync(goodbyeBytes); | |||||
await fs.DisposeAsync(); | |||||
}); | |||||
await client.LoginAsync(TokenType.Bot, token); | await client.LoginAsync(TokenType.Bot, token); | ||||
await client.StartAsync(); | await client.StartAsync(); | ||||
@@ -127,9 +82,9 @@ namespace idn | |||||
await client.StopAsync(); | await client.StopAsync(); | ||||
client.Dispose(); | client.Dispose(); | ||||
logCancelToken.Cancel(); | logCancelToken.Cancel(); | ||||
try | |||||
{ await logTask; } | |||||
finally { Console.WriteLine("goodbye!"); } | |||||
await Task.Delay(-1, logCancelToken.Token); | |||||
Console.WriteLine("goodbye!"); | |||||
} | } | ||||
static IEnumerable<Assembly> GetAssemblies() | static IEnumerable<Assembly> GetAssemblies() | ||||