private async Task OnReady() { int users = _client.Guilds.Sum(g => g.MemberCount); await _client.SetGameAsync($"/help with {users} users", null, type : ActivityType.Listening); await _client.DownloadUsersAsync(_client.Guilds); }
private async Task OnUserVoiceStateUpdated(SocketUser user, SocketVoiceState before, SocketVoiceState after) { if (Channel == null) { return; } if (user.Id == _client.CurrentUser.Id) { return; } //Ensure we can get all users in this channel await _client.DownloadUsersAsync(new [] { Channel.Guild }); //If there are other users in the channel stay in the channel var count = await Channel.GetUsersAsync().Select(a => a.Count).Sum(); if (count > 1) { return; } //No one is listening :( await Stop(); }
// Fire if new user connect and download all member again private Task UserJoined(SocketGuildUser user) { t.CWLTextColor("User: "******"Joined the Guild.", ConsoleColor.Yellow); LogMain("User: "******"Joined the Guild.", LogLevel.Log); Task.Run(async() => { await _client.DownloadUsersAsync(_client.Guilds); }); t.CWLTextColor("All Users downloaded", ConsoleColor.Yellow); LogMain("All Users downloaded", LogLevel.Log); return(Task.CompletedTask); }
async Task LoginToDiscord(string token) { if (testMode) { return; } // Remember to keep token private or to read it from an // external source! In this case, we are reading the token // from an environment variable. If you do not know how to set-up // environment variables, you may find more information on the // Internet or by using other methods such as reading from // a configuration. await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); await client.DownloadUsersAsync(client.Guilds); }
private async Task Client_ShardReady_DownloadUsers(DiscordSocketClient arg) { //method not thread safe, but it's ok. if (ShardsDownloadingUsers.Contains(arg.ShardId)) { return; } try { ShardsDownloadingUsers.Add(arg.ShardId); await Task.Delay(5000); await arg.DownloadUsersAsync(arg.Guilds); int users = arg.Guilds.Sum(x => x.Users.Count); _ = WebhookClients.NamikoLogChannel.SendMessageAsync($":space_invader: `{DateTime.Now:HH:mm:ss}` - `Shard {arg.ShardId} downloaded {users} users.`"); } finally { ShardsDownloadingUsers.Remove(arg.ShardId); } }
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); }; }