Browse Source

Update logging sample

Remove redundant part of the sample
pull/1304/head
Still Hsu 6 years ago
parent
commit
581037760f
1 changed files with 14 additions and 19 deletions
  1. +14
    -19
      docs/guides/concepts/samples/logging.cs

+ 14
- 19
docs/guides/concepts/samples/logging.cs View File

@@ -1,29 +1,24 @@
using Discord;
using Discord.WebSocket;

public class Program
public class LoggingService
{
private DiscordSocketClient _client;
static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
public LoggingService(DiscordSocketClient client, CommandService command)
{
_client = new DiscordSocketClient(new DiscordSocketConfig
{
LogLevel = LogSeverity.Info
});

_client.Log += Log;

await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken"));
await _client.StartAsync();
await Task.Delay(-1);
client.Log += LogAsync;
command.Log += LogAsync;
}

private Task Log(LogMessage message)
private Task LogAsync(LogMessage message)
{
Console.WriteLine(message.ToString());
if (message.Exception is CommandException cmdException)
{
Console.WriteLine($"[Command/{message.Severity}] {cmdException.Command.Aliases.First()}"
+ $" failed to execute in {cmdException.Context.Channel}.");
Console.WriteLine(cmdException);
}
else
Console.WriteLine($"[General/{message.Severity}] {message}");

return Task.CompletedTask;
}
}

Loading…
Cancel
Save