setIcon method

Future<void> setIcon(
  1. String path, {
  2. String? reason,
})

Modifies the icon of the role from String path.

We consider having the file structure

.dart_tool
assets/
  images/
    penguin.png
src/
test/
.env
pubspec.yaml

Example :

final Role? role = guild.roles.cache.get('240561194958716924');
if (role != null) {
  await role.setIcon('assets/images/penguin.png');
}

Implementation

Future<void> setIcon (String path, { String? reason }) async {
  if (!manager.guild.features.contains(GuildFeature.roleIcons)) {
    throw MissingFeatureException("Guild ${manager.guild.name} has no 'ROLE_ICONS' feature.");
  }

  String icon = await Helper.getPicture(path);

  Response response = await ioc.use<DiscordApiHttpService>().patch(url: "/guilds/${manager.guild.id}/roles/$id")
    .payload({ 'icon': icon })
    .auditLog(reason)
    .build();

  if (response.statusCode == 200) {
    _icon = icon;
  }
}