コード例 #1
0
        public async Task RedditSubscribe([Summary("The interval between each check, in minutes.")] int minutes, [Summary("The name of the subreddit.")] string subreddit)
        {
            if (minutes < 5)
            {
                await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("Your check interval cannot be shorter than five minutes."), lifeTime : Config.FeedbackMessageLifeTime);

                return;
            }
            if (minutes > 1440)
            {
                await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("Your check interval cannot be longer than a day."), lifeTime : Config.FeedbackMessageLifeTime);

                return;
            }

            using (SubDBContext DBContext = DBFactory.Create <SubDBContext>())
            {
                string channelId = Context.Channel.Id.ToString();
                string userId    = Context.User.Id.ToString();

                var subExists = (DBContext.RedditSubscribers.FromSql("SELECT * FROM RedditSubscribers WHERE Id = {0} AND Subscriber = {1} AND Subreddit = {2} LIMIT 1", Context.Channel.Id.ToString(), Context.User.Id.ToString(), subreddit).AsNoTracking().FirstOrDefault() != null);
                if (subExists)
                {
                    await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("This channel is already subscribed to the specified subreddit."), lifeTime : Config.FeedbackMessageLifeTime);

                    return;
                }
                var newUser = new RedditSubscriber(channelId, userId, subreddit, "Text", minutes, DateTime.Now);
                await DBContext.RedditSubscribers.AddAsync(newUser);

                await DBContext.SaveChangesAsync();

                RedditService.AddRedditStream(Context.Channel as SocketTextChannel, subreddit, minutes, Context.User.Id);
                await ReplyAsync("", embed : EmbedService.MakeSuccessFeedbackEmbed($"You have subscribed the channel {Context.Channel.Name.Bold()} to the subreddit {subreddit.Bold()}!"));
            }
        }