public Bot(BotConfig config) { _config = config; _discordConfig = new DiscordConfiguration { Token = _config.BotToken, TokenType = TokenType.Bot, AutoReconnect = true, LogLevel = LogLevel.Debug, UseInternalLogHandler = true }; _client = new DiscordClient(_discordConfig); _client.Ready += this.Client_Ready; _client.ClientErrored += this.Client_Error; _client.MessageCreated += this.Client_OnMessage; _mailClient = new SmtpClient(_config.SmtpSettings.SmtpServer) { Port = _config.SmtpSettings.SmtpPort, Credentials = new NetworkCredential(_config.SmtpSettings.Username, _config.SmtpSettings.Password), EnableSsl = _config.SmtpSettings.Ssl }; _context = new AuthContext(); }
public static async Task Connect(string token) { if (string.IsNullOrWhiteSpace(token)) { throw new ArgumentException("No token provided."); } Client = new DSharpPlus.DiscordClient(new DSharpPlus.DiscordConfiguration() { Token = token, Intents = DSharpPlus.DiscordIntents.Guilds, MinimumLogLevel = Microsoft.Extensions.Logging.LogLevel.Warning }); OperationTracker = new TaskCompletionSource(); Guilds.Clear(); try { Client.GuildDownloadCompleted += GuildDownloadCompleted; await Client.ConnectAsync(); } catch (DSharpPlus.Exceptions.UnauthorizedException) { Client = null; throw new ArgumentException("Invalid token provided."); } await ConnectFinished; }
public async Task RunAsync() { var json = string.Empty; using (var fs = System.IO.File.OpenRead("config.json")) using (var sr = new StreamReader(fs, new UTF8Encoding(false))) json = await sr.ReadToEndAsync().ConfigureAwait(false); var configJson = JsonConvert.DeserializeObject <ConfigJson>(json); var config = new DiscordConfiguration { Token = configJson.Token, TokenType = TokenType.Bot, AutoReconnect = true, LogLevel = LogLevel.Debug, UseInternalLogHandler = true }; Client = new DSharpPlus.DiscordClient(config); Client.Ready += OnClientReady; var commandsConfig = new CommandsNextConfiguration { StringPrefix = configJson.Prefix, EnableMentionPrefix = true, }; Commands = Client.UseCommandsNext(commandsConfig); await Client.ConnectAsync(); await Task.Delay(-1); }
private static Task GuildDownloadCompleted( DSharpPlus.DiscordClient sender, DSharpPlus.EventArgs.GuildDownloadCompletedEventArgs e) { Task.Run(() => ConstructDiscordGuildCollection()) .ContinueWith((a) => OperationFinished(a)); return(Task.CompletedTask); }