execute method
Execute a webhook.
Implementation
Future<Message?> execute(Snowflake id, MessageBuilder builder,
{required String token, bool? wait, Snowflake? threadId, String? threadName, List<Snowflake>? appliedTags, String? username, String? avatarUrl}) async {
final route = HttpRoute()
..webhooks(id: id.toString())
..add(HttpRoutePart(token));
final queryParameters = {if (wait != null) 'wait': wait.toString(), if (threadId != null) 'thread_id': threadId.toString()};
final HttpRequest request;
if (!identical(builder.attachments, sentinelList) && builder.attachments?.isNotEmpty == true) {
final attachments = builder.attachments!;
final payload = {
...builder.build(),
if (threadName != null) 'thread_name': threadName,
if (appliedTags != null) 'applied_tags': appliedTags.map((e) => e.toString()),
if (username != null) 'username': username,
if (avatarUrl != null) 'avatar_url': avatarUrl,
};
final files = <MultipartFile>[];
for (int i = 0; i < attachments.length; i++) {
files.add(MultipartFile.fromBytes(
'files[$i]',
attachments[i].data,
filename: attachments[i].fileName,
));
((payload['attachments'] as List)[i] as Map)['id'] = i.toString();
}
request = MultipartRequest(
route,
method: 'POST',
jsonPayload: jsonEncode(payload),
files: files,
queryParameters: queryParameters,
authenticated: false,
);
} else {
request = BasicRequest(
route,
method: 'POST',
body: jsonEncode({
...builder.build(),
if (threadName != null) 'thread_name': threadName,
if (appliedTags != null) 'applied_tags': appliedTags.map((e) => e.toString()),
}),
queryParameters: queryParameters,
authenticated: false,
);
}
final response = await client.httpHandler.executeSafe(request);
if (wait != true) {
return null;
}
final channelId = Snowflake.parse((response.jsonBody as Map<String, Object?>)['channel_id']!);
final messageManager = (client.channels[channelId] as PartialTextChannel).messages;
final message = messageManager.parse(response.jsonBody as Map<String, Object?>);
client.updateCacheWith(message);
return message;
}