コード例 #1
0
        public async Task <DateTime> AddTimeout(string recipient, int broadcasterId, double seconds, string twitchBotApiLink)
        {
            DateTime timeoutExpiration = DateTime.UtcNow.AddSeconds(seconds);

            BotTimeout timedoutUser = new BotTimeout();

            if (TimedoutUsers.Any(m => m.Username == recipient))
            {
                timedoutUser = await ApiBotRequest.PatchExecuteTaskAsync <BotTimeout>(
                    twitchBotApiLink + $"bottimeouts/patch/{broadcasterId}?username={recipient}",
                    "timeout",
                    timeoutExpiration);

                TimedoutUsers.RemoveAll(t => t.Username == recipient);
            }
            else
            {
                timedoutUser = await ApiBotRequest.PostExecuteTaskAsync(
                    twitchBotApiLink + $"bottimeouts/create",
                    new BotTimeout { Username = recipient, Timeout = timeoutExpiration, BroadcasterId = broadcasterId }
                    );
            }

            TimedoutUsers.Add(new TimeoutUser
            {
                Username             = recipient,
                TimeoutExpirationUtc = timeoutExpiration,
                HasBeenWarned        = false
            });

            return(timeoutExpiration);
        }
コード例 #2
0
        public async Task <IActionResult> Create([FromBody] BotTimeout userBotTimeout)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.BotTimeout.Add(userBotTimeout);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", new { broadcasterId = userBotTimeout.BroadcasterId, username = userBotTimeout.Username }, userBotTimeout));
        }
コード例 #3
0
        public async Task <string> DeleteUserTimeout(string recipient, int broadcasterId, string twitchBotApiLink)
        {
            BotTimeout removedTimeout = await ApiBotRequest.DeleteExecuteTaskAsync <BotTimeout>(twitchBotApiLink + $"bottimeouts/delete/{broadcasterId}?username={recipient}");

            if (removedTimeout == null)
            {
                return("");
            }

            string name = removedTimeout.Username;

            TimedoutUsers.RemoveAll(r => r.Username == name);
            return(name);
        }
コード例 #4
0
        public async Task <IActionResult> Delete([FromRoute] int broadcasterId, [FromQuery] string username = "")
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var botTimeout = new object();

            if (!string.IsNullOrEmpty(username))
            {
                BotTimeout userBotTimeout = await _context.BotTimeout
                                            .SingleOrDefaultAsync(m => m.Username == username && m.BroadcasterId == broadcasterId);

                if (userBotTimeout == null)
                {
                    return(NotFound());
                }

                _context.BotTimeout.Remove(userBotTimeout);

                botTimeout = userBotTimeout;
            }
            else
            {
                List <BotTimeout> userBotTimeouts = await _context.BotTimeout
                                                    .Where(m => m.BroadcasterId == broadcasterId && m.Timeout < DateTime.UtcNow)
                                                    .ToListAsync();

                if (userBotTimeouts == null || userBotTimeouts.Count == 0)
                {
                    return(NotFound());
                }

                _context.BotTimeout.RemoveRange(userBotTimeouts);

                botTimeout = userBotTimeouts;
            }

            await _context.SaveChangesAsync();

            return(Ok(botTimeout));
        }
コード例 #5
0
        public async Task <IActionResult> Patch([FromRoute] int broadcasterId, [FromQuery] string username, [FromBody] JsonPatchDocument <BotTimeout> botTimeoutPatch)
        {
            BotTimeout userBotTimeout = _context.BotTimeout.SingleOrDefault(m => m.Username == username && m.BroadcasterId == broadcasterId);

            if (userBotTimeout == null)
            {
                return(BadRequest());
            }

            botTimeoutPatch.ApplyTo(userBotTimeout, ModelState);

            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            _context.BotTimeout.Update(userBotTimeout);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserBotTimeoutExists(broadcasterId, username))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(userBotTimeout));
        }