public bool Equals(LfgUser other) { return(other != null && Username.Equals(other.Username) && Discriminator.Equals(other.Discriminator) && DiscordId.Equals(other.DiscordId)); }
public async Task <bool> StartTriviaAsync(int questionsToPlay, SocketCommandContext context) { // Create a combination of guildID and channelID to form a unique identifier for each trivia instance var id = new DiscordId(context.Guild.Id, context.Channel.Id, null); if (!trivias.ContainsKey(id)) { _ = trivias.TryAdd(id, new OTDBTriviaInstance(context, dbContext, clientFactory)); } return(trivias.TryGetValue(id, out OTDBTriviaInstance instance) ? await instance.StartTriviaAsync(questionsToPlay).ConfigureAwait(false) : false); }
/// <summary> /// Gets the current global high scores for the guild /// </summary> /// <param name="context">Context of the channel where the high score was requested</param> /// <returns></returns> public async Task <string> GetGlobalHighScoresAsync(int amount, SocketCommandContext context) { var id = new DiscordId(context.Guild.Id, context.Channel.Id, null); if (id.GuildId == null || !trivias.ContainsKey(id)) { using var trivia = new OTDBTriviaInstance(context, dbContext, clientFactory); return(await trivia.GetGlobalHighScoresAsync(amount, id.GuildId.Value).ConfigureAwait(false)); } else { return(await trivias[id].GetGlobalHighScoresAsync(amount, id.GuildId.Value).ConfigureAwait(false)); } }
/// <summary> /// Stops the trivia in the specified guild's channel if a trivia is running, otherwise returns false /// </summary> /// <param name="id">Combined Id of the Discord Guild and channel for the trivia</param> /// <returns>success</returns> public async Task <bool> StopTriviaAsync(DiscordId id) { if (!trivias.ContainsKey(id)) { return(false); } else { var result = await trivias[id].StopTriviaAsync().ConfigureAwait(false); if (trivias.TryRemove(id, out var instance)) { instance.Dispose(); } return(result); } }
/// <summary> /// Skips the trivia in the specified guild's channel if a trivia is running, otherwise returns false /// </summary> /// <param name="id">Combined Id of the Discord Guild and channel for the trivia</param> /// <returns>success</returns> public async Task <bool> SkipQuestionAsync(DiscordId id) { return(trivias.ContainsKey(id) ? await trivias[id].SkipQuestionAsync().ConfigureAwait(false) : false); }
public async Task <IActionResult> LoginWithBot([FromQuery] string token) { string DiscordIdFromLink; bool isValid; // Validate the token. try { string decryptedToken = StringCipher.Decrypt(token, _configuration["TokenPassword"]); if (Hmac.VerifyMessage(decryptedToken, _configuration["TokenKey"])) { isValid = true; DiscordIdFromLink = Hmac.DecodeMessage(decryptedToken); } else { return(BadRequest()); } } catch { return(BadRequest()); } // Handle login. if (isValid) { StreamNightUser user = await _userManager.FindByNameAsync(DiscordIdFromLink); bool signInUser = true; if (user == null) { signInUser = false; // User doesn't have an account, let's transparently create one and log them in. ulong.TryParse(DiscordIdFromLink, out ulong DiscordId); user = new StreamNightUser { UserName = DiscordId.ToString(), DiscordId = DiscordId }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { signInUser = true; _logger.LogInformation("Created a new user from a Discord link."); } } if (signInUser) { // Check if the user should be in the administrator role try { bool hasStreamRole = false; bool hasAdminRole = false; // Get the user's roles from the Discord bot DSharpPlus.Entities.DiscordMember member = await _discordBot.DiscordClient.GetMemberById(user.DiscordId); foreach (DSharpPlus.Entities.DiscordRole discordRole in member.Roles) { // If the user has the stream role if (discordRole.Name == _discordBot.DiscordClient.StreamRole) { hasStreamRole = true; } if (discordRole.Name == _discordBot.DiscordClient.AdminRole) { hasAdminRole = true; } } // Check if the user is in the stream role already bool userInStreamRole = await _userManager.IsInRoleAsync(user, "StreamController"); if (!userInStreamRole && hasStreamRole) { // Add the stream role to the user await _userManager.AddToRoleAsync(user, "StreamController"); } else if (userInStreamRole && !hasStreamRole) { // Remove the stream role from the user await _userManager.RemoveFromRoleAsync(user, "StreamController"); } // Check if the user is in the admin role already bool userInAdmin = await _userManager.IsInRoleAsync(user, "Administrator"); if (!userInAdmin && hasAdminRole) { // Add the admin role to the user await _userManager.AddToRoleAsync(user, "Administrator"); } else if (userInAdmin && !hasAdminRole) { // Remove the admin role from the user await _userManager.RemoveFromRoleAsync(user, "Administrator"); } } // The user might not exist, catch the exception and do nothing catch (NullReferenceException) { } await _signInManager.SignInAsync(user, isPersistent : false, authenticationMethod : "DiscordLink"); _logger.LogInformation("User logged in using a Discord link."); } return(LocalRedirect(returnUrl)); } return(BadRequest()); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { ulong.TryParse(info.Principal.FindFirstValue(ClaimTypes.NameIdentifier), out ulong DiscordId); var user = new StreamNightUser { UserName = DiscordId.ToString(), DiscordId = DiscordId }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { try { // Get the user's roles from the Discord bot DSharpPlus.Entities.DiscordMember member = await _discordBot.DiscordClient.GetMemberById(user.DiscordId); foreach (DSharpPlus.Entities.DiscordRole discordRole in member.Roles) { // If the user has the stream role if (discordRole.Name == _discordBot.DiscordClient.StreamRole) { await _userManager.AddToRoleAsync(user, "StreamController"); } if (discordRole.Name == _discordBot.DiscordClient.AdminRole) { await _userManager.AddToRoleAsync(user, "Administrator"); } } } // The user might not exist, catch the exception and do nothing catch (NullReferenceException) { } result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } else if (result.Errors.Where(e => e.Code == "DuplicateUserName").Any()) { var existingUser = await _userManager.FindByNameAsync(DiscordId.ToString()); var addLoginResult = await _userManager.AddLoginAsync(existingUser, info); if (addLoginResult.Succeeded) { await _signInManager.SignInAsync(existingUser, isPersistent : false); return(LocalRedirect(returnUrl)); } } else { ViewData["ResultMessage"] = "Couldn't sign in with Discord. Try logging in with a DM instead."; } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public string Echo(DiscordId id) { return(id.ToString()); }