1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! [Application role connection metadata](https://docs.discord.com/developers/resources/application-role-connection-metadata):
//! the fields an app defines for linked roles.
//!
//! An app with a `role_connections_verification_url` declares up to 5 metadata records. Guilds
//! then require values for them on a role (e.g. "account age ≥ 30 days"), and the app stores
//! each user's values as an [`ApplicationRoleConnection`](crate::user::ApplicationRoleConnection).
use std::collections::BTreeMap;
use compact_str::CompactString;
use serde::{Deserialize, Serialize};
use crate::{macros::int_enum, util::Locale};
/// An [application role connection metadata record](https://docs.discord.com/developers/resources/application-role-connection-metadata#application-role-connection-metadata-object):
/// one field that guilds can compare against when granting a linked role.
///
/// discord.js counterpart: `ApplicationRoleConnectionMetadata`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ApplicationRoleConnectionMetadata {
/// How the user's value is compared with the guild's configured value.
#[serde(rename = "type")]
pub kind: MetadataType,
/// The dictionary key of the field (`a-z`, `0-9` and `_`; 1-50 characters).
pub key: CompactString,
/// The field's name (1-100 characters).
pub name: CompactString,
/// Translations of the name, keyed by locale (D-058).
#[serde(
default,
skip_serializing_if = "BTreeMap::is_empty",
deserialize_with = "crate::serde_util::null_as_default"
)]
pub name_localizations: BTreeMap<Locale, CompactString>,
/// The field's description (1-200 characters).
pub description: CompactString,
/// Translations of the description, keyed by locale (D-058).
#[serde(
default,
skip_serializing_if = "BTreeMap::is_empty",
deserialize_with = "crate::serde_util::null_as_default"
)]
pub description_localizations: BTreeMap<Locale, CompactString>,
}
impl ApplicationRoleConnectionMetadata {
/// A record without translations.
///
/// discord.js counterpart: `ApplicationRoleConnectionMetadataEditOptions`.
///
/// # Examples
///
/// ```
/// use djs_model::application_role_connection_metadata::{
/// ApplicationRoleConnectionMetadata, MetadataType,
/// };
///
/// let record = ApplicationRoleConnectionMetadata::new(
/// MetadataType::IntegerGreaterThanOrEqual,
/// "level",
/// "Level",
/// "The player's level",
/// );
/// assert_eq!(
/// serde_json::to_string(&record)?,
/// r#"{"type":2,"key":"level","name":"Level","description":"The player's level"}"#
/// );
/// # Ok::<(), serde_json::Error>(())
/// ```
#[must_use]
pub fn new(
kind: MetadataType,
key: impl Into<CompactString>,
name: impl Into<CompactString>,
description: impl Into<CompactString>,
) -> Self {
Self {
kind,
key: key.into(),
name: name.into(),
name_localizations: BTreeMap::new(),
description: description.into(),
description_localizations: BTreeMap::new(),
}
}
}
int_enum! {
/// The [comparison](https://docs.discord.com/developers/resources/application-role-connection-metadata#application-role-connection-metadata-object-application-role-connection-metadata-type)
/// of an [`ApplicationRoleConnectionMetadata`] record.
///
/// The user's value is stored as a string in
/// [`ApplicationRoleConnection::metadata`](crate::user::ApplicationRoleConnection::metadata):
/// an integer for the integer and boolean (`1`/`0`) comparisons, an ISO 8601 timestamp for
/// the date-time ones (the guild configures a number of days before today).
///
/// discord.js counterpart: `ApplicationRoleConnectionMetadataType`.
#[doc(alias = "ApplicationRoleConnectionMetadataType")]
pub enum MetadataType: u8 {
/// The user's integer is less than or equal to the guild's.
IntegerLessThanOrEqual = 1,
/// The user's integer is greater than or equal to the guild's.
IntegerGreaterThanOrEqual = 2,
/// The user's integer is equal to the guild's.
IntegerEqual = 3,
/// The user's integer is not equal to the guild's.
IntegerNotEqual = 4,
/// The user's date-time is at most the guild's number of days before today.
DatetimeLessThanOrEqual = 5,
/// The user's date-time is at least the guild's number of days before today.
DatetimeGreaterThanOrEqual = 6,
/// The user's boolean is `1` (true).
BooleanEqual = 7,
/// The user's boolean is not `1` (true).
BooleanNotEqual = 8,
}
}