示例#1
0
        public async Task AddSongAsync([Remainder] string paramString = null)
        {
            if (IsAdmin())
            {
                var eventId = paramString.ParseArgs("赛事");
                var songId  = paramString.ParseArgs("歌曲");

                if (string.IsNullOrEmpty(eventId) || string.IsNullOrEmpty(songId))
                {
                    await ReplyAsync(embed : ("用法: `添加歌曲 -赛事 \"[赛事ID]\" -歌曲 [链接/key]`\n" +
                                              "赛事ID可以通过`赛事列表`命令查找\n" +
                                              "可选参数: `-难度 [Easy/Normal/Hard/Expert/ExpertPlus]`, `-谱型 [例如: onesaber]`, `-[修改项]` (例如: 不死模式为 `-nofail`)").ErrorEmbed());

                    return;
                }

                //Parse the difficulty input, either as an int or a string
                BeatmapDifficulty difficulty = BeatmapDifficulty.ExpertPlus;

                string difficultyArg = paramString.ParseArgs("难度");
                if (difficultyArg != null)
                {
                    //If the enum conversion doesn't succeed, try it as an int
                    if (!Enum.TryParse(difficultyArg, true, out difficulty))
                    {
                        await ReplyAsync(embed : "请检查难度参数".ErrorEmbed());

                        return;
                    }
                }

                string characteristic = paramString.ParseArgs("谱型");
                characteristic = characteristic ?? "Standard";

                GameOptions   gameOptions   = GameOptions.None;
                PlayerOptions playerOptions = PlayerOptions.None;

                //Load up the GameOptions and PlayerOptions
                foreach (GameOptions o in Enum.GetValues(typeof(GameOptions)))
                {
                    if (paramString.ParseArgs(o.ToString()) == "true")
                    {
                        gameOptions = (gameOptions | o);
                    }
                }

                foreach (PlayerOptions o in Enum.GetValues(typeof(PlayerOptions)))
                {
                    if (paramString.ParseArgs(o.ToString()) == "true")
                    {
                        playerOptions = (playerOptions | o);
                    }
                }

                //Sanitize input
                if (songId.StartsWith("https://beatsaver.com/") || songId.StartsWith("https://bsaber.com/"))
                {
                    //Strip off the trailing slash if there is one
                    if (songId.EndsWith("/"))
                    {
                        songId = songId.Substring(0, songId.Length - 1);
                    }

                    //Strip off the beginning of the url to leave the id
                    songId = songId.Substring(songId.LastIndexOf("/") + 1);
                }

                if (songId.Contains("&"))
                {
                    songId = songId.Substring(0, songId.IndexOf("&"));
                }

                var server = ServerService.GetServer();
                if (server == null)
                {
                    await ReplyAsync(embed : "服务器不在线,所以不能添加歌曲".ErrorEmbed());
                }
                else
                {
                    //Get the hash for the song
                    var hash       = BeatSaverDownloader.GetHashFromID(songId);
                    var knownPairs = await HostScraper.ScrapeHosts(server.State.KnownHosts, $"{server.CoreServer.Address}:{server.CoreServer.Port}", 0);

                    var targetPair  = knownPairs.FirstOrDefault(x => x.Value.Events.Any(y => y.EventId.ToString() == eventId));
                    var targetEvent = targetPair.Value.Events.FirstOrDefault(x => x.EventId.ToString() == eventId);
                    var songPool    = targetEvent.QualifierMaps.ToList();

                    if (OstHelper.IsOst(hash))
                    {
                        if (!SongExists(songPool, hash, characteristic, (int)difficulty, (int)gameOptions, (int)playerOptions))
                        {
                            GameplayParameters parameters = new GameplayParameters
                            {
                                Beatmap = new Beatmap
                                {
                                    Name           = OstHelper.GetOstSongNameFromLevelId(hash),
                                    LevelId        = hash,
                                    Characteristic = new Characteristic
                                    {
                                        SerializedName = characteristic
                                    },
                                    Difficulty = difficulty
                                },
                                GameplayModifiers = new GameplayModifiers
                                {
                                    Options = gameOptions
                                },
                                PlayerSettings = new PlayerSpecificSettings
                                {
                                    Options = playerOptions
                                }
                            };

                            songPool.Add(parameters);
                            targetEvent.QualifierMaps = songPool.ToArray();

                            var response = await server.SendUpdateQualifierEvent(targetPair.Key, targetEvent);

                            if (response.Type == Response.ResponseType.Success)
                            {
                                await ReplyAsync(embed : ($"已添加: {parameters.Beatmap.Name} ({difficulty}) ({characteristic})" +
                                                          $"{(gameOptions != GameOptions.None ? $" 附加游戏参数: ({gameOptions})" : "")}" +
                                                          $"{(playerOptions != PlayerOptions.None ? $" 附加玩家参数: ({playerOptions})" : "!")}").SuccessEmbed());
                            }
                            else if (response.Type == Response.ResponseType.Fail)
                            {
                                await ReplyAsync(embed : response.Message.ErrorEmbed());
                            }
                        }
                        else
                        {
                            await ReplyAsync(embed : "歌曲已存在于数据库".ErrorEmbed());
                        }
                    }
                    else
                    {
                        var songInfo = await BeatSaverDownloader.GetSongInfo(songId);

                        string songName = songInfo.Name;

                        if (!songInfo.HasDifficulty(characteristic, difficulty))
                        {
                            BeatmapDifficulty nextBestDifficulty = songInfo.GetClosestDifficultyPreferLower(characteristic, difficulty);

                            if (SongExists(songPool, hash, characteristic, (int)nextBestDifficulty, (int)gameOptions, (int)playerOptions))
                            {
                                await ReplyAsync(embed : $"{songName} 不存在 {difficulty} 难度, 而且 {nextBestDifficulty} 已存在于赛事".ErrorEmbed());
                            }
                            else
                            {
                                GameplayParameters parameters = new GameplayParameters
                                {
                                    Beatmap = new Beatmap
                                    {
                                        Name           = songName,
                                        LevelId        = $"custom_level_{hash.ToUpper()}",
                                        Characteristic = new Characteristic
                                        {
                                            SerializedName = characteristic
                                        },
                                        Difficulty = nextBestDifficulty
                                    },
                                    GameplayModifiers = new GameplayModifiers
                                    {
                                        Options = gameOptions
                                    },
                                    PlayerSettings = new PlayerSpecificSettings
                                    {
                                        Options = playerOptions
                                    }
                                };

                                songPool.Add(parameters);
                                targetEvent.QualifierMaps = songPool.ToArray();

                                var response = await server.SendUpdateQualifierEvent(targetPair.Key, targetEvent);

                                if (response.Type == Response.ResponseType.Success)
                                {
                                    await ReplyAsync(embed : ($"{songName} 不存在 {difficulty} 难度, 使用 {nextBestDifficulty} 难度代替。\n" +
                                                              $"已添加至歌曲列表" +
                                                              $"{(gameOptions != GameOptions.None ? $" 附加游戏参数: ({gameOptions})" : "")}" +
                                                              $"{(playerOptions != PlayerOptions.None ? $" 附加玩家参数: ({playerOptions})" : "!")}").SuccessEmbed());
                                }
                                else if (response.Type == Response.ResponseType.Fail)
                                {
                                    await ReplyAsync(embed : response.Message.ErrorEmbed());
                                }
                            }
                        }
                        else
                        {
                            GameplayParameters parameters = new GameplayParameters
                            {
                                Beatmap = new Beatmap
                                {
                                    Name           = songName,
                                    LevelId        = $"custom_level_{hash.ToUpper()}",
                                    Characteristic = new Characteristic
                                    {
                                        SerializedName = characteristic
                                    },
                                    Difficulty = difficulty
                                },
                                GameplayModifiers = new GameplayModifiers
                                {
                                    Options = gameOptions
                                },
                                PlayerSettings = new PlayerSpecificSettings
                                {
                                    Options = playerOptions
                                }
                            };

                            songPool.Add(parameters);
                            targetEvent.QualifierMaps = songPool.ToArray();

                            var response = await server.SendUpdateQualifierEvent(targetPair.Key, targetEvent);

                            if (response.Type == Response.ResponseType.Success)
                            {
                                await ReplyAsync(embed : ($"{songName} ({difficulty}) ({characteristic}) 已下载并添加至歌曲列表" +
                                                          $"{(gameOptions != GameOptions.None ? $" 附加游戏参数: ({gameOptions})" : "")}" +
                                                          $"{(playerOptions != PlayerOptions.None ? $" 附加玩家参数: ({playerOptions})" : "!")}").SuccessEmbed());
                            }
                            else if (response.Type == Response.ResponseType.Fail)
                            {
                                await ReplyAsync(embed : response.Message.ErrorEmbed());
                            }
                        }
                    }
                }
            }
            else
            {
                await ReplyAsync(embed : "你没有足够的权限使用该命令".ErrorEmbed());
            }
        }
示例#2
0
        public async Task AddSongAsync([Remainder] string paramString = null)
        {
            if (IsAdmin())
            {
                var eventId = paramString.ParseArgs("eventId");
                var songId  = paramString.ParseArgs("song");

                if (string.IsNullOrEmpty(eventId) || string.IsNullOrEmpty(songId))
                {
                    await ReplyAsync(embed : ("Usage: `addSong -eventId \"[event id]\" -song [song link]`\n" +
                                              "To find event ids, please run `listEvents`\n" +
                                              "Optional parameters: `-difficulty [difficulty]`, `-characteristic [characteristic]` (example: `-characteristic onesaber`), `-[modifier]` (example: `-nofail`)").ErrorEmbed());

                    return;
                }

                //Parse the difficulty input, either as an int or a string
                BeatmapDifficulty difficulty = BeatmapDifficulty.ExpertPlus;

                string difficultyArg = paramString.ParseArgs("difficulty");
                if (difficultyArg != null)
                {
                    //If the enum conversion doesn't succeed, try it as an int
                    if (!Enum.TryParse(difficultyArg, true, out difficulty))
                    {
                        await ReplyAsync(embed : "Could not parse difficulty parameter".ErrorEmbed());

                        return;
                    }
                }

                string characteristic = paramString.ParseArgs("characteristic");
                characteristic = characteristic ?? "Standard";

                GameOptions   gameOptions   = GameOptions.None;
                PlayerOptions playerOptions = PlayerOptions.None;

                //Load up the GameOptions and PlayerOptions
                foreach (GameOptions o in Enum.GetValues(typeof(GameOptions)))
                {
                    if (paramString.ParseArgs(o.ToString()) == "true")
                    {
                        gameOptions = (gameOptions | o);
                    }
                }

                foreach (PlayerOptions o in Enum.GetValues(typeof(PlayerOptions)))
                {
                    if (paramString.ParseArgs(o.ToString()) == "true")
                    {
                        playerOptions = (playerOptions | o);
                    }
                }

                //Sanitize input
                if (songId.StartsWith("https://beatsaver.com/") || songId.StartsWith("https://bsaber.com/"))
                {
                    //Strip off the trailing slash if there is one
                    if (songId.EndsWith("/"))
                    {
                        songId = songId.Substring(0, songId.Length - 1);
                    }

                    //Strip off the beginning of the url to leave the id
                    songId = songId.Substring(songId.LastIndexOf("/") + 1);
                }

                if (songId.Contains("&"))
                {
                    songId = songId.Substring(0, songId.IndexOf("&"));
                }

                var server = ServerService.GetServer();
                if (server == null)
                {
                    await ReplyAsync(embed : "The Server is not running, so we can't can't add songs to it".ErrorEmbed());
                }
                else
                {
                    //Get the hash for the song
                    var hash       = BeatSaverDownloader.GetHashFromID(songId);
                    var knownPairs = await HostScraper.ScrapeHosts(server.State.KnownHosts, $"{server.CoreServer.Address}:{server.CoreServer.Port}", 0);

                    var targetPair  = knownPairs.FirstOrDefault(x => x.Value.Events.Any(y => y.EventId.ToString() == eventId));
                    var targetEvent = targetPair.Value.Events.FirstOrDefault(x => x.EventId.ToString() == eventId);
                    var songPool    = targetEvent.QualifierMaps.ToList();

                    if (OstHelper.IsOst(hash))
                    {
                        if (!SongExists(songPool, hash, characteristic, (int)difficulty, (int)gameOptions, (int)playerOptions))
                        {
                            GameplayParameters parameters = new GameplayParameters
                            {
                                Beatmap = new Beatmap
                                {
                                    Name           = OstHelper.GetOstSongNameFromLevelId(hash),
                                    LevelId        = hash,
                                    Characteristic = new Characteristic
                                    {
                                        SerializedName = characteristic
                                    },
                                    Difficulty = difficulty
                                },
                                GameplayModifiers = new GameplayModifiers
                                {
                                    Options = gameOptions
                                },
                                PlayerSettings = new PlayerSpecificSettings
                                {
                                    Options = playerOptions
                                }
                            };

                            songPool.Add(parameters);
                            targetEvent.QualifierMaps = songPool.ToArray();

                            var response = await server.SendUpdateQualifierEvent(targetPair.Key, targetEvent);

                            if (response.Type == Response.ResponseType.Success)
                            {
                                await ReplyAsync(embed : ($"Added: {parameters.Beatmap.Name} ({difficulty}) ({characteristic})" +
                                                          $"{(gameOptions != GameOptions.None ? $" with game options: ({gameOptions})" : "")}" +
                                                          $"{(playerOptions != PlayerOptions.None ? $" with player options: ({playerOptions})" : "!")}").SuccessEmbed());
                            }
                            else if (response.Type == Response.ResponseType.Fail)
                            {
                                await ReplyAsync(embed : response.Message.ErrorEmbed());
                            }
                        }
                        else
                        {
                            await ReplyAsync(embed : "Song is already active in the database".ErrorEmbed());
                        }
                    }
                    else
                    {
                        var songInfo = await BeatSaverDownloader.GetSongInfo(songId);

                        string songName = songInfo.Name;

                        if (!songInfo.HasDifficulty(characteristic, difficulty))
                        {
                            BeatmapDifficulty nextBestDifficulty = songInfo.GetClosestDifficultyPreferLower(characteristic, difficulty);

                            if (SongExists(songPool, hash, characteristic, (int)nextBestDifficulty, (int)gameOptions, (int)playerOptions))
                            {
                                await ReplyAsync(embed : $"{songName} doesn't have {difficulty}, and {nextBestDifficulty} is already in the event".ErrorEmbed());
                            }
                            else
                            {
                                GameplayParameters parameters = new GameplayParameters
                                {
                                    Beatmap = new Beatmap
                                    {
                                        Name           = songName,
                                        LevelId        = $"custom_level_{hash.ToUpper()}",
                                        Characteristic = new Characteristic
                                        {
                                            SerializedName = characteristic
                                        },
                                        Difficulty = nextBestDifficulty
                                    },
                                    GameplayModifiers = new GameplayModifiers
                                    {
                                        Options = gameOptions
                                    },
                                    PlayerSettings = new PlayerSpecificSettings
                                    {
                                        Options = playerOptions
                                    }
                                };

                                songPool.Add(parameters);
                                targetEvent.QualifierMaps = songPool.ToArray();

                                var response = await server.SendUpdateQualifierEvent(targetPair.Key, targetEvent);

                                if (response.Type == Response.ResponseType.Success)
                                {
                                    await ReplyAsync(embed : ($"{songName} doesn't have {difficulty}, using {nextBestDifficulty} instead.\n" +
                                                              $"Added to the song list" +
                                                              $"{(gameOptions != GameOptions.None ? $" with game options: ({gameOptions})" : "")}" +
                                                              $"{(playerOptions != PlayerOptions.None ? $" with player options: ({playerOptions})" : "!")}").SuccessEmbed());
                                }
                                else if (response.Type == Response.ResponseType.Fail)
                                {
                                    await ReplyAsync(embed : response.Message.ErrorEmbed());
                                }
                            }
                        }
                        else
                        {
                            GameplayParameters parameters = new GameplayParameters
                            {
                                Beatmap = new Beatmap
                                {
                                    Name           = songName,
                                    LevelId        = $"custom_level_{hash.ToUpper()}",
                                    Characteristic = new Characteristic
                                    {
                                        SerializedName = characteristic
                                    },
                                    Difficulty = difficulty
                                },
                                GameplayModifiers = new GameplayModifiers
                                {
                                    Options = gameOptions
                                },
                                PlayerSettings = new PlayerSpecificSettings
                                {
                                    Options = playerOptions
                                }
                            };

                            songPool.Add(parameters);
                            targetEvent.QualifierMaps = songPool.ToArray();

                            var response = await server.SendUpdateQualifierEvent(targetPair.Key, targetEvent);

                            if (response.Type == Response.ResponseType.Success)
                            {
                                await ReplyAsync(embed : ($"{songName} ({difficulty}) ({characteristic}) downloaded and added to song list" +
                                                          $"{(gameOptions != GameOptions.None ? $" with game options: ({gameOptions})" : "")}" +
                                                          $"{(playerOptions != PlayerOptions.None ? $" with player options: ({playerOptions})" : "!")}").SuccessEmbed());
                            }
                            else if (response.Type == Response.ResponseType.Fail)
                            {
                                await ReplyAsync(embed : response.Message.ErrorEmbed());
                            }
                        }
                    }
                }
            }
            else
            {
                await ReplyAsync(embed : "You do not have sufficient permissions to use this command".ErrorEmbed());
            }
        }