public FasBotFramework() { var socketConfig = new DiscordSocketConfig { RestClientProvider = DefaultRestClientProvider.Create(useProxy: false) }; _client = new DiscordSocketClient(socketConfig); _commands = new CommandService(); _commandHandler = new CommandHandler(_client, _commands); _client.Log += Log; _commands.Log += Log; _services = ConfigureServices(); }
public DiscordInput(string token, bool testMode = false) { if (testMode) { this.testMode = testMode; return; } var cfg = new DiscordSocketConfig(); cfg.AlwaysDownloadUsers = true; cfg.RestClientProvider = DefaultRestClientProvider.Create(useProxy: true); client = new DiscordSocketClient(cfg); // client.MessageReceived += MessageReceived; Commands = new CommandService(); // commandHandler = new CommandHandler(client, commands); // Subscribe the logging handler to both the client and the CommandService. client.Log += Log; Commands.Log += Log; // Source: https://github.com/Aux/Discord.Net-Example // Create a new instance of a service collection var services = new ServiceCollection(); ConfigureServices(services, client, Commands); // Build the service provider provider = services.BuildServiceProvider(); // Start the command handler service provider.GetRequiredService <CommandHandler>(); LoginToDiscord(token); // do not call any further commands from the client here client.Ready += async() => { this.IsReady = true; await Log(new LogMessage(LogSeverity.Info, "Program", $"Bot started. Initialisation time was {Program.InitTime}ms")); while (LogQueue.Count > 0) { await SendLog(); } }; client.UserJoined += async(user) => { Program.CurLeaderboard.LatestJoinedPlayer = (user.Username, user.Id); Program.Controller.SerializeLeaderboard(); // send message to help managers auto add players var commandsChnl = Program.Config.GetCommandChannel(); if (commandsChnl != null) { var embed = new EmbedBuilder() .WithThumbnailUrl(user.GetAvatarUrl()) .WithColor(Discord.Color.Blue) .WithTitle("New user: "******"#" + user.DiscriminatorValue) .WithFooter("ID: " + user.Id); var nl = Environment.NewLine; embed.Description = $"Auto-add {user.Mention} to the leaderboard using: {nl}{nl} `!autocp [name]`"; await SendMessage("", commandsChnl, embed.Build()); } await client.DownloadUsersAsync(client.Guilds); }; }
// Long-running worker thread. private async Task WorkerAsync(Func <DiscordRestConfig, DiscordWebhookClient> clientFactory, CancellationToken ct) { var config = new DiscordRestConfig { RestClientProvider = DefaultRestClientProvider.Create(true) }; #if DEBUG config.LogLevel = LogSeverity.Info; #endif using var client = clientFactory(config); #if DEBUG client.Log += log => { Debug.WriteLine(log.ToString()); return(Task.CompletedTask); }; #endif var messageBuffer = new List <Embed>(); do { try { await Task.Delay(_RequestThrottleTime, ct); } catch (OperationCanceledException) { // cancelled from Delay } try { // Take 1 // Consider the case where ct is cancelled and we are draining the queue. if (!impendingMessagesSemaphore.Wait(0)) { await impendingMessagesSemaphore.WaitAsync(ct); } } catch (OperationCanceledException) { // cancelled from WaitAsync } for (int i = 0; i < _MaxMessagesPerPack; i++) { // Consume 1 var result = impendingMessages.TryDequeue(out var embed); Debug.Assert(result); if (result) { messageBuffer.Add(embed); } // Take another if (!impendingMessagesSemaphore.Wait(0)) { break; } } await client.SendMessageAsync(embeds : messageBuffer); messageBuffer.Clear(); } while (!ct.IsCancellationRequested || impendingMessagesSemaphore.CurrentCount > 0); }