Exemple #1
0
        public async Task GetBeatmapPack(string ID)
        {
            SocketUserMessage msg = await Context.Channel.SendMessageAsync("Fetching data...", attachID : true);

            IEnumerable <OsuBeatmap> beatmapPack = await OsuNetwork.DownloadBeatmapPack(int.Parse(ID), logger : msg);

            beatmapPack = beatmapPack.OrderBy(x => x.Difficulty.Rating).OrderBy(x => x.GameMode);

            OsuGameModes[] gameModes; //get amount of gamemodes present in the beatmapset
            {
                List <OsuGameModes> collector = new List <OsuGameModes>();
                Tool.ForEach(beatmapPack, x => { if (!collector.Contains(x.GameMode))
                                                 {
                                                     collector.Add(x.GameMode);
                                                 }
                             });

                gameModes = collector.ToArray();
            }

            OsuBeatmap packRef = beatmapPack.First();

            OsuUser creator = await OsuNetwork.DownloadUser(packRef.Creator, OsuGameModes.STD, tolerateNull : true, maxAttempts : 3);

            EmbedBuilder eb = new EmbedBuilder();

            eb.WithAuthor(x =>
            {
                x.Name    = packRef.Title;
                x.Url     = packRef.URL;
                x.IconUrl = "https://cdn.discordapp.com/attachments/420948614966411299/421301562032390164/beatmapPackLogo.png";
            });

            eb.Image = $"{packRef.CoverPictureURL}"; //$"https://b.ppy.sh/thumb/{packRef.BeatmapSetID}l.jpg";

            eb.Description = "";

            if (creator != null)
            {
                eb.Description += $"Created By: [{creator.Username}]({creator.ProfileURL})\n";
            }

            eb.Description += $"📥 **[Download]({packRef.DownloadURL(false)})**";
            eb.Color        = Color.FromArgb(28, 164, 185);

            eb.Thumbnail = packRef.ThumbnailPictureURL;
            eb.Footer    = packRef.GetFooter(creator);

            eb.AddField(x =>
            {
                x.Name  = $"{packRef.Length} ⏱ {packRef.FavouriteCount} ❤️";
                x.Value = $"BPM: **{string.Format("{0:0.##}", packRef.BPM)}**";
            });

            //Display beatmaps
            {
                void addBeatmapField(OsuGameModes gamemode, bool includeName)
                {
                    eb.AddField(x =>
                    {
                        x.Name  = includeName ? $"{CustomEmoji.Osu.Gamemode.GetGamemodeEmoji(gamemode)} {OsuGameModesConverter.GameModeName(gamemode)}" : CustomEmoji.Void.ToString();
                        x.Value = "empty";

                        x.IsInline = true;
                    });
                }

                for (int i = 0; i < gameModes.Length; i++)
                {
                    for (int ii = 0; ii < 2; ii++)
                    {
                        addBeatmapField(gameModes[i], ii == 0);
                    }
                }

                OsuGameModes previousMode = OsuGameModes.None;

                int efbRef = 0;
                int efbPos = -1;

                foreach (OsuBeatmap beatmap in beatmapPack)
                {
                    if (previousMode != beatmap.GameMode)
                    {
                        previousMode = beatmap.GameMode;
                        efbPos++;

                        efbRef = 0;
                    }

                    string beatmapVersion = beatmap.Version;

                    if (beatmapVersion.Length > 14)
                    {
                        beatmapVersion = beatmapVersion.Substring(0, 14) + "...";
                    }

                    string beatmapInfo = $"{CustomEmoji.Osu.Difficulty.GetDifficultyEmoji(beatmap.Difficulty.Rating, beatmap.GameMode)} **[{beatmapVersion}](https://osu.ppy.sh/b/{beatmap.BeatmapID})**\n"; // - *{string.Format("{0:0.##}", beatmap.Difficulty.Rating)}★*

                    if (eb.Fields[efbPos * 2 + efbRef + 1].Value == "empty")
                    {
                        eb.Fields[efbPos * 2 + efbRef + 1].Value = beatmapInfo;
                    }
                    else
                    {
                        eb.Fields[efbPos * 2 + efbRef + 1].Value += beatmapInfo;
                    }

                    efbRef++;
                    if (efbRef == 2)
                    {
                        efbRef = 0;
                    }
                }
            }

            //Insert a zero width space char to make a new line or remove useless \n
            for (int i = 1; i < eb.Fields.Count; i++)
            {
                string efbStr = eb.Fields[i].Value.ToString();

                if (i < eb.Fields.Count - 2)
                {
                    eb.Fields[i].Value = efbStr + '\u200b';
                }
                else
                {
                    if (eb.Fields[i].Value == "empty")
                    {
                        eb.Fields.Remove(eb.Fields[i]);
                    }
                    else
                    {
                        eb.Fields[i].Value = efbStr.Remove(efbStr.Length - 1, 1);
                    }
                }
            }

            await msg.EditAsync($"showing {beatmapPack.Count()} beatmaps", eb.Build());
        }
Exemple #2
0
        //Thanks to peppy no longer needed

        /*public static async Task<bool> ReplayExists(OsuGameModes gameMode, object beatmapID, object userID) => (await NetworkHandler.DownloadJSON(
         *  $"{api}get_replay?" +
         *  $"k={Keys.API_KEYS["Osu"]}&" +
         *  $"m={OsuGameModesConverter.ToOfficialNumeration(gameMode)}&" +
         *  $"b={beatmapID}&" +
         *  $"u={userID}", 2)).Contains("content");*/

        private static async Task <T[]> DownloadObjects <T>(string url, int maxAttempts, SocketUserMessage logger, bool tolerateNull = false, object additional = null)
        {
            string json = await NetworkHandler.DownloadJSON(url, maxAttempts);

            try
            {
                object[] rawTypes = JsonConvert.DeserializeObject <object[]>(json);

                if (rawTypes.Length == 0)
                {
                    if (!tolerateNull)
                    {
                        throw new ArgumentNullException();
                    }
                    else
                    {
                        return(new T[1]); //so it can return null when obtaining a singular object with [0]
                    }
                }

                T[] converted = new T[rawTypes.Length];

                for (int i = 0; i < rawTypes.Length; i++)
                {
                    converted[i] = (T)(additional != null ?
                                       Activator.CreateInstance(typeof(T), rawTypes[i], additional) :
                                       Activator.CreateInstance(typeof(T), rawTypes[i]));
                }

                return(converted);
            }
            catch (ArgumentNullException e)
            {
                if (logger != null)
                {
                    if (typeof(OsuUser) == typeof(T))
                    {
                        await logger.EditAsync(
                            "User was unable to be retrieved from the official osu! api. ❔\n" +
                            "Possible instances: bound user, top 3 players of a beatmap", null);
                    }
                    if (typeof(OsuBeatmap) == typeof(T))
                    {
                        await logger.EditAsync("This beatmap(or beatmap pack) was unable to be retrieved from the official osu! api. ❔", null);
                    }
                    else if (typeof(OsuUserRecent) == typeof(T))
                    {
                        await logger.EditAsync($"No recent plays have been found for game mode {OsuGameModesConverter.GameModeName((OsuGameModes)additional)}. 🔎", null);
                    }
                    else if (typeof(OsuScore) == typeof(T))
                    {
                        await logger.EditAsync($"Scores were unable to be retrieved from the official osu! api. 🔎", null);
                    }

                    throw e;
                }
            }

            return(default(T[]));
        }
Exemple #3
0
        public async Task OsuBindUser([Remainder] string input)
        {
            OsuBoundUserDB bound = await OsuDB.GetBoundUserBy_DiscordID(Context.Message.Author.ID);

            string[] args = input.Split(' ');

            if (args[0].ToLower() == "mainmode")
            {
                if (bound == null)
                {
                    await Context.Channel.SendMessageAsync("You need to bind your osu user first. `OsuBind [username]`");
                }
                else
                {
                    if (args.Length == 1)
                    {
                        await Context.Channel.SendMessageAsync("You need to specify a Game Mode type. It defaults to `standard`.");
                    }
                    else
                    {
                        OsuGameModes gameMode = OsuGameModesConverter.FromOfficialName(input.Substring(args[0].Length + 1), true);
                        bound.MainMode = gameMode;
                        await OsuDB.UpdateBoundOsuUser(bound);

                        await Context.Channel.SendMessageAsync($"Main mode has been successfully changed to **{OsuGameModesConverter.GameModeName(gameMode)}**!");
                    }
                }
            }
            else
            {
                OsuUser osuUser = await OsuNetwork.DownloadUser(input, OsuGameModes.STD);

                if (bound == null)
                {
                    await OsuDB.RegisterBoundOsuUser(osuUser, Context.Message.Author.ID);

                    await Context.Channel.SendMessageAsync($"Binded {input} to `{OsuSQL.table_name}`");
                }
                else
                {
                    await Context.Channel.SendMessageAsync($"{input} is already binded.");
                }
            }
        }