public async Task MainAsync(string apiToken, DiscordBotConfig cfg, CancellationToken token) { // Centralize the logic for commands into a separate method. await InitCommands(cfg).ConfigureAwait(false); // Login and connect. await _client.LoginAsync(TokenType.Bot, apiToken).ConfigureAwait(false); await _client.StartAsync().ConfigureAwait(false); await Task.Delay(5_000, token).ConfigureAwait(false); var game = Config.Name; if (!string.IsNullOrWhiteSpace(game)) { await _client.SetGameAsync(game).ConfigureAwait(false); } var app = await _client.GetApplicationInfoAsync().ConfigureAwait(false); Owner = app.Owner.Id; // Wait infinitely so your bot actually stays connected. await MonitorStatusAsync(token).ConfigureAwait(false); }
public SysCord(Bot bot, DiscordBotConfig cfg) { Bot = bot; Config = cfg; Globals.Self = this; Globals.Bot = bot; _client = new DiscordSocketClient(new DiscordSocketConfig { // How much logging do you want to see? LogLevel = LogSeverity.Info, // If you or another service needs to do anything with messages // (eg. checking Reactions, checking the content of edited/deleted messages), // you must set the MessageCacheSize. You may adjust the number as needed. //MessageCacheSize = 50, }); _commands = new CommandService(new CommandServiceConfig { // Again, log level: LogLevel = LogSeverity.Info, // This makes commands get run on the task thread pool instead on the websocket read thread. // This ensures long running logic can't block the websocket connection. DefaultRunMode = RunMode.Sync, // There's a few more properties you can set, // for example, case-insensitive commands. CaseSensitiveCommands = false, }); // Subscribe the logging handler to both the client and the CommandService. _client.Log += Log; _commands.Log += Log; // Setup your DI container. _services = ConfigureServices(); }
public async Task InitCommands(DiscordBotConfig cfg) { var assembly = Assembly.GetExecutingAssembly(); await _commands.AddModulesAsync(assembly, _services).ConfigureAwait(false); var modules = _commands.Modules.ToList(); var blacklist = cfg.ModuleBlacklist .Replace("Module", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(z => z.Trim()).ToList(); foreach (var module in modules) { var name = module.Name.Replace("Module", ""); if (blacklist.Any(z => z.Equals(name, StringComparison.OrdinalIgnoreCase))) { await _commands.RemoveModuleAsync(module).ConfigureAwait(false); } } // Subscribe a handler to see if a message invokes a command. _client.MessageReceived += HandleMessageAsync; }