Example #1
0
        public static void NoScrobblesFoundErrorResponse(this EmbedBuilder embed, LastResponseStatus apiResponse, ICommandContext context, Logger.Logger logger)
        {
            embed.WithTitle("Error while attempting get Last.FM information");
            switch (apiResponse)
            {
            case LastResponseStatus.Failure:
                embed.WithDescription("Can't retrieve scrobbles because Last.FM is having issues. Please try again later. \n" +
                                      "Please note that .fmbot isn't affiliated with Last.FM.");
                break;

            case LastResponseStatus.MissingParameters:
                embed.WithDescription("You or the user you're searching for has no scrobbles/artists on their profile, or Last.FM is having issues. Please try again later. \n \n" +
                                      "Recently changed your Last.FM username? Please change it here too using `.fmset`. \n" +
                                      "For more info on your settings, use `.fmset help`.");
                break;

            default:
                embed.WithDescription(
                    "You or the user you're searching for has no scrobbles/artists on their profile, or Last.FM is having issues. Please try again later.");
                break;
            }

            embed.WithThumbnailUrl("https://www.last.fm/static/images/marvin.e51495403de9.png");
            embed.WithColor(Constants.WarningColorOrange);
            logger.LogError($"No scrobbles found for user, error code {apiResponse}", context.Message.Content, context.User.Username, context.Guild?.Name, context.Guild?.Id);
        }
Example #2
0
        private async Task ExecuteCommand(SocketUserMessage msg, ShardedCommandContext context, int argPos,
                                          string customPrefix = null)
        {
            if (StackCooldownTarget.Contains(msg.Author))
            {
                //If they have used this command before, take the time the user last did something, add 1100ms, and see if it's greater than this very moment.
                if (StackCooldownTimer[StackCooldownTarget.IndexOf(msg.Author)].AddMilliseconds(1100) >=
                    DateTimeOffset.Now)
                {
                    return;
                }

                StackCooldownTimer[StackCooldownTarget.IndexOf(msg.Author)] = DateTimeOffset.Now;
            }
            else
            {
                //If they've never used this command before, add their username and when they just used this command.
                StackCooldownTarget.Add(msg.Author);
                StackCooldownTimer.Add(DateTimeOffset.Now);
            }

            var searchResult = this._commands.Search(context, argPos);

            // If no command were found, return.
            if ((searchResult.Commands == null || searchResult.Commands.Count == 0) && customPrefix != null && !msg.Content.StartsWith(customPrefix))
            {
                return;
            }

            if ((searchResult.Commands == null || searchResult.Commands.Count == 0) && msg.Content.StartsWith(ConfigData.Data.CommandPrefix))
            {
                var commandPrefixResult = await this._commands.ExecuteAsync(context, msg.Content.Remove(0, 1), this._provider);

                if (commandPrefixResult.IsSuccess)
                {
                    Statistics.CommandsExecuted.Inc();
                }
                else
                {
                    var logger = new Logger.Logger();
                    logger.LogError(commandPrefixResult.ToString(), context.Message.Content);
                }

                return;
            }

            var result = await this._commands.ExecuteAsync(context, argPos, this._provider);

            if (result.IsSuccess)
            {
                Statistics.CommandsExecuted.Inc();
            }
            else
            {
                var logger = new Logger.Logger();
                logger.LogError(result.ToString(), context.Message.Content);
            }
        }
Example #3
0
        public static void UsernameNotSetErrorResponse(this EmbedBuilder embed, ICommandContext context, Logger.Logger logger)
        {
            embed.WithTitle("Error while attempting get Last.FM information");
            embed.WithDescription("Your Last.FM username has not been set. \n" +
                                  "Please use the `.fmset` command to connect your Last.FM account to .fmbot. \n" +
                                  $"Example: `{ConfigData.Data.CommandPrefix}fmset lastfmusername`\n \n" +
                                  "For more info, use `.fmset help`.");

            embed.WithColor(Constants.WarningColorOrange);
            logger.LogError("Last.FM username not set", context.Message.Content, context.User.Username, context.Guild?.Name, context.Guild?.Id);
        }
Example #4
0
 /// <summary>
 /// Handles the <see cref="DiscordShardedClient.Log"/> event.
 /// </summary>
 /// <param name="logMessage">The log message.</param>
 private Task LogEvent(LogMessage logMessage)
 {
     Task.Run(() =>
     {
         // If log message is a Serializer Error, Log the message to the SerializerError folder.
         if (logMessage.Message.Contains("Serializer Error"))
         {
             _logger.LogError($"Source: {logMessage.Source} Exception: {logMessage.Exception} Message: {logMessage.Message}");
         }
         Log(logMessage.Message);
     });
     return(Task.CompletedTask);
 }
