public async Task CheckReactionMessage(CommandContext ctx, string messageToUse) { if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions)) { return; } DiscordMessage message = await CommandHelpers.GetMessageFromUrl(ctx, messageToUse); if (message is null) { return; } ReactionMessage associatedReactionMessage = _reactionInteractions.ReactionMessages.FirstOrDefault(x => x.Message.Equals(message)); if (associatedReactionMessage == null) { await ctx.RespondAsync("Couldn't find the ReactionMessage associated with this Discord message"); return; } await ctx.TriggerTypingAsync(); await _reactionInteractions.CheckAllRules(associatedReactionMessage); await ctx.RespondAsync("Done"); }
public async Task CheckAllRules(ReactionMessage reactionMessage) { IReadOnlyCollection <DiscordMember> guildMembers = await reactionMessage.Message.Channel.Guild.GetAllMembersAsync(); foreach (KeyValuePair <DiscordEmoji, DiscordRole> rule in reactionMessage.Rules) { IEnumerable <DiscordUser> reactedMembers = reactionMessage.Message.GetReactionsAsync(rule.Key, 500).Result; foreach (DiscordMember member in guildMembers) { if (member.Equals(_client.CurrentUser)) { continue; } bool reacted = false; bool hasRole = false; if (reactedMembers.FirstOrDefault(x => x.Equals(member)) != null) { reacted = true; } if (member.Roles.Contains(rule.Value)) { hasRole = true; } if ((reacted && hasRole) || (!reacted && !hasRole)) { continue; } if (reacted) { Console.WriteLine(member.Username + " grant " + rule.Value.Name); await member.GrantRoleAsync(rule.Value); continue; } Console.WriteLine(member.Username + " revoke " + rule.Value.Name); await member.RevokeRoleAsync(rule.Value); } } }
public async Task EditReactionMessage(CommandContext ctx, string messageUrl) { if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions)) { return; } SetupState state = _setupStates[ctx.Guild.Id]; if (state.IsInSetupMode || state.IsInEditMode) { await ctx.RespondAsync( "The bot is already setting up / editing a reaction message, finish it then try again"); return; } DiscordMessage message = await CommandHelpers.GetMessageFromUrl(ctx, messageUrl); if (message is null) { return; } ReactionMessage associatedMessage = _reactionInteractions.ReactionMessages.FirstOrDefault(x => x.Message.Equals(message)); if (associatedMessage == null) { await ctx.RespondAsync("This message doesn't have reaction interactions setup"); return; } state.ReactionMessage.Message = associatedMessage.Message; foreach (KeyValuePair <DiscordEmoji, DiscordRole> rule in associatedMessage.Rules) { state.ReactionMessage.Rules.Add(rule.Key, rule.Value); } state.OriginalReactionMessage = associatedMessage; await ctx.RespondAsync($"Got the message to edit, continue the edit with {ctx.Prefix}{CommandNames.AddEmojiToReactionMessage} (emoji) (mention to role)\nPreview the rules with {ctx.Prefix}{CommandNames.PreviewReactionMessage}"); state.IsInEditMode = true; }
public SerializedReactionMessage(ReactionMessage reactionMessage) { GuildId = (ulong)reactionMessage.Message.Channel.GuildId; ChannelId = reactionMessage.Message.ChannelId; MessageId = reactionMessage.Message.Id; Rules = new List <SerializedRule>(); foreach (KeyValuePair <DiscordEmoji, DiscordRole> rule in reactionMessage.Rules) { if (rule.Key.Id != 0) { Rules.Add(new SerializedRule(true, rule.Key.Id.ToString(), rule.Value.Id)); continue; } Rules.Add(new SerializedRule(false, rule.Key.GetDiscordName(), rule.Value.Id)); } }
private Task DiscordOnMessageReactionRemoved(DiscordClient sender, MessageReactionRemoveEventArgs e) { return(Task.Run(() => { ReactionMessage associatedMessage = ReactionMessages.FirstOrDefault(x => x.Message.Equals(e.Message)); if (associatedMessage == null || e.User.Equals(_client.CurrentUser)) { return; } if (!associatedMessage.Rules.TryGetValue(e.Emoji, out DiscordRole roleToGrant)) { return; } DiscordMember member = ((DiscordMember)e.User); member.RevokeRoleAsync(roleToGrant).Wait(); GuildEventLogger.Instance .LogInfo(e.Guild, $"{member.Mention} was revoked the role {roleToGrant.Mention} by reacting to [this message]({associatedMessage.Message.JumpLink})").Wait(); })); }
public void LoadReactionMessages() { ReactionMessages = new List <ReactionMessage>(); if (!File.Exists(ReactionMessagesLocalPath)) { return; } string json = File.ReadAllText(ReactionMessagesLocalPath); List <SerializedReactionMessage> serializedReactionMessages = JsonConvert.DeserializeObject <List <SerializedReactionMessage> >(json); foreach (SerializedReactionMessage serializedReactionMessage in serializedReactionMessages) { ReactionMessage reactionMessage = serializedReactionMessage.ToReactionMessage(); if (reactionMessage != null) { ReactionMessages.Add(reactionMessage); } } }
public async Task RemoveReactionMessage(CommandContext ctx, string messageToUse) { if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions)) { return; } DiscordMessage message = await CommandHelpers.GetMessageFromUrl(ctx, messageToUse); if (message is null) { return; } ReactionMessage associatedReactionMessage = _reactionInteractions.ReactionMessages.FirstOrDefault(x => x.Message.Equals(message)); if (associatedReactionMessage == null) { await ctx.RespondAsync("Couldn't find the ReactionMessage associated with this Discord message"); return; } _reactionInteractions.ReactionMessages.Remove(associatedReactionMessage); await ctx.RespondAsync("Succesfully removed the corresponding ReactionMessage"); _reactionInteractions.SaveToFile(); foreach (KeyValuePair <DiscordEmoji, DiscordRole> rule in associatedReactionMessage.Rules) { try { await associatedReactionMessage.Message.DeleteOwnReactionAsync(rule.Key); } catch { } } }