| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * Copyright 2021 Craig Edwards and D++ contributors |
| 7 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | ************************************************************************************/ |
| 22 | #include <dpp/auditlog.h> |
| 23 | #include <dpp/discordevents.h> |
| 24 | #include <dpp/json.h> |
| 25 | |
| 26 | namespace dpp { |
| 27 | |
| 28 | using json = nlohmann::json; |
| 29 | |
| 30 | audit_entry::audit_entry(): type(aut_guild_update) { |
| 31 | } |
| 32 | |
| 33 | audit_entry &audit_entry::fill_from_json_impl(nlohmann::json *j) { |
| 34 | this->id = snowflake_not_null(j, "id"); |
| 35 | this->type = (audit_type)int8_not_null(j, "action_type"); |
| 36 | this->user_id = snowflake_not_null(j, "user_id"); |
| 37 | this->target_id = snowflake_not_null(j, "target_id"); |
| 38 | this->reason = string_not_null(j, "reason"); |
| 39 | if (j->contains("changes")) { |
| 40 | auto &c = (*j)["changes"]; |
| 41 | for (auto & change : c) { |
| 42 | audit_change ac; |
| 43 | ac.key = string_not_null(&change, "key"); |
| 44 | if (change.find("new_value") != change.end()) { |
| 45 | ac.new_value = change["new_value"].dump(-1, ' ', false, json::error_handler_t::replace); |
| 46 | } |
| 47 | if (change.find("old_value") != change.end()) { |
| 48 | ac.old_value = change["old_value"].dump(-1, ' ', false, json::error_handler_t::replace); |
| 49 | } |
| 50 | this->changes.push_back(ac); |
| 51 | } |
| 52 | } |
| 53 | if (j->contains("options")) { |
| 54 | auto &o = (*j)["options"]; |
| 55 | audit_extra opts; |
| 56 | opts.automod_rule_name = string_not_null(&o, "auto_moderation_rule_name"); |
| 57 | opts.automod_rule_trigger_type = string_not_null(&o, "auto_moderation_rule_trigger_type"); |
| 58 | opts.channel_id = snowflake_not_null(&o, "channel_id"); |
| 59 | opts.count = string_not_null(&o, "count"); |
| 60 | opts.delete_member_days = string_not_null(&o, "delete_member_days"); |
| 61 | opts.id = snowflake_not_null(&o, "id"); |
| 62 | opts.members_removed = string_not_null(&o, "members_removed"); |
| 63 | opts.message_id = snowflake_not_null(&o, "message_id"); |
| 64 | opts.role_name = string_not_null(&o, "role_name"); |
| 65 | opts.type = string_not_null(&o, "type"); |
| 66 | opts.application_id = snowflake_not_null(&o, "application_id"); |
| 67 | this->extra = opts; |
| 68 | } |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | auditlog& auditlog::fill_from_json_impl(nlohmann::json* j) { |
| 73 | set_object_array_not_null<audit_entry>(j, "audit_log_entries", this->entries); |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | } |
| 78 |