Example #5
0
        private void ConfigureServices(IServiceCollection services)
        {
            var discordClient = new DiscordShardedClient(new DiscordSocketConfig
            {
                LogLevel         = LogSeverity.Verbose,
                MessageCacheSize = 0
            });

            var logger = new Logger.Logger();

            services
            .AddSingleton(discordClient)
            .AddSingleton(new CommandService(new CommandServiceConfig
            {
                LogLevel       = LogSeverity.Verbose,
                DefaultRunMode = RunMode.Async,
            }))
            .AddSingleton <CommandHandler>()
            .AddSingleton <StartupService>()
            .AddSingleton <TimerService>()
            .AddSingleton <IUserIndexQueue, UserIndexQueue>()
            .AddSingleton <IPrefixService, PrefixService>()
            .AddSingleton <IndexService>()
            .AddSingleton(logger)
            .AddSingleton <Random>()          // Add random to the collection
            .AddSingleton(this.Configuration) // Add the configuration to the collection
            .AddHttpClient();

            services
            .AddTransient <ILastfmApi, LastfmApi>();

            using (var context = new FMBotDbContext())
            {
                try
                {
                    logger.Log("Ensuring database is up to date");
                    context.Database.Migrate();
                }
                catch (Exception e)
                {
                    logger.LogError("Migrations", $"Something went wrong while creating/updating the database! \n{e.Message}");
                    throw;
                }
            }
        }
Example #6
0
        public static void ErrorResponse(this EmbedBuilder embed, ResponseStatus apiResponse, string message, ICommandContext context, Logger.Logger logger)
        {
            embed.WithTitle("Error while attempting get Last.FM information");
            switch (apiResponse)
            {
            case ResponseStatus.Failure:
                embed.WithDescription("Can't retrieve data because Last.FM is having issues. Please try again later. \n" +
                                      "Please note that .fmbot isn't affiliated with Last.FM.");
                break;

            default:
                embed.WithDescription(
                    message);
                break;
            }

            embed.WithColor(Constants.WarningColorOrange);
            logger.LogError($"Last.fm returned error: {message}, error code {apiResponse}", context.Message.Content, context.User.Username, context.Guild?.Name, context.Guild?.Id);
        }
Example #7
0
        public static void NoScrobblesFoundErrorResponse(this EmbedBuilder embed, ResponseStatus apiResponse, ICommandContext context, Logger.Logger logger)
        {
            embed.WithTitle("Error while attempting get Last.FM information");
            switch (apiResponse)
            {
            case ResponseStatus.Failure:
                embed.WithDescription("Can't retrieve scrobbles because Last.FM is having issues. Please try again later. \n" +
                                      "Please note that .fmbot isn't affiliated with Last.FM.");
                break;

            default:
                embed.WithDescription(
                    "You have no scrobbles/artists on your profile, or Last.FM is having issues. Please try again later.");
                break;
            }

            embed.WithColor(Constants.WarningColorOrange);
            logger.LogError($"No scrobbles found for user, error code {apiResponse}", context.Message.Content, context.User.Username, context.Guild?.Name, context.Guild?.Id);
        }
Example #8
0
        private async Task ExecuteCommand(SocketUserMessage msg, ShardedCommandContext context, int argPos, string customPrefix = null)
        {
            if (StackCooldownTarget.Contains(msg.Author))
            {
                //If they have used this command before, take the time the user last did something, add 1100ms, and see if it's greater than this very moment.
                if (StackCooldownTimer[StackCooldownTarget.IndexOf(msg.Author)].AddMilliseconds(1100) >=
                    DateTimeOffset.Now)
                {
                    return;
                }

                StackCooldownTimer[StackCooldownTarget.IndexOf(msg.Author)] = DateTimeOffset.Now;
            }
            else
            {
                //If they've never used this command before, add their username and when they just used this command.
                StackCooldownTarget.Add(msg.Author);
                StackCooldownTimer.Add(DateTimeOffset.Now);
            }

            var searchResult = this._commands.Search(context, argPos);

            // If custom prefix is enabled, no commands found and message does not start with custom prefix, return
            if ((searchResult.Commands == null || searchResult.Commands.Count == 0) && customPrefix != null && !msg.Content.StartsWith(customPrefix))
            {
                return;
            }

            var disabledCommands = this._disabledCommandService.GetDisabledCommands(context.Guild?.Id);

            if (searchResult.Commands != null &&
                disabledCommands != null &&
                disabledCommands.Any(searchResult.Commands.First().Command.Name.Contains))
            {
                await context.Channel.SendMessageAsync("The command you're trying to execute has been disabled in this server.");

                return;
            }

            if ((searchResult.Commands == null || searchResult.Commands.Count == 0) && msg.Content.StartsWith(ConfigData.Data.Bot.Prefix))
            {
                var commandPrefixResult = await this._commands.ExecuteAsync(context, msg.Content.Remove(0, 1), this._provider);

                if (commandPrefixResult.IsSuccess)
                {
                    Statistics.CommandsExecuted.Inc();
                }
                else
                {
                    var logger = new Logger.Logger();
                    logger.LogError(commandPrefixResult.ToString(), context.Message.Content);
                }

                return;
            }

            if (searchResult.Commands[0].Command.Attributes.OfType <LoginRequiredAttribute>().Any())
            {
                var userRegistered = await this._userService.UserRegisteredAsync(context.User);

                if (!userRegistered)
                {
                    var embed = new EmbedBuilder()
                                .WithColor(Constants.LastFMColorRed);
                    var logger = new Logger.Logger();
                    embed.UsernameNotSetErrorResponse(context, customPrefix ?? ConfigData.Data.Bot.Prefix, logger);
                    await context.Channel.SendMessageAsync("", false, embed.Build());

                    return;
                }
            }

            var result = await this._commands.ExecuteAsync(context, argPos, this._provider);

            if (result.IsSuccess)
            {
                Statistics.CommandsExecuted.Inc();
            }
            else
            {
                var logger = new Logger.Logger();
                logger.LogError(result.ToString(), context.Message.Content);
            }
        }