Created
December 22, 2018 08:51
-
-
Save TheDarkCode/c3575203e2ac15af40b6d35acf552c38 to your computer and use it in GitHub Desktop.
Discord Bot .NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Discord; | |
using Discord.WebSocket; | |
using Discord.Commands; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ExampleBot.Core.Services | |
{ | |
public class DiscordBot : BackgroundService, IDiscordBot | |
{ | |
private DiscordSocketClient _discordClient; | |
public DiscordSocketClient client | |
{ | |
get | |
{ | |
if (_discordClient is null) | |
{ | |
_discordClient = new DiscordSocketClient(); | |
return _discordClient; | |
} | |
else | |
{ | |
return _discordClient; | |
} | |
} | |
private set | |
{ | |
_discordClient = value; | |
} | |
} | |
private string _token; | |
private readonly ILogger<DiscordBot> _logger; | |
//public DiscordBot() { | |
//} | |
public DiscordBot(ILogger<DiscordBot> logger) | |
{ | |
_token = ""; | |
_logger = logger; | |
client = new DiscordSocketClient(); | |
} | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
_logger.LogDebug($"Discord Bot is starting."); | |
stoppingToken.Register(() => _logger.LogDebug($" Discord Bot background task is stopping.")); | |
while (!stoppingToken.IsCancellationRequested) | |
{ | |
await client.LoginAsync(TokenType.Bot, _token); | |
await client.StartAsync(); | |
await Task.Delay(Timeout.Infinite, stoppingToken); | |
} | |
_logger.LogDebug($"Discord Bot background task is stopping.."); | |
} | |
public override async Task StopAsync(CancellationToken cancellationToken) | |
{ | |
await client.LogoutAsync(); | |
await client.StopAsync(); | |
//client = null; | |
} | |
} | |
public interface IDiscordBot : IHostedService | |
{ | |
DiscordSocketClient client { get; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using ExampleBot.Core.Factories; | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ExampleBot.Core.Extensions | |
{ | |
public static class HostedServiceExtensions | |
{ | |
public static void AddHostedService<TService, TImplementation>(this IServiceCollection services) | |
where TService : class | |
where TImplementation : class, IHostedService, TService | |
{ | |
services.AddSingleton<TService, TImplementation>(); | |
services.AddHostedService<HostedServiceFactory<TService>>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Hosting; | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ExampleBot.Core.Factories | |
{ | |
public class HostedServiceFactory<TService> : IHostedService | |
{ | |
private readonly IHostedService _hostedService; | |
public HostedServiceFactory(TService hostedService) | |
{ | |
_hostedService = (IHostedService)hostedService; | |
} | |
public Task StartAsync(CancellationToken cancellationToken) | |
{ | |
return _hostedService.StartAsync(cancellationToken); | |
} | |
public Task StopAsync(CancellationToken cancellationToken) | |
{ | |
return _hostedService.StopAsync(cancellationToken); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment