public async Task CheckLimit(SocketCommandContext context, SocketUser user) { using (var soraContext = new SoraContext()) { var userDb = Utility.OnlyGetUser(user.Id, soraContext); if (userDb == null) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.PurpleEmbed, Utility.SuccessLevelEmoji[4], $"💍 {Utility.GiveUsernameDiscrimComb(user)} has a limit of 1. Married to 0 users")); return; } int marryLimit = ((int)(Math.Floor((double)(ExpService.CalculateLevel(userDb.Exp) / 10)))) + 1; await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.PurpleEmbed, Utility.SuccessLevelEmoji[4], $"💍 {Utility.GiveUsernameDiscrimComb(user)} has a limit of {marryLimit}. Married to {userDb.Marriages.Count} users")); } }
public async Task <bool> CanAddNewPlaylist(SocketCommandContext context, User userDb) { if (context.User.Id == Utility.OWNER_ID)//backdoor so i can add as many as i want { return(true); } int level = ExpService.CalculateLevel(userDb.Exp); int amountGranted = (int)Math.Floor((double)((level - (MIN_LEVEL - NEED_FOR_EXTRA_PLAYLIST)) / NEED_FOR_EXTRA_PLAYLIST)); int amountLeft = amountGranted - userDb.ShareCentrals.Count; if (amountLeft > 0) { return(true); } await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback( Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "You reached your playlist limit. You gain another slot every 2 levels!")); return(false); }
public async Task Marry(SocketCommandContext context, SocketUser user) { //Check if its urself if (user.Id == context.User.Id) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"You can't and shouldn't marry yourself ;_;")); return; } using (var soraContext = new SoraContext()) { var requestorDb = Utility.GetOrCreateUser(context.User.Id, soraContext); var askedDb = Utility.GetOrCreateUser(user.Id, soraContext); int allowedMarriagesRequestor = ((int)(Math.Floor((double)(ExpService.CalculateLevel(requestorDb.Exp) / 10)))) + 1; int allowedMarriagesAsked = ((int)(Math.Floor((double)(ExpService.CalculateLevel(askedDb.Exp) / 10)))) + 1; //check both limits if (requestorDb.Marriages.Count >= allowedMarriagesRequestor) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"{Utility.GiveUsernameDiscrimComb(context.User)}, you already reached your marriage limit. Level up to increase it")); return; } if (askedDb.Marriages.Count >= allowedMarriagesAsked) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"{Utility.GiveUsernameDiscrimComb(user)} already reached their marriage limit. They must level up to increase the limit")); //TODO this sounds like shit. change it return; } //Check for duplicate if (requestorDb.Marriages.Any(x => x.PartnerId == user.Id) || askedDb.Marriages.Any(x => x.PartnerId == context.User.Id)) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"You cannot marry someone twice!")); return; } //Proceed to ask for marriage var msg = await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.PurpleEmbed, Utility.SuccessLevelEmoji[4], $"{Utility.GiveUsernameDiscrimComb(user)}, do you want to marry {Utility.GiveUsernameDiscrimComb(context.User)}? 💍")); Criteria <SocketMessage> criteria = new Criteria <SocketMessage>(); criteria.AddCriterion(new EnsureFromUserInChannel(user.Id, context.Channel.Id)); var response = await _interactive.NextMessageAsync(context, criteria, TimeSpan.FromSeconds(45)); if (response == null) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"{Utility.GiveUsernameDiscrimComb(user)} didn't answer in time >.<")); return; } if ((!response.Content.Contains(" yes ", StringComparison.OrdinalIgnoreCase) && !response.Content.Contains(" yes,", StringComparison.OrdinalIgnoreCase) && !response.Content.Contains("yes ", StringComparison.OrdinalIgnoreCase) && !response.Content.Contains("yes,", StringComparison.OrdinalIgnoreCase)) && !response.Content.Equals("yes", StringComparison.OrdinalIgnoreCase)) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"{Utility.GiveUsernameDiscrimComb(user)} didn't answer with a yes ˚‧º·(˚ ˃̣̣̥᷄⌓˂̣̣̥᷅ )‧º·˚")); return; } //Answer contains a yes requestorDb.Marriages.Add(new Marriage() { PartnerId = user.Id, Since = DateTime.UtcNow }); //_soraContext.SaveChangesThreadSafe(); askedDb.Marriages.Add(new Marriage() { PartnerId = context.User.Id, Since = DateTime.UtcNow }); await soraContext.SaveChangesAsync(); } await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.PurpleEmbed, Utility.SuccessLevelEmoji[4], $"You are now married 💑").WithImageUrl("https://media.giphy.com/media/iQ5rGja9wWB9K/giphy.gif")); }
public async Task SharePlaylist(SocketCommandContext context, string shareUrl, string title, string tags, bool isPrivate) { using (var soraContext = new SoraContext()) { var userDb = Utility.OnlyGetUser(context.User.Id, soraContext); if (userDb == null || ExpService.CalculateLevel(userDb.Exp) < MIN_LEVEL) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"You need to be at least lvl {MIN_LEVEL} to share playlists!")); return; } if (await CanAddNewPlaylist(context, userDb) == false) { return; } if (!shareUrl.StartsWith("https://hastebin.com/")) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "The link must be a valid hastebin link!")); return; } if (!shareUrl.EndsWith(".sora") && !shareUrl.EndsWith(".fredboat")) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Must be an originaly exported sora or fredboat playlist!") .WithDescription( $"Use `{Utility.GetGuildPrefix(context.Guild, soraContext)}export` when you have a Queue! This is to minimize errors.")); return; } if (shareUrl.EndsWith(".fredboat")) { shareUrl = shareUrl.Replace(".fredboat", ".sora"); } if (soraContext.ShareCentrals.Any(x => x.ShareLink == shareUrl)) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Playlist already exists!")); return; } string[] seperatedTags; if (tags.IndexOf(";", StringComparison.Ordinal) < 1) { seperatedTags = new[] { tags }; } else { seperatedTags = tags.Split(";"); } List <string> betterTags = new List <string>(); foreach (var tag in seperatedTags) { string finalTag = tag; if (finalTag.Contains(";")) { finalTag = finalTag.Replace(";", ""); } if (!string.IsNullOrWhiteSpace(finalTag)) { betterTags.Add(finalTag.Trim()); } } if (betterTags.Count < 1) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Add at least one Tag!").WithDescription("Tags must be added like this: `trap;edm;chill music;other`")); return; } if (betterTags.Count > 10) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Please dont exceed 10 tags!")); return; } string joinedTags = String.Join(";", betterTags); var eb = new EmbedBuilder() { Color = Utility.BlueInfoEmbed, Title = $"{Utility.SuccessLevelEmoji[3]} Are you sure you want share this? y/n", Description = $"{shareUrl}\n" + $"You can change the Title and Tags afterwards but never the playlist link!", Author = new EmbedAuthorBuilder() { IconUrl = context.User.GetAvatarUrl() ?? Utility.StandardDiscordAvatar, Name = Utility.GiveUsernameDiscrimComb(context.User) } }; eb.AddField(x => { x.IsInline = false; x.Name = "Title"; x.Value = title; }); eb.AddField(x => { x.IsInline = true; x.Name = "Tags"; x.Value = joinedTags.Replace(";", " - "); }); eb.AddField(x => { x.IsInline = true; x.Name = "Is Private?"; x.Value = $"{(isPrivate ? "Yes" : "No")}"; }); var msg = await context.Channel.SendMessageAsync("", embed : eb); var response = await _interactive.NextMessageAsync(context, true, true, TimeSpan.FromSeconds(45)); await msg.DeleteAsync(); if (response == null) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Didn't answer in time ;_;")); return; } if (response.Content.Equals("y", StringComparison.OrdinalIgnoreCase) || response.Content.Equals("yes", StringComparison.OrdinalIgnoreCase)) { userDb.ShareCentrals.Add(new ShareCentral() { CreatorId = context.User.Id, Downvotes = 0, Upvotes = 0, ShareLink = shareUrl, Titel = title, IsPrivate = isPrivate, Tags = joinedTags }); await soraContext.SaveChangesAsync(); await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.GreenSuccessEmbed, Utility.SuccessLevelEmoji[0], $"Successfully {(isPrivate ? "saved" : "shared")} playlist (ノ◕ヮ◕)ノ*:・゚✧")); } else { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Didn't answer with y or yes! Discarded changes")); } } }
public async Task VotePlaylist(SocketCommandContext context, string url, bool vote) { using (var soraContext = new SoraContext()) { var userDb = Utility.OnlyGetUser(context.User.Id, soraContext); if (userDb == null || ExpService.CalculateLevel(userDb.Exp) < MIN_LEVEL) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"You need to be at least lvl {MIN_LEVEL} to vote on playlists!")); return; } var playlistDb = soraContext.ShareCentrals.FirstOrDefault(x => x.ShareLink == url); if (playlistDb == null) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "There is no shared playlist with that Url!")); return; } //First check if it was ever voted on var voteDb = soraContext.Votings.FirstOrDefault(x => x.ShareLink == url && x.VoterId == context.User.Id); if (voteDb == null) { userDb.Votings.Add(new Voting() { ShareLink = playlistDb.ShareLink, UpOrDown = vote, VoterId = context.User.Id }); if (vote) //UPVOTED { playlistDb.Upvotes++; await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.GreenSuccessEmbed, Utility.SuccessLevelEmoji[0], "Successfully UPVOTED playlist")); } else //DOWNVOTED { playlistDb.Downvotes++; await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.GreenSuccessEmbed, Utility.SuccessLevelEmoji[0], "Successfully DOWNVOTED playlist")); } await soraContext.SaveChangesAsync(); } else { if (voteDb.UpOrDown == vote) { await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "You already voted this playlist with this vote! You can change your vote though!")); return; } voteDb.UpOrDown = vote; if (vote) //UPVOTE { playlistDb.Downvotes--; playlistDb.Upvotes++; await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.GreenSuccessEmbed, Utility.SuccessLevelEmoji[0], "Successfully updated vote to UPVOTED!")); } else//DOWNVOTE { playlistDb.Downvotes++; playlistDb.Upvotes--; await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.GreenSuccessEmbed, Utility.SuccessLevelEmoji[0], "Successfully updated vote to DOWNVOTED!")); } await soraContext.SaveChangesAsync(); } } }
public async Task OnUserExpGain(int epGain, SocketCommandContext context) { using (var soraContext = new SoraContext()) { var user = context.User as SocketGuildUser; if (user == null) { Console.WriteLine("ROLE USER NULL"); return; } var guildDb = Utility.GetOrCreateGuild(context.Guild.Id, soraContext); var banned = guildDb.LevelRoles.Where(x => x.Banned).ToList(); //check if user has any of the banned roles if (banned.Count != 0) { if (banned.Any(role => user.Roles.Any(x => x.Id == role.RoleId))) { return; } //No banned roles. } //user can gain EXP var guildUser = Utility.GetOrCreateGuildUser(user.Id, context.Guild.Id, soraContext); int previousLevel = ExpService.CalculateLevel(guildUser.Exp); guildUser.Exp += epGain; await soraContext.SaveChangesAsync(); //gained ep, do the rest int currentLevel = ExpService.CalculateLevel(guildUser.Exp); //if no lvl up occured, there is no need to do anything. if (previousLevel == currentLevel) { return; } //there are no level roles if (guildDb.LevelRoles.Count == 0) { return; } //send lvl up message if (guildDb.EnabledLvlUpMessage) { var msg = (string.IsNullOrWhiteSpace(guildDb.LevelUpMessage) ? DEFAULT_MSG : guildDb.LevelUpMessage); msg = EditMsg(msg, user, currentLevel); try { if (guildDb.SendLvlDm) { var eb = new EmbedBuilder() { Author = new EmbedAuthorBuilder() { IconUrl = context.Guild.IconUrl ?? Utility.StandardDiscordAvatar, Name = context.Guild.Name }, Description = msg, Color = Utility.BlueInfoEmbed }; await(await user.GetOrCreateDMChannelAsync()).SendMessageAsync("", embed: eb); } else { await context.Channel.SendMessageAsync(msg); } } catch (Exception e) { Console.WriteLine(e); } } //Give roles var roles = guildDb.LevelRoles.Where(x => x.RequiredLevel == currentLevel && !x.Banned).ToList(); if (roles.Count == 0) { return; } //Check if sora has manage roles permission! var sora = context.Guild.CurrentUser; if (!sora.GuildPermissions.Has(GuildPermission.ManageRoles)) { //try to send a DM to the owner try { await(await context.Guild.Owner.GetOrCreateDMChannelAsync()).SendMessageAsync("", embed: Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"Sora is missing crucial permissions!") .WithDescription($"You've set up Roles as level rewards but Sora is missing the Manage Roles permission in {context.Guild.Name}! " + $"{Utility.GiveUsernameDiscrimComb(context.User)} earned a new role but couldn't receive it due to the missing permissions! " + $"This message will be sent to you every time a user levels up and would receive a new role. Either remove the role rewards or grant Sora the missing permissions!")); } catch (Exception e) { Console.WriteLine(e); } await soraContext.SaveChangesAsync(); return; } foreach (var role in roles) { //check if he already has it if (user.Roles.Any(x => x.Id == role.RoleId)) { continue; } //if not add var addRole = context.Guild.GetRole(role.RoleId); //check if role still exists if (addRole == null) { //remove role from list of roles and continue guildDb.LevelRoles.Remove(role); continue; } await user.AddRoleAsync(addRole); } await soraContext.SaveChangesAsync(); } }
public async Task DrawProfileCard(SocketCommandContext context, SocketUser user) { using (var soraContext = new SoraContext()) { try { var userDb = Utility.GetOrCreateUser(user.Id, soraContext); //Check for cooldown var requestorDb = Utility.GetOrCreateUser(context.User.Id, soraContext); if (requestorDb.ShowProfileCardAgain.CompareTo(DateTime.UtcNow) < 0) { requestorDb.ShowProfileCardAgain = DateTime.UtcNow.AddSeconds(15); await soraContext.SaveChangesAsync(); } else { var remainingSeconds = requestorDb.ShowProfileCardAgain.Subtract(DateTime.UtcNow.TimeOfDay).Second; await context.Channel.SendMessageAsync("", embed : Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], $"Dont break me >.< Please wait another {remainingSeconds} seconds!")); return; } Uri requestUri = new Uri(user.GetAvatarUrl() ?? Utility.StandardDiscordAvatar); //remove temporary avatar file if it already exists if (File.Exists($"ProfileData/{user.Id}Avatar.png")) { File.Delete($"ProfileData/{user.Id}Avatar.png"); } //Get the user avatar using (var client = new HttpClient()) using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) using (Stream contentStream = await(await client.SendAsync(request)).Content.ReadAsStreamAsync(), stream = new FileStream($"ProfileData/{user.Id}Avatar.png", FileMode.Create, FileAccess.Write, FileShare.None, 3145728, true)) { await contentStream.CopyToAsync(stream); await contentStream.FlushAsync(); contentStream.Dispose(); await stream.FlushAsync(); stream.Dispose(); } var username = (user.Username.Length > 18 ? user.Username.Remove(18) + "..." : user.Username); //Get Local Rank var guildDb = Utility.GetOrCreateGuild(context.Guild.Id, soraContext); var sortedUsers = guildDb.Users.OrderByDescending(x => x.Exp).ToList(); var localRank = sortedUsers.FindIndex(x => x.UserId == user.Id) + 1; //Get local LVL var guildUser = Utility.GetOrCreateGuildUser(user.Id, context.Guild.Id, soraContext); var localLevel = ExpService.CalculateLevel(guildUser.Exp); //get global rank var globalRank = _globalCache.FindIndex(x => x.UserId == user.Id) + 1; // if its not in cache do this: if (globalRank == 0) { globalRank = _globalCache.Count + 1; } //Get global lvl var globalLevel = ExpService.CalculateLevel(userDb.Exp); //calculate needed exp for next lvl int localNeededExp = ExpService.CalculateNeededExp(localLevel + 1); int globalNeededExp = ExpService.CalculateNeededExp(globalLevel + 1); //Get clan var clanName = (string.IsNullOrWhiteSpace(userDb.ClanName) ? "" : userDb.ClanName); //get background image var bgImage = (userDb.HasBg ? $"ProfileData/{user.Id}BGF.png" : $"ProfileCreation/defaultBG.png"); //Draw profile card ProfileImageGeneration.GenerateProfile($"ProfileData/{user.Id}Avatar.png", bgImage, username, clanName, globalRank, globalLevel, (int)userDb.Exp, globalNeededExp, localRank, localLevel, (int)guildUser.Exp, localNeededExp, $"ProfileData/{user.Id}.png"); } catch (Exception e) { Console.WriteLine(e); throw; } if (File.Exists($"ProfileData/{user.Id}.png")) { await context.Channel.SendFileAsync($"ProfileData/{user.Id}.png"); File.Delete($"ProfileData/{user.Id}.png"); File.Delete($"ProfileData/{user.Id}Avatar.png"); } else { await context.Channel.SendMessageAsync("", embed : Utility .ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Failed to create profile card! Maybe try to get a new Background? Or contact the creator here") .WithUrl(Utility.DISCORD_INVITE)); } } }