private async Task Client_MessageReceived(SocketMessage msg) { // TODO: Implement error catching, and ensure that the report was actually forwarded before replying. // TODO: Check if there is more than one staff role in use, or if standard members should also be allowed to report // Optional: Implement a command to reply from the manager channel directly to the DM to simulate // a group DM. Or maybe it'd be easier to create a real group DM and keep the bot simple. if (msg.Author.Id == socketClient.CurrentUser.Id || msg.Author.IsBot) { return; // Don't respond to our own messages or other bots to help prevent loops } if ((msg.Channel as SocketGuildChannel) == null) // Check that this is a DM (no guild associated with it) { SocketGuildUser socketUser = socketClient.GetGuild(config.MainGuild).GetUser(msg.Author.Id); // Attempt to see if the user object is cached and in the target guild RestGuildUser restUser = null; List <ulong> roles = new List <ulong>(); if (socketUser != null) // If the user is in our guild, store their roles { roles.AddRange(socketUser.Roles.Select(x => x.Id)); } else // Otherwise ensure it isn't a caching issue by attempting to manually download the user { restUser = await restClient.GetGuildUserAsync(config.MainGuild, msg.Author.Id); } if (roles.Count() == 0 && restUser == null) { return; // The user isn't in the server } else if (restUser != null) // Store the roles from the downloaded user { roles.AddRange(restUser.RoleIds); } if (!roles.Contains(config.StaffRole)) { return; // Return if the user isn't a staff member } // At this point we've determined that the user is in the server and has the staff role string source = $"Message receieved from {msg.Author.Username}#{msg.Author.Discriminator} [{msg.Author.Id}]"; // Information about who sent it to prevent abuse if (msg.Content.Length + source.Length + 2 < 2000) // Make sure we're not about to send a message larger than Discord allows { await(socketClient.GetChannel(config.ManagerChannel) as SocketTextChannel).SendMessageAsync($"{source}\n{msg.Content}"); } else { await(socketClient.GetChannel(config.ManagerChannel) as SocketTextChannel).SendMessageAsync(source); await(socketClient.GetChannel(config.ManagerChannel) as SocketTextChannel).SendMessageAsync(msg.Content); } await msg.Channel.SendMessageAsync($"Thank you for your report {msg.Author.Mention}, it has been forwarded to the relevant channel."); } }
protected override void CycleThink() { if (World.GameConfiguration.DiscordGuildID != null && World.GameConfiguration.DiscordToken != null) { foreach (var player in Player.GetWorldPlayers(this.World) .Where(p => !string.IsNullOrEmpty(p.Token) && !p.AuthenticationStarted)) { player.AuthenticationStarted = true; Task.Run(async() => { using (var drc = new DiscordRestClient()) { try { await drc.LoginAsync(Discord.TokenType.Bearer, player.Token); if (drc.CurrentUser != null) { var playerRoles = new List <string>(); var userId = drc.CurrentUser.Id; await drc.LoginAsync(Discord.TokenType.Bot, World.GameConfiguration.DiscordToken); var user = await drc.GetGuildUserAsync(World.GameConfiguration.DiscordGuildID.Value, userId); var guild = await drc.GetGuildAsync(World.GameConfiguration.DiscordGuildID.Value); foreach (var roleID in user.RoleIds) { var role = guild.Roles.FirstOrDefault(r => r.Id == roleID); if (role != null) { playerRoles.Add(role.Name); } } player.LoginName = user.Username; player.LoginID = user.Id; player.Roles = playerRoles; player.Avatar = $"https://cdn.discordapp.com/avatars/{user.Id}/{user.AvatarId}.png?size=128"; player.OnAuthenticated(); } } catch (Exception) { } } }); } } }
public async Task <Option <IGuildUser> > GetOrSetAndGet(ulong userId, ulong guildId) { var user = this.Get(userId, guildId); if (user.HasValue) { return(user); } // Do rest request var userResp = await _restClient.GetGuildUserAsync(guildId, userId).ConfigureAwait(false); if (userResp == null) { return(Option.None <IGuildUser>()); } // Otherwise set cache _cacheService.Set(CacheId.GetGuildUser(userId, guildId), userResp, TimeSpan.FromMinutes(_USER_TTL_MINS)); return(Option.Some <IGuildUser>(userResp)); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new ApplicationUser { UserName = info.DefaultUserName, Email = info.DefaultUserName }; DiscordRestClient dcli = new DiscordRestClient(new DiscordRestConfig { LogLevel = Discord.LogSeverity.Info }); await dcli.LoginAsync(Discord.TokenType.Bot, ConfigHelper.Instance.DiscordBotToken); ulong discordUserId = Convert.ToUInt64(info.ExternalIdentity.Claims.First().Value); ulong guildId = Convert.ToUInt64(ConfigHelper.Instance.DiscordGuildID); RestGuildUser guser = dcli.GetGuildUserAsync(guildId, discordUserId).Result; if (guser != null) { RestGuild guild = dcli.GetGuildAsync(guildId).Result; List <string> roleNames = guild.Roles.Where(x => guser.RoleIds.Contains(x.Id)).Select(x => x.Name).ToList(); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext())); foreach (string roleName in roleNames) { if (!roleManager.RoleExists(roleName)) { roleManager.Create(new IdentityRole(roleName)); } } var result = await UserManager.CreateAsync(user); if (result.Succeeded) { await UserManager.AddToRolesAsync(user.Id, roleNames.ToArray()); result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public async Task GetGuildUserAsync(string irrelevant) { IGuildUser testUser = await _clientRest.GetGuildUserAsync(Context.Guild.Id, Context.Message.MentionedUsers.First().Id); await ReplyAsync(testUser.ToString()); }
private async Task Client_ReactionAdded(Cacheable <IUserMessage, ulong> cache, Cacheable <IMessageChannel, ulong> channelCache, SocketReaction reaction) { if (reaction.Channel.Id == 431953417024307210 && RoleColors.ContainsKey(reaction.Emote.Name)) { var user = ((SocketGuildUser)reaction.User); if (user.Roles.Select(x => x.Id).ToList().Contains(957765545086828616)) { return; } //await RemoveAllColors(user.Id); var restUser = await restClient.GetGuildUserAsync(user.Guild.Id, user.Id); var roles = restUser.RoleIds.ToList(); roles = roles.Where(x => !RoleColors.ContainsValue(x) && x != restUser.GuildId).ToList(); roles.Add(RoleColors[reaction.Emote.Name]); await restUser.ModifyAsync(x => x.RoleIds = roles); //if (!user.Roles.Contains(user.Guild.GetRole(RoleColors[reaction.Emote.Name]))) //{ // lastRole = DateTimeOffset.Now; // RoleAdditions.Enqueue(new RoleAddition() { userId = user.Id, roleId = RoleColors[reaction.Emote.Name] }); // //await user.AddRoleAsync(user.Guild.GetRole(RoleColors[reaction.Emote.Name])); //} } //else if (reaction.Channel.Id == 431953417024307210 && reaction.Emote.Name == "🚫") //{ // var user = ((SocketGuildUser)reaction.User); // foreach (var r in user.Roles.Where(x => RoleColors.ContainsValue(x.Id))) // { // await user.RemoveRoleAsync(r); // } //} else if (reaction.Channel.Id == 431953417024307210) { SocketRole role = null; var user = ((SocketGuildUser)reaction.User); if (user.Roles.Select(x => x.Id).ToList().Contains(957765545086828616)) { return; } switch (reaction.Emote.Name) { case "NoU": role = user.Guild.GetRole(498078860517048331); break; case "pitchpls": role = user.Guild.GetRole(639924839091535920); break; case "friendheartred": role = user.Guild.GetRole(749344457345728593); break; case "friendheartteal": role = user.Guild.GetRole(749344359467581476); break; case "friendheartorange": role = user.Guild.GetRole(749345361855905902); break; case "friendheartgrey": role = user.Guild.GetRole(749344514963144714); break; case "PANTS": role = user.Guild.GetRole(759219920771874877); break; case "⛏️": role = user.Guild.GetRole(948944486602534912); break; case "borbDoodle": role = user.Guild.GetRole(904533609346654249); break; } if (role == null) { return; } if (!user.Roles.Contains(role)) { await user.AddRoleAsync(role); } } //else if (reaction.Channel.Id == 431953417024307210 && reaction.Emote.Name == "NoU") //{ // var user = ((SocketGuildUser)reaction.User); // if (!user.Roles.Contains(user.Guild.GetRole(498078860517048331))) // await user.AddRoleAsync(user.Guild.GetRole(498078860517048331)); //} //else if (reaction.Channel.Id == 431953417024307210 && reaction.Emote.Name == "pitchpls") //{ // var user = ((SocketGuildUser)reaction.User); // if (!user.Roles.Contains(user.Guild.GetRole(639924839091535920))) // await user.AddRoleAsync(user.Guild.GetRole(639924839091535920)); //} }