Skip to content

Commit

Permalink
feat: added first blood notifications to discord
Browse files Browse the repository at this point in the history
  • Loading branch information
BRAVO68WEB committed Jan 22, 2024
1 parent 1671533 commit fd61ade
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
1 change: 0 additions & 1 deletion apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "tauri dev",
"tauri": "tauri"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/api/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ZodEnvironmentVariables = z.object({
MAIL_LOGGER: z.string(),
MAIL_FROM_EMAIL: z.string(),
MAIL_FROM_NAME: z.string(),
DISCORD_BLOOD_HOOK: z.string(),
});

ZodEnvironmentVariables.parse(process.env);
Expand Down
43 changes: 43 additions & 0 deletions packages/api/helpers/notifyBlood.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import axios from "axios";

export const notifyForBlood = async (
{
teamName,
challengeName,
}: {
teamName: string;
challengeName: string;
}
) => {
try {
const res = await axios.post(process.env.DISCORD_BLOOD_HOOK, {
content: "@here",
tts: false,
embeds: [
{
id: Date.now().toString(),
description: "",
fields: [],
title: `🎉 ${teamName} owned first blood 🩸 for ${challengeName}`,
color: 7_782_594,
footer: {
icon_url: "https://rcs.encryptedge.in/logo.png",
text: "eeCTF"
},
author: {
name: "RCS CTF 2024"
}
}
],
components: [],
actions: {},
username: "eeCTF Blood Notify"
}
);
return res.data;
} catch (error) {
if(error instanceof Error) {
throw new TypeError(error.message);
}
}
};
37 changes: 36 additions & 1 deletion packages/api/services/challenges.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ import { SnowflakeId } from "hyperflake";

import { Challenges_Insert_Input, Machines_Insert_Input, Mutation_Root, Query_Root } from "../graphql/types";
import { client } from "../helpers/gqlClient";
import { notifyForBlood } from "../helpers/notifyBlood";

const snowflake = SnowflakeId();

interface IBloodCheck extends Query_Root {
blood_check: {
challenge_id: string;
submission: {
submited_flag: string;
};
}[];
}

export class ChallengeService {
public createMachineS = async (reqBody: IMachineCreateInput) => {
try {
Expand Down Expand Up @@ -164,6 +174,7 @@ export class ChallengeService {

public submitFlagS = async (reqBody: IMachineSubmitFlagInput) => {
try {
let is_blood_aquired = false;
const { challenge_id, submited_flag, team_id, user_id } = reqBody;

if (!challenge_id || !submited_flag || !team_id || !user_id) {
Expand All @@ -175,14 +186,25 @@ export class ChallengeService {
scores(where: {challenge_id: {_eq: $challenge_id}, team_id: { _eq: $team_id }}) {
id
}
blood_check: scores(where: {challenge_id: {_eq: $challenge_id}}) {
challenge_id
submission {
submited_flag
}
}
}
`;

const { scores } : Query_Root = await client.request(queryPreCheck, {
const { scores, blood_check } : IBloodCheck = await client.request(queryPreCheck, {
challenge_id,
team_id,
});

if(blood_check.length > 0){
is_blood_aquired = true;
}

if(scores.length > 0){
throw new Error("Already solved");
}
Expand Down Expand Up @@ -236,6 +258,12 @@ export class ChallengeService {
id
team_id
user_id
team {
name
}
challenge {
name
}
}
}
`;
Expand All @@ -252,6 +280,13 @@ export class ChallengeService {
throw new Error("Failed to submit score");
}

if(!is_blood_aquired){
notifyForBlood({
teamName: insert_scores_one.team.name,
challengeName: insert_scores_one.challenge.name,
});
}

return {
message: "GG!"
};
Expand Down

0 comments on commit fd61ade

Please sign in to comment.