public async Task <HttpStatusCode> SaveCommandItem(CommandItemCreateDto item) { string jsonString = JsonSerializer.Serialize(item); var statusCode = await _client.PostAsync("/api/CommandItems/", item); if (statusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedAccessException("Your username or password is incorrect!"); } _logger.LogDebug("Posting JSON\n {json}:\nresult: {result}", jsonString, (int)statusCode); return(statusCode); }
public async Task <ActionResult <CommandItem> > CreateCommandItem(CommandItemCreateDto commandItemCreate) { var commandItemModel = _mapper.Map <CommandItem>(commandItemCreate); if (commandItemModel.Channel.ChannelName.StartsWith("@")) { commandItemModel.Channel.ChannelName = "DM"; commandItemModel.Channel.ChannelId = 0; } await _commandItemRepo.CreateCommandItem(commandItemModel); await _commandItemRepo.SaveChanges(); return(CreatedAtRoute(nameof(GetCommandItemById), new { Id = commandItemModel.Id }, commandItemModel)); }
private async Task OnCommandExecuted(Optional <CommandInfo> command, ICommandContext context, IResult result) { if (_apiService.ApiIsEnabled) { if (command.IsSpecified) { var commandItem = new CommandItemCreateDto { Guild = new GuildCreateDto { GuildId = context.Guild?.Id ?? 0, GuildName = context.Guild?.Name ?? "DM" }, Channel = new ChannelCreateDto { ChannelId = context.Channel.Id, ChannelName = context.Channel.Name }, Name = command.Value.Name, Module = command.Value.Module.Name, Message = context.Message.Content, Succesful = result.IsSuccess, Date = DateTimeOffset.UtcNow, User = new UserCreateDto { UserId = context.User.Id, UserName = context.User.Username } }; try { await _apiService.CommandItemApi.SaveCommandItem(commandItem); } catch (Exception e) { _logger.LogWarning(e, "An exception occured while trying to contact the API"); } } } if (result.Error == CommandError.UnknownCommand) { //TODO make this optional/a setting Task.Run(async() => { _logger.LogDebug("{user} attempted to use an unknown command ({command}) on {server}/{channel}", context.User.Username, context.Message.Content, context.Guild?.Name ?? "DM", context.Channel); var badCommandMessage = await context.Channel.SendMessageAsync(ImageLookupUtility.GetImageUrl("BADCOMMAND_IMAGES")); await Task.Delay(3500); await badCommandMessage.DeleteAsync(); }); return; } if (!result.IsSuccess) { _logger.LogError("Error Occured for command {command}: {error} in {server}/{channel}", context.Message.Content, result.Error, context.Guild?.Name ?? "DM", context.Channel); Console.WriteLine($"The following error occured: {result.Error}"); await context.Channel.SendMessageAsync($"The following error occured: {result.ErrorReason}"); } }