public async Task <string> RemoveReactRole(SocketGuild guild, IMessage message, Emote emote, Emoji emoji, SocketRole role) { if (!guildsDefinition.reactRoles.ContainsKey(guild.Id)) { return("No react roles for this guild."); } foreach (string group in guildsDefinition.reactRoles[guild.Id].Keys) { string e = emote != null?emote.ToString() : (emoji != null ? emoji.ToString() : null); if (e == null) { return("Invalid emote."); } ReactRolesDefinition rrd = guildsDefinition.reactRoles[guild.Id][group].FirstOrDefault(x => x.messageID == message.Id && x.role == role.Name && (x.emoji == e)); if (rrd != null) { guildsDefinition.reactRoles[guild.Id][group].Remove(rrd); await message.RemoveReactionAsync(emote != null?(IEmote)emote : (IEmote)emoji, client.CurrentUser); return($"Removed reaction role {role.Name}."); } } return($"Reaction for role {role} not found with given parameters."); }
private async Task ReactionRemoved(Cacheable <IUserMessage, ulong> before, ISocketMessageChannel after, SocketReaction reaction) { var t = Task.Factory.StartNew(async() => { if (reaction.User.Value.IsBot) { return; } var chn = reaction.Channel as SocketGuildChannel; if (!guildsDefinition.reactRoles.ContainsKey(chn.Guild.Id)) { return; } // Check if the message exist. bool foundMessage = false; ReactRolesDefinition rrDef = null; string msgGroup = ""; foreach (string group in guildsDefinition.reactRoles[chn.Guild.Id].Keys) { var x = guildsDefinition.reactRoles[chn.Guild.Id][group].FirstOrDefault(x => x.messageID == reaction.MessageId && CompareEmote(x.emoji, reaction.Emote.ToString())); if (x != null) { foundMessage = true; msgGroup = group; rrDef = x; break; } } if (!foundMessage) { return; } // REMOVE ROLE // SocketRole realRole = chn.Guild.Roles.FirstOrDefault(x => x.Name.ToLower() == rrDef.role.ToLower()); if (realRole != null) { await RemoveRole(realRole, chn.Guild, reaction.UserId); } }); }
/// <summary> /// When a user adds an reaction, check if we have to assign a role for that assignment. /// </summary> /// <param name="before"></param> /// <param name="after"></param> /// <param name="reaction"></param> /// <returns></returns> private async Task ReactionAdded(Cacheable <IUserMessage, ulong> before, ISocketMessageChannel after, SocketReaction reaction) { if (reaction.User.Value.IsBot) { return; } var chn = reaction.Channel as SocketGuildChannel; if (!guildsDefinition.reactRoles.ContainsKey(chn.Guild.Id)) { return; } // Check if the message exist. bool foundMessage = false; ReactRolesDefinition rrDef = null; string msgGroup = ""; foreach (string group in guildsDefinition.reactRoles[chn.Guild.Id].Keys) { if (guildsDefinition.reactRoles[chn.Guild.Id][group].FirstOrDefault(x => x.messageID == reaction.MessageId && x.emoji == reaction.Emote.ToString()) != null) { foundMessage = true; msgGroup = group; rrDef = guildsDefinition.reactRoles[chn.Guild.Id][group].FirstOrDefault(x => x.messageID == reaction.MessageId && x.emoji == reaction.Emote.ToString()); break; } } if (!foundMessage) { return; } // ASSIGNING ROLES // if (string.IsNullOrEmpty(msgGroup)) { // No group assigned, so don't bother trying to check other groups. SocketRole realRole = chn.Guild.Roles.FirstOrDefault(x => x.Name.ToLower() == rrDef.role.ToLower()); if (realRole != null) { await AssignRole(realRole, chn.Guild, reaction.UserId); } } else { // Group assigned, make sure nothing else in the group is assigned. for (int i = 0; i < guildsDefinition.reactRoles[chn.Guild.Id][msgGroup].Count; i++) { if (chn.Guild.GetUser(reaction.UserId).Roles.FirstOrDefault(x => x.Name == guildsDefinition.reactRoles[chn.Guild.Id][msgGroup][i].role) != null) { var m = await reaction.Channel.SendMessageAsync($"{reaction.User.Value.Mention}, you can only have one role within group {msgGroup}."); _ = Clean(m); return; } } // Assign role. SocketRole realRole = chn.Guild.Roles.FirstOrDefault(x => x.Name.ToLower() == rrDef.role.ToLower()); if (realRole != null) { await AssignRole(realRole, chn.Guild, reaction.UserId); } } }