public async Task <bool> Init(string[] args)
        {
            Settings = ServerSettings.Load();
            if (!await Settings.Init(args))
            {
                return(false);
            }
            Logger.Instance = new UnixLogger(webClient, Path.Combine(Settings.ServerDir, "log.csv"), Settings.WebHook);
            if (Settings.Test)
            {
                await Logger.WriteLine("Started in test mode");
            }
            await Logger.WriteLine(Settings.GitCommit);

            Api = new OsuApi(webClient, Settings.OsuId, Settings.OsuSecret, Settings.OsuApiKey);
            var apiToken = Api.RefreshToken();

            Directory.CreateDirectory(Path.Combine(Settings.ServerDir, "beatmaps"));
            Directory.CreateDirectory(Path.Combine(Settings.ServerDir, "replays"));
            BeatmapDb    = new ServerBeatmapDb(Api, Settings.ServerDir, Settings.Reload);
            ReplayDb     = new ServerReplayDb(Api, Settings.ServerDir);
            CachedMisses = new MemoryCache <ulong, Response>(128);
            CachedMisses.SetPolicy(typeof(LfuEvictionPolicy <,>));

            Discord = new DiscordShardedClient(new DiscordConfiguration
            {
                Token     = Settings.DiscordToken,
                TokenType = TokenType.Bot,
                Intents   = DiscordIntents.Guilds |
                            DiscordIntents.GuildMessages |
                            DiscordIntents.GuildMessageReactions |
                            DiscordIntents.DirectMessages |
                            DiscordIntents.DirectMessageReactions
            });
            var slash = await Discord.UseSlashCommandsAsync(new SlashCommandsConfiguration
            {
                Services = new ServiceCollection().AddSingleton(this).BuildServiceProvider(),
            });

            if (Settings.Test)
            {
                slash.RegisterCommands <Commands>(Settings.TestGuild);
            }
            else
            {
                slash.RegisterCommands <Commands>();
            }

            numberEmojis = new DiscordEmoji[10];

            for (int i = 0; i < 10; i++)
            {
                numberEmojis[i] = DiscordEmoji.FromUnicode(i + "\ufe0f\u20e3");
            }

            status = new Stopwatch();
            status.Start();

            Discord.MessageCreated += async(d, e) =>
            {
                await HandleMessage(d, e);
                await CheckStatus();
            };

            Discord.MessageReactionAdded += HandleReaction;

            Discord.ComponentInteractionCreated += HandleInteraction;

            Discord.ClientErrored += async(d, e) =>
            {
                await Logger.WriteLine(e.EventName);

                await Logger.LogException(e.Exception);
            };

            Discord.SocketErrored += async(d, e) =>
            {
                await Logger.LogException(e.Exception);
            };

            foreach (var s in slash)
            {
                s.Value.SlashCommandErrored += async(d, e) =>
                {
                    await Logger.WriteLine(e.Context.CommandName);

                    await Logger.LogException(e.Exception);
                };
            }
            Logger.Instance.UpdateLogs += () => Logger.LogAbsolute(Logging.ServersJoined, Discord.ShardClients.Select(s => s.Value.Guilds?.Count ?? 0).Sum());

            await apiToken;

            return(true);
        }
Exemplo n.º 2
0
        public async Task <bool> Init(string[] args)
        {
            Settings = ServerSettings.Load();
            if (!await Settings.Init(args))
            {
                return(false);
            }
            Logger.Instance = new Logger(Path.Combine(Settings.ServerDir, "log.csv"), Settings.WebHook);
            Api             = new OsuApi(Settings.OsuId, Settings.OsuSecret, Settings.OsuApiKey);
            var apiToken = Api.RefreshToken();

            Directory.CreateDirectory(Path.Combine(Settings.ServerDir, "beatmaps"));
            Directory.CreateDirectory(Path.Combine(Settings.ServerDir, "replays"));
            BeatmapDb    = new ServerBeatmapDb(Api, Settings.ServerDir, Settings.Reload);
            ReplayDb     = new ServerReplayDb(Api, Settings.ServerDir);
            CachedMisses = new MemoryCache <DiscordMessage, SavedMiss>(128);
            CachedMisses.SetPolicy(typeof(LfuEvictionPolicy <,>));

            Discord = new DiscordClient(new DiscordConfiguration
            {
                Token     = Settings.DiscordToken,
                TokenType = TokenType.Bot
            });

            numberEmojis = new DiscordEmoji[10];

            for (int i = 0; i < 10; i++)
            {
                numberEmojis[i] = DiscordEmoji.FromName(Discord, $":{numbers[i]}:");
            }

            status = new Stopwatch();
            status.Start();

            Discord.MessageCreated += async(d, e) =>
            {
                await HandleMessage(d, e);
                await CheckStatus();
            };

            Discord.MessageReactionAdded += HandleReaction;

            Discord.ClientErrored += async(d, e) =>
            {
                Logger.Log(Logging.ErrorUnhandled);
                await Logger.WriteLine(e.EventName);

                await Logger.WriteLine(e.Exception, Logger.LogLevel.ALERT);
            };

            Discord.SocketErrored += async(d, e) =>
            {
                Logger.Log(Logging.ErrorUnhandled);
                await Logger.WriteLine(e.Exception, Logger.LogLevel.ALERT);

                await d.ConnectAsync();
            };

            Logger.Instance.UpdateLogs += () => Logger.LogAbsolute(Logging.ServersJoined, Discord?.Guilds?.Count ?? 0);
            await apiToken;

            return(true);
        }
        public async Task <string> Load(GuildSettings guildSettings, OsuApi api, ServerReplayDb replays, ServerBeatmapDb beatmaps)
        {
            this.guildSettings = guildSettings;
            JToken score = null;

            if (Username != null && UserId == null)
            {
                UserId = await api.GetUserIdv1(Username);
            }

            if (BeatmapId != null)
            {
                _beatmap = await beatmaps.GetBeatmapFromId(BeatmapId);
            }

            if (ReplayFile != null)
            {
                _replay = new Replay(ReplayFile);
            }
            else if (ScoreId != null)
            {
                if (Mods == null || Beatmap == null)
                {
                    score = await api.GetScorev2(ScoreId);
                }
                else
                {
                    _replay = await replays.GetReplayFromOnlineId(ScoreId, Mods, Beatmap);
                }
            }

            if (_replay == null && PlayIndex.HasValue)
            {
                if (PlayIndex.Value < 0)
                {
                    return("Index value must be greater than 0");
                }

                if (UserId != null && UserScores != null)
                {
                    score = await api.GetUserScoresv2(UserId, UserScores, PlayIndex.Value, FailedScores);
                }
                else if (BeatmapId != null)
                {
                    score = await api.GetBeatmapScoresv2(BeatmapId, PlayIndex.Value);
                }
            }

            if (score != null)
            {
                if (!(bool)score["replay"])
                {
                    return("Replay not saved online");
                }
                if ((bool)score["perfect"])
                {
                    return("No misses");
                }

                if (_beatmap == null)
                {
                    _beatmap = await beatmaps.GetBeatmapFromId((string)score["beatmap"]["id"]);
                }
                _replay = await replays.GetReplayFromScore(score, _beatmap);
            }

            if (_beatmap == null && _replay != null)
            {
                _beatmap = await beatmaps.GetBeatmap(_replay.MapHash);
            }

            if (_beatmap != null && _replay != null && _beatmap.BeatmapHash != _replay.MapHash)
            {
                _beatmap = await beatmaps.GetBeatmapFromId(_beatmap.BeatmapID.Value.ToString(), forceRedl : true);
            }

            if (_replay != null && !_replay.fullLoaded)
            {
                return("Replay does not contain any cursor data - can't analyze");
            }


            if (_replay != null && _beatmap != null)
            {
                if (_beatmap.Mode != GameMode.osu)
                {
                    return(null);
                }

                _analyzer = new ReplayAnalyzer(_beatmap, _replay);
                Loaded    = true;
                return(null);
            }
            return($"Couldn't find {(_replay == null? "replay" : "beatmap")}");
        }
Exemplo n.º 4
0
        public async Task <string> Load(OsuApi api, ServerReplayDb replays, ServerBeatmapDb beatmaps)
        {
            JToken score = null;

            if (Username != null && UserId == null)
            {
                UserId = await api.GetUserIdv1(Username);
            }

            if (BeatmapId != null)
            {
                _beatmap = await beatmaps.GetBeatmapFromId(BeatmapId);
            }

            if (ReplayFile != null)
            {
                _replay = new Replay(ReplayFile);
            }
            else if (ScoreId != null && Mods != null)
            {
                _replay = await replays.GetReplayFromOnlineId(ScoreId, Mods, _beatmap);
            }

            if (_replay == null && PlayIndex.HasValue)
            {
                if (UserId != null && UserScores != null)
                {
                    score = await api.GetUserScoresv2(UserId, UserScores, PlayIndex.Value, FailedScores);
                }
                else if (BeatmapId != null)
                {
                    score = await api.GetBeatmapScoresv2(BeatmapId, PlayIndex.Value);
                }

                if (score != null && _beatmap == null)
                {
                    _beatmap = await beatmaps.GetBeatmapFromId((string)score["beatmap"]["id"]);
                }
            }

            if (score != null && _beatmap != null)
            {
                _replay = await replays.GetReplayFromScore(score, _beatmap);
            }

            if (_beatmap == null && _replay != null)
            {
                _beatmap = await beatmaps.GetBeatmap(_replay.MapHash);
            }

            if (_replay != null && !_replay.fullLoaded)
            {
                return("Replay does not contain any cursor data - can't analyze");
            }

            if (_replay != null && _beatmap != null)
            {
                _analyzer = new ReplayAnalyzer(_beatmap, _replay);
                Loaded    = true;
                return(null);
            }
            return($"Couldn't find {(_replay == null? "replay" : "beatmap")}");
        }