toggle method

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

Toggle a Role from the GuildMember. If the user has the role, this method will remove the role, else this method will add the role.

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.toggle(role.id)
}

You can pass a reason for the audit logs.

Example :

await member.roles.toggle('446556480850755604', reason: 'Hello, World!');

Implementation

Future<void> toggle (Snowflake id, {String? reason}) async {
  return cache.containsKey(id)
    ? remove(id, reason: reason)
    : add(id, reason: reason);
}