Esempio n. 1
0
        async Task TryReplyAsync(string message)
        {
            var warningResponses = _Config.Get <List <string> >("WarningResponses");

            try
            {
                await ReplyAsync(message);
            }
            catch (HttpException httpException) when(warningResponses.Any(httpException.Message.EndsWith))
            {
                _Logger.LogWarning(httpException.Message);
            }
        }
Esempio n. 2
0
        public string DetermineErrorMessage(short interval, Time time)
        {
            var minJobConfig    = _Config.Get <SubJobConfig>("MinJobConfig");
            var maxJobConfig    = _Config.Get <SubJobConfig>("MaxJobConfig");
            var minTimeSpan     = AsTimeSpan(minJobConfig);
            var maxTimeSpan     = AsTimeSpan(maxJobConfig);
            var desiredTimeSpan = AsTimeSpan(interval, time);

            if (desiredTimeSpan < minTimeSpan || desiredTimeSpan > maxTimeSpan)
            {
                return($"Interval must be between {minJobConfig.Interval} {minJobConfig.Time} and {maxJobConfig.Interval} {maxJobConfig.Time}.");
            }
            switch (time)
            {
            case Time.Hour:
            case Time.Hours:
                var validHours = _Config.Get <List <short> >("ValidHours");
                if (!validHours.Contains(interval))
                {
                    return(InvalidConfigMessage(time, validHours));
                }
                return(null);

            case Time.Minute:
            case Time.Minutes:
                var validMinutes = _Config.Get <List <short> >("ValidMinutes");
                if (!validMinutes.Contains(interval))
                {
                    return(InvalidConfigMessage(time, validMinutes));
                }
                return(null);

            default:
                throw new UnexpectedTimeException(time);
            }
        }
Esempio n. 3
0
        protected override async Task <IUserMessage> ReplyAsync(string message = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)
        {
            var warningResponses = _Config.Get <List <string> >("WarningResponses");

            try
            {
                return(await base.ReplyAsync(message : message, embed : embed));
            }
            catch (HttpException httpException) when(warningResponses.Any(httpException.Message.Contains))
            {
                _Logger.LogWarning(httpException, $"Error replying to channel '{Context.Channel.Id}'. Deleting channel.");
                await _FunctionWrapper.DeleteJobConfigAsync(Context.Channel.Id);

                return(null);
            }
        }
Esempio n. 4
0
        public async Task Run()
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .AddEnvironmentVariables()
                         .Build();

            _ConfigWrapper             = new ConfigurationWrapper(config);
            _ListenToOnlyTheseChannels = _ConfigWrapper.Get <List <ulong> >("ListenToOnlyTheseChannels");
            _FunctionWrapper           = new FunctionWrapper(_ConfigWrapper);
            _DiscordClient             = new DiscordSocketClient();
            var trendHelper = new TrendHelper(_ConfigWrapper);

            trendHelper.PrefixUpdated += TrendHelper_PrefixUpdated;
            _Services = new ServiceCollection()
                        .AddSingleton(_DiscordClient)
                        .AddSingleton <ITrendHelper>(trendHelper)
                        .AddSingleton(_FunctionWrapper)
                        .AddSingleton(_ConfigWrapper)
                        .AddLogging(s => s.AddConsole())
                        .BuildServiceProvider();
            _Logger             = _Services.GetService <ILogger <Bot> >();
            _DiscordClient.Log += Log;
            await LogInAsync();
            await StartAsync();

            _Commands = new CommandService();
            await _Commands.AddModulesAsync(Assembly.GetEntryAssembly(), _Services);

            DetermineModuleNames();
            _DiscordClient.MessageReceived += MessageReceived;
            _DiscordClient.JoinedGuild     += JoinedGuild;
            _DiscordClient.LeftGuild       += LeftGuild;
            await _DiscordClient.SetGameAsync(_ConfigWrapper["PlayingGame"]);

            _PrefixDictionary = await _FunctionWrapper.GetPrefixDictionaryAsync();
        }
Esempio n. 5
0
        public bool IsValidTimeZoneFraction(decimal utcHourOffset)
        {
            var validTimeZoneFractions = _ConfigWrapper.Get <List <decimal> >("ValidTimeZoneFractions");

            return(utcHourOffset % 1 == 0 || validTimeZoneFractions.Any(s => utcHourOffset % s == 0));
        }