Esempio n. 1
0
 public static ReactionRoleModel UpdateRRModel(this ReactionRoleModel model, IEmote emote, RoleManageModel rule)
 {
     if (model.Rule is not ReactionRuleModel rrModel)
     {
         return(model);
     }
     return(model with {
         Rule = rrModel.WithNewRule(emote.Name, rule)
     });
 }
Esempio n. 2
0
 public static ReactionRoleModel WithEmoteLinked(this ReactionRoleModel model, string emote)
 {
     if (model.Rule is not ReverseRuleModel ruleModel)
     {
         return(model);
     }
     return(model with {
         Rule = ruleModel.WithEmote(emote)
     });
 }
Esempio n. 3
0
        public static Embed CreateReactionRoleRuleEmbed(ReactionRoleModel model)
        {
            var builder = new EmbedBuilder()
                          .WithTitle($"Reaction Role: {model.Name}")
                          .AddField("Channel", MentionUtils.MentionChannel(model.ChannelId))
                          .WithColor(116, 223, 207)
                          .WithCurrentTimestamp();

            if (model.Rule is ReactionRuleModel rule)
            {
                foreach (var(emote, roles) in rule.Reactions)
                {
                    builder.AddRule(emote, roles);
                }
            }
            else
            {
                builder.AddField("Reaction", (model.Rule as ReverseRuleModel).Emote);
            }

            return(builder.Build());
        }
Esempio n. 4
0
        public async Task <bool> AddOrUpdateReactionRole(ReactionRoleModel reactionRole)
        {
            try
            {
                var id = reactionRole.Rule switch
                {
                    ReactionRuleModel model => _context.ReactionRuleModels.Update(model).Entity.Id,
                    ReverseRuleModel model => _context.ReverseRuleModels.Update(model).Entity.Id
                };
                await _context.SaveChangesAsync();

                var storage = reactionRole.ToStorage() with {
                    RuleId = id
                };
                var entity =
                    await _context.ReactionRoleModels.Include(x => x.Rule)
                    .FirstOrDefaultAsync(x =>
                                         x.Name == storage.Name && x.GuildId == storage.GuildId);

                if (entity is null)
                {
                    _context.ReactionRoleModels.Add(storage);
                }
                else
                {
                    _context.Entry(entity).CurrentValues.SetValues(storage);
                }

                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return(true);
        }
        private async Task <bool> ManageRR(ReactionRoleModel rrModel, string eventType)
        {
            List <IEmote> emotes = new();

            while (true)
            {
                var ruleResult = await GetReactionRole();

                if (ruleResult.IsFailure())
                {
                    return(false);
                }
                var(emote, roleManageModel) = ruleResult.Get();
                _logging.Verbose("Writing new Rule...");
                rrModel = rrModel.UpdateRRModel(emote, roleManageModel);
                emotes.Add(emote);
                var checkLoop = await GetIsThereAnotherRule();

                if (checkLoop.IsFailure())
                {
                    return(false);
                }
                if (checkLoop.Get() == false)
                {
                    break;
                }
            }

            var res =
                from title in GetRRTitle()
                from content in GetRRContent()
                select CreateReactionRoleMessageEmbed(title, content);

            var embedRes = await res;

            if (embedRes.IsFailure())
            {
                return(false);
            }

            var embed = embedRes.Get();

            _logging.Info("Created embed!");

            var msg = await Context.Guild.GetTextChannel(rrModel.ChannelId).SendMessageAsync(embed: embed);

            rrModel = rrModel with {
                MessageId = msg.Id
            };

            _logging.Info("Sent Message!");
            //await msg.RemoveAllReactionsAsync();
            await msg.AddReactionsAsync(emotes.ToArray());

            _rrService.UpsertReactionMessage(rrModel);
            await _rrRepo.AddOrUpdateReactionRole(rrModel);

            _logging.Info($"Successfully {eventType}!");
            await SendChannelMessage(
                $"> **Successfully {eventType}!**");
            await SendChannelMessage(embed : CreateReactionRoleRuleEmbed(rrModel));

            return(true);
        }
    }