nyxx/nyxx_interactions/lib/src/models/Interaction.dart

85 lines
2.1 KiB
Dart
Raw Normal View History

2020-12-20 17:09:44 +01:00
part of nyxx_interactions;
2021-02-10 17:31:54 +01:00
/// The Interaction data. e.g channel, guild and member
class Interaction extends SnowflakeEntity {
2021-02-10 17:31:54 +01:00
/// Reference to bot instance.
final Nyxx _client;
2020-12-20 17:09:44 +01:00
2021-02-10 17:31:54 +01:00
/// The type of the interaction received.
late final int type;
2020-12-20 17:09:44 +01:00
2021-02-10 17:31:54 +01:00
/// The guild the command was sent in.
late final Cacheable<Snowflake, Guild>? guild;
2020-12-20 17:09:44 +01:00
2021-02-10 17:31:54 +01:00
/// The channel the command was sent in.
2020-12-22 12:33:28 +01:00
late final Cacheable<Snowflake, TextChannel> channel;
2020-12-20 17:09:44 +01:00
2021-02-10 17:31:54 +01:00
/// The member who sent the interaction
late final Member author;
2020-12-20 17:09:44 +01:00
2021-02-10 17:31:54 +01:00
/// Token to send requests
2020-12-20 17:09:44 +01:00
late final String token;
2021-02-10 17:31:54 +01:00
/// Version of interactions api
2020-12-20 17:09:44 +01:00
late final int version;
2021-02-10 17:31:54 +01:00
/// Name of interaction
2021-02-10 17:29:42 +01:00
late final String name;
2021-02-10 17:31:54 +01:00
/// Args of the interaction
late final Iterable<InteractionOption> args;
/// Id of command
late final Snowflake commandId;
Interaction._new(this._client, Map<String, dynamic> raw) : super(Snowflake(raw["id"])) {
this.type = raw["type"] as int;
if (raw["guild_id"] != null) {
this.guild = CacheUtility.createCacheableGuild(
_client,
Snowflake(raw["guild_id"]),
);
} else {
this.guild = null;
}
2020-12-22 12:33:28 +01:00
this.channel = CacheUtility.createCacheableTextChannel(
2021-02-10 17:31:54 +01:00
_client,
Snowflake(raw["channel_id"]),
);
this.author = EntityUtility.createGuildMember(
2021-02-10 17:31:54 +01:00
_client,
Snowflake(raw["guild_id"]),
raw["member"] as Map<String, dynamic>,
);
this.token = raw["token"] as String;
this.version = raw["version"] as int;
this.name = raw["data"]["name"] as String;
this.args = _generateArgs(raw["data"] as Map<String, dynamic>);
this.commandId = Snowflake(raw["data"]["id"]);
}
/// Allows to fetch argument value by argument name
dynamic? getArg(String name) {
try {
return this.args.firstWhere((element) => element.name == name);
} on Error {
return null;
}
}
Iterable<InteractionOption> _generateArgs(Map<String, dynamic> rawData) sync* {
if (rawData["options"] == null) {
return;
}
final options = rawData["options"] as List<dynamic>;
for (final option in options) {
yield InteractionOption._new(option as Map<String, dynamic>);
}
2020-12-20 17:09:44 +01:00
}
2021-02-10 17:29:42 +01:00
}