Ejemplo n.º 1
0
        public async Task ClientOnReactionAdded(Cacheable <IUserMessage, ulong> cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
        {
            try
            {
                //Reaction doesn't match a star
                if (!reaction.Emote.Name.Equals("⭐"))
                {
                    return;
                }
                //get Message
                var msg = await cacheable.GetOrDownloadAsync();

                //Dont do anything if the msg originates from a bot
                if (msg.Author.IsBot)
                {
                    return;
                }
                //Reaction was a star
                using (SoraContext soraContext = new SoraContext())
                {
                    var guild   = ((SocketGuildChannel)socketMessageChannel).Guild;
                    var guildDb = Utility.GetOrCreateGuild(guild.Id, soraContext);
                    //Either the starboard wasn't set up or the channel doesnt exist anymore.
                    if (guildDb.StarChannelId == 0)
                    {
                        return;
                    }
                    var starChannel = guild.GetTextChannel(guildDb.StarChannelId);
                    if (starChannel == null)
                    {
                        //guildDb.StarChannelId = 0; //Reset the channelID to 0 so in the future we dont have to save anything anymore :D
                        //await soraContext.SaveChangesAsync(); //TODO TEMPORARILY DISABLED DUE TO SOME ERROR
                        return;
                    }
                    //Check if reaction is from author
                    if (msg.Author.Id == reaction.UserId)
                    {
                        return;
                    }
                    //check if it was added once before and if it was added too many times!
                    var starMsg = guildDb.StarMessages.FirstOrDefault(x => x.MessageId == msg.Id);
                    if (starMsg != null && starMsg.HitZeroCount >= 3)
                    {
                        return;
                    }
                    //if it was null create a new one otherwise keep the old one
                    bool wasNull = false;
                    if (starMsg == null)
                    {
                        starMsg = new StarMessage()
                        {
                            GuildForeignId = guild.Id,
                            HitZeroCount   = 0,
                            MessageId      = msg.Id,
                            StarCount      = 0,
                            IsPosted       = false
                        };
                        wasNull = true;
                    }

                    //Add star
                    starMsg.StarCount++;
                    //Check if its enough to post
                    if (starMsg.StarCount >= guildDb.StarMinimum && !starMsg.IsPosted)
                    {
                        //POST
                        starMsg.PostedMsgId = await PostStarMessage(starChannel, msg);

                        if (starMsg.PostedMsgId == 0)
                        {
                            try
                            {
                                await socketMessageChannel.SendMessageAsync("", embed : Utility.ResultFeedback(
                                                                                Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2], "Something failed. Can't add msg to starboard. Serenity#0783 has been notified"));
                            }
                            catch (Exception e)
                            {
                                await SentryService.SendMessage("EVEN FAILED WITH ERROR MESSAGEEEEEEEEEEEEE :C\n" + e);

                                return;
                            }

                            return;
                        }
                        starMsg.IsPosted = true;
                    }

                    //save changes made
                    if (wasNull)
                    {
                        guildDb.StarMessages.Add(starMsg);
                    }
                    await soraContext.SaveChangesAsync();

                    //if it wasnt null and posted add to update
                    if (!wasNull && starMsg.IsPosted)
                    {
                        //add it to the starmsg update cache so we can update the count!
                        _toUpdate.Add(new StarMsgUpdateStruct()
                        {
                            GuildId     = guild.Id,
                            PostedMsgId = starMsg.PostedMsgId
                        });
                    }
                    //check if starpostedmsg == 0
                    if (starMsg.PostedMsgId != 0)
                    {
                        await CacheService.SetDiscordUserMessage(starChannel, starMsg.PostedMsgId, TimeSpan.FromHours(1));
                    }
                }
            }
            catch (Exception e)
            {
                await SentryService.SendMessage(e.ToString());
            }
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("I'm alive");
                });
                endpoints.MapPost("/addmessage", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var add = new AddMessage();

                    var dto = await context.Request.ReadFromJsonAsync <NewMessageDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    var result = await add.Add(dto, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(result);
                });
                endpoints.MapPost("/addroom", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var add = new AddRoom();

                    var dto = await context.Request.ReadFromJsonAsync <NewRoomDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    if (jwt.admin == 0)
                    {
                        throw new Exception("You are not allowed to add a new room");
                    }

                    var result = await add.Add(dto, jwt.id, _context);
                });
                endpoints.MapPost("/editmessage", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var edit             = new EditMessage();

                    var dto = await context.Request.ReadFromJsonAsync <EditMessageDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    var result = await edit.Edit(dto, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(result);
                });
                endpoints.MapPost("/editroom", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var edit             = new EditRoom();

                    var dto = await context.Request.ReadFromJsonAsync <EditRoomDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    if (jwt.admin == 0)
                    {
                        throw new Exception("You are not allowed to edit room details");
                    }

                    var result = await edit.Edit(dto, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(result);
                });
                endpoints.MapPost("/deletemessage", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var delete           = new DeleteMessage();

                    var dto = await context.Request.ReadFromJsonAsync <DeleteMessageDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    var result = await delete.Delete(dto.MessageId, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(result);
                });
                endpoints.MapPost("/closeroom", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var close            = new CloseRoom();

                    var dto = await context.Request.ReadFromJsonAsync <CloseOpenRoomDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    if (jwt.admin == 0)
                    {
                        throw new Exception("You are not allowed to close a room");
                    }

                    var result = await close.Close(dto.RoomId, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(result);
                });
                endpoints.MapPost("/openroom", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var open             = new OpenRoom();

                    var dto = await context.Request.ReadFromJsonAsync <CloseOpenRoomDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    if (jwt.admin == 0)
                    {
                        throw new Exception("You are not allowed to open a room");
                    }

                    var result = await open.Open(dto.RoomId, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(result);
                });
                endpoints.MapPost("/star", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var star             = new StarMessage();

                    var dto = await context.Request.ReadFromJsonAsync <StarMessageDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    await star.Star(dto.MessageId, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(StatusCodes.Status200OK);
                });
                endpoints.MapPost("/unstar", async context =>
                {
                    MyDbContext _context = new MyDbContext();
                    var star             = new UnStarMessage();

                    var dto = await context.Request.ReadFromJsonAsync <StarMessageDTO>();
                    var jwt = DecodeHeader.GetJwtToken(context.Request.Headers);

                    await star.UnStar(dto.MessageId, jwt.id, _context);
                    await context.Response.WriteAsJsonAsync(StatusCodes.Status200OK);
                });
            });
        }