Created
July 7, 2021 01:14
-
-
Save feelingnothing/277e86fff4c6f4019afebb79f76966f1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // MY STARTUP CLASS | |
| internal static class Entrypoint | |
| { | |
| private static async Task Main() | |
| { | |
| await using var services = ConfigureServices(); | |
| var client = services.GetRequiredService<DiscordClient>(); | |
| var commands = services.GetRequiredService<CommandsNextExtension>(); | |
| commands.RegisterCommands(Assembly.GetExecutingAssembly()); | |
| await client.ConnectAsync(); | |
| await Task.Delay(-1); | |
| } | |
| private static ServiceProvider ConfigureServices() | |
| { | |
| return new ServiceCollection() | |
| .AddLogging(b => b.AddSerilog()) | |
| .AddSingleton(_ => new DiscordClient(new DiscordConfiguration | |
| { | |
| Token = Environment.GetEnvironmentVariable("TOKEN"), | |
| TokenType = TokenType.Bot, | |
| Intents = DiscordIntents.All, | |
| })) | |
| .AddSingleton(services => | |
| { | |
| var client = services.GetRequiredService<DiscordClient>(); | |
| return client.UseCommandsNext(new CommandsNextConfiguration | |
| { | |
| Services = services, | |
| EnableDefaultHelp = false, | |
| StringPrefixes = new[] {".", "!"} | |
| }); | |
| }) | |
| .AddDbContext<DiscordDbContext>(o => o.UseSqlite($"Data Source={Environment.CurrentDirectory + @"\db.sqlite3"}")) | |
| .BuildServiceProvider(); | |
| } | |
| } | |
| // MODULE BASE CLASS | |
| public class DiscordModuleBase : BaseCommandModule | |
| { | |
| public DiscordModuleBase(DiscordDbContext context, ILogger<BaseDiscordClient> logger) | |
| { | |
| Database = context; | |
| Logger = logger; | |
| } | |
| protected DiscordDbContext Database { get; } | |
| private ILogger<BaseDiscordClient> Logger { get; } | |
| private Task LogAsync(LogLevel level, string message, string? origin, Exception? exception = null) | |
| { | |
| Logger.Log(level, new EventId(0, origin), exception, message); | |
| return Task.CompletedTask; | |
| } | |
| protected async Task LogDebugAsync(string message, [CallerMemberName] string? origin = null, Exception? exception = null) => await LogAsync(LogLevel.Debug, message, origin, exception); | |
| protected async Task LogInfoAsync(string message, [CallerMemberName] string? origin = null, Exception? exception = null) => await LogAsync(LogLevel.Information, message, origin, exception); | |
| protected async Task LogWarningAsync(string message, [CallerMemberName] string? origin = null, Exception? exception = null) => await LogAsync(LogLevel.Warning, message, origin, exception); | |
| protected async Task LogErrorAsync(string message, [CallerMemberName] string? origin = null, Exception? exception = null) => await LogAsync(LogLevel.Error, message, origin, exception); | |
| protected async Task LogCriticalAsync(string message, [CallerMemberName] string? origin = null, Exception? exception = null) => await LogAsync(LogLevel.Critical, message, origin, exception); | |
| } | |
| // SAMPLE MODULE THAT DOES NOT WORK | |
| public class InfoModule : DiscordModuleBase | |
| { | |
| public InfoModule(DiscordDbContext context, ILogger<BaseDiscordClient> logger) : base(context, logger) | |
| { | |
| } | |
| [Command("help")] | |
| public async Task InfoAsync(CommandContext ctx) | |
| { | |
| // TODO: Implement help command | |
| await ctx.RespondAsync(""); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment