add method

Future<void> add(
  1. Snowflake id, {
  2. String? reason,
})

Add a Role to the GuildMember

Example :

final Role? role = guild.roles.cache.get('446556480850755604');
final GuildMember? member = guild.members.cache.get('240561194958716924');

if (member != null && role != null) {
  await member.roles.add(role.id)
}

You can pass a reason for the audit logs.

Example :

await member.roles.add('446556480850755604', reason: 'I love this user');

Implementation

Future<void> add (Snowflake id, {String? reason}) async {
  Role? role = manager.cache.get(id);

  if(role == null) {
    throw NotExistException('You can\'t add a role that don\'t exist!');
  }

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

  if (response.statusCode == 204) {
    cache.putIfAbsent(id, () => role);
    return;
  }

  final payload = jsonDecode(response.body);

  if(payload['code'] == DiscordErrorsCode.missingPermissions.value) {
    container.use<ConsoleService>().warn('Bot don\'t have permissions to add or remove roles !');
    return;
  }
}