Skip to content

Commit 75529ef

Browse files
committed
[Update]
1 parent 2999da0 commit 75529ef

File tree

9 files changed

+158
-89
lines changed

9 files changed

+158
-89
lines changed

command/ctf.command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ const CTFCommand = {
2323
await checkFlag(options.getString("flag"), user, interaction);
2424
break;
2525
case "challenge":
26+
const idChall = Math.random().toString(36).substring(2, 10);
2627
const password = options.getString("password");
2728
if (password === adminPassword) {
28-
await saveFlag(options.getString("id"),
29+
await saveFlag(idChall,
2930
options.getString("author"),
3031
options.getString("chall"),
3132
options.getNumber("point"),
@@ -49,7 +50,7 @@ const CTFCommand = {
4950
await getInfoHacker(hacker ?? user, interaction);
5051
break;
5152
case "listchall":
52-
await getAllChallenge(options.getString("password") == adminPassword, options.getString("category"), interaction);
53+
await getAllChallenge(options.getString("password") == adminPassword, options.getString("category"), options.getString("idcontest"), interaction);
5354
break;
5455
case "rmchall":
5556
await deleteChallenge(options.getString("password") == adminPassword,

command/slash.deploy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const serverCommands: Array<RESTPostAPIChatInputApplicationCommandsJSONBody> = [
2222
{ name: 'Reverse Engineering', value: 'Reverse Engineering' } as APIApplicationCommandOptionChoice<string>,
2323
{ name: 'Web Exploitation', value: 'Web Exploitation' } as APIApplicationCommandOptionChoice<string>,
2424
).setDescription("Type category"))
25+
.addStringOption(option => option.setName("idcontest").setDescription("Type contest ID (No if not)"))
2526
.addStringOption(option => option.setName("password").setDescription("Type password to verify")),
2627
new SlashCommandBuilder().setName("challenge").setDescription("Add new flag!")
2728
.addStringOption(option => option.setName('id').setDescription("Add new flag!").setRequired(true))

controller/contest.controller.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import ContestModel from "../model/contest.model";
33
import playerModel from "../model/player.model";
44
import teamModel from "../model/team.model";
55
import { createEmbed } from "../feature/component";
6+
import flagModel from "../model/flag.model";
7+
import { Flags } from "../interface/model.interface";
68

79
export const createContest = async (
810
idContest: String | null,
@@ -200,4 +202,39 @@ export const listContests = async (interaction: ChatInputCommandInteraction<Cach
200202
} catch (error) {
201203
await interaction.reply("Không thể lấy danh sách contest vào lúc này!");
202204
}
205+
}
206+
207+
export const getAllChallOfContest = async (admin: Boolean, category: String | null, idContest: String, interaction: ChatInputCommandInteraction<CacheType>) => {
208+
let challenges: Array<Flags>;
209+
if (!category) {
210+
211+
if (admin) {
212+
challenges = await flagModel.find({ idContest: idContest }, "idChall nameAuthor nameChall point level description mode url category idContest");
213+
214+
} else {
215+
challenges = await flagModel.find({ mode: true, idContest: null }, "idChall nameAuthor nameChall point level description mode url category");
216+
}
217+
} else {
218+
if (admin) {
219+
challenges = await flagModel.find({ category: category, idContest: idContest }, "idChall nameAuthor nameChall point level description mode url category idContest");
220+
} else {
221+
challenges = await flagModel.find({ mode: true, category: category, idContest: null }, "idChall nameAuthor nameChall point level description mode url category");
222+
}
223+
}
224+
let infoChallenges = "";
225+
challenges.map((challenge: Flags, index: number) => {
226+
infoChallenges += `${index + 1}. ***Tên thử thách:*** ` + challenge.nameChall +
227+
"***\n\tID:*** " + challenge.idChall +
228+
"***\n\tTác giả:*** " + challenge.nameAuthor +
229+
"***\n\tLoại:*** " + challenge.category +
230+
"***\n\tMô tả:*** " + challenge.description +
231+
"***\n\tĐiểm:*** " + challenge.point +
232+
"***\n\tĐộ khó:*** " + challenge.level +
233+
"***\n\tLink thử thách:*** " + challenge.url + "\n";
234+
if (admin) {
235+
infoChallenges += "***\n\tTrạng thái:*** " + (challenge.mode ? "Public" : "Private") + "***\tID Challenge:*** " + challenge.idChall + "\n" + "***\n\tID Contest:*** " + challenge.idContest ?? "Không" + "\n";
236+
}
237+
});
238+
const embed = createEmbed("Danh sách thử thách" + (category ? ` thuộc loại ${category}` : ""), infoChallenges);
239+
await interaction.reply({ embeds: [embed] });
203240
}

controller/flag.controller.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import FlagModel from "../model/flag.model";
44
import scoreModel from "../model/score.model";
55
import playerModel from "../model/player.model";
66
import { createEmbed } from "../feature/component";
7+
import contestModel from "../model/contest.model";
78

89
export const saveFlag = async (
910
idChall: String | null,
@@ -54,38 +55,42 @@ export const checkFlag = async (flag: String | null, players: User, interaction:
5455
}
5556

5657

57-
export const getAllChallenge = async (admin: Boolean, category: String | null, interaction: ChatInputCommandInteraction<CacheType>) => {
58-
let challenges;
59-
if (!category) {
60-
61-
if (admin) {
62-
challenges = await FlagModel.find({}, "idChall nameAuthor nameChall point level description mode url category idContest");
58+
export const getAllChallenge = async (admin: Boolean, category: String | null, idContest: String | null, interaction: ChatInputCommandInteraction<CacheType>) => {
6359

64-
} else {
65-
challenges = await FlagModel.find({ mode: true, idContest: null }, "idChall nameAuthor nameChall point level description mode url category");
66-
}
60+
const contest = await contestModel.findOne({ idContest: idContest });
61+
62+
if (contest == null) {
63+
await interaction.reply("Không tìm thấy contest này!");
6764
} else {
68-
if (admin) {
69-
challenges = await FlagModel.find({category: category}, "idChall nameAuthor nameChall point level description mode url category idContest");
70-
} else {
71-
challenges = await FlagModel.find({ mode: true, category: category, idContest: null }, "idChall nameAuthor nameChall point level description mode url category");
65+
let challenges: Array<Flags> = [];
66+
challenges = await FlagModel.find({
67+
mode: !admin,
68+
category: category ?? { $regex: /.*/ },
69+
idContest: idContest
70+
}, "idChall nameAuthor nameChall point level description mode url category");
71+
72+
if (challenges.length == 0) {
73+
await interaction.reply(`Các challenge của contest ***${contest.nameContest}*** chưa được cập nhập hoặc public!`);
7274
}
73-
}
74-
let infoChallenges = "";
75-
challenges.map((challenge: Flags) => {
76-
infoChallenges += challenge.idChall + ". ***Tên thử thách:*** " + challenge.nameChall +
77-
"***\n\tTác giả:*** " + challenge.nameAuthor +
78-
"***\n\tLoại:*** " + challenge.category +
79-
"***\n\tMô tả:*** " + challenge.description +
80-
"***\n\tĐiểm:*** " + challenge.point +
81-
"***\n\tĐộ khó:*** " + challenge.level +
82-
"***\n\tLink thử thách:*** " + challenge.url + "\n";
83-
if (admin) {
84-
infoChallenges += "***\n\tTrạng thái:*** " + (challenge.mode ? "Public" : "Private") + "***\tID Challenge:*** " + challenge.idChall + "\n" + "***\n\tID Contest:*** " + challenge.idContest ?? "Không" + "\n";
75+
else {
76+
let infoChallenges = "";
77+
challenges.map((challenge: Flags, index: number) => {
78+
infoChallenges += `${index + 1}. ***Tên thử thách:*** ` + challenge.nameChall +
79+
"***\n\tID:*** " + challenge.idChall +
80+
"***\n\tTác giả:*** " + challenge.nameAuthor +
81+
"***\n\tLoại:*** " + challenge.category +
82+
"***\n\tMô tả:*** " + challenge.description +
83+
"***\n\tĐiểm:*** " + challenge.point +
84+
"***\n\tĐộ khó:*** " + challenge.level +
85+
"***\n\tLink thử thách:*** " + challenge.url + "\n";
86+
if (admin) {
87+
infoChallenges += "***\n\tTrạng thái:*** " + (challenge.mode ? "Public" : "Private") + "***\tID Challenge:*** " + challenge.idChall + "\n" + "***\n\tID Contest:*** " + challenge.idContest ?? "Không" + "\n";
88+
}
89+
});
90+
const embed = createEmbed("Danh sách thử thách" + (category ? ` thuộc loại ${category}` : ""), infoChallenges);
91+
await interaction.reply({ embeds: [embed] });
8592
}
86-
});
87-
const embed = createEmbed("Danh sách thử thách" + (category ? ` thuộc loại ${category}` : ""), infoChallenges);
88-
await interaction.reply({ embeds: [embed] });
93+
}
8994
}
9095

9196

@@ -121,7 +126,7 @@ export const scoreBoard = async (interaction: ChatInputCommandInteraction<CacheT
121126
const score = await playerModel.find({}, "idUser nameUser point numberFlags level").sort({ point: -1 });
122127
let infoScore = "";
123128
score.map((player, index) => {
124-
infoScore += `${index + 1}. ***ID:*** ${player.idUser}***\tTên:*** ${player.nameUser}***\tĐiểm:*** ${player.point}***\tSố lượng cờ:*** ${player.numberFlags}***\tCấp độ:*** ${player.level}\n`;
129+
infoScore += `${index + 1}. ***ID:*** ${player.idUser}***\n\tTên:*** ${player.nameUser}***\tĐiểm:*** ${player.point}***\tSố lượng cờ:*** ${player.numberFlags}***\tCấp độ:*** ${player.level}\n`;
125130
});
126131
const embed = createEmbed("Bảng xếp hạng", infoScore);
127132
await interaction.reply({ embeds: [embed] });

controller/player.controller.ts

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ const setLevelHacker = (point: Number) => {
1616
} else if (1000 < value && value <= 1500) {
1717
return "🏴‍☠️ Hacker";
1818
} else if (1500 < value && value <= 2500) {
19-
return "🥷 Ninja warrior";
19+
return "🥷 Binary warrior";
2020
} else if (2500 < value && value <= 3500) {
2121
return "🚩 Redteam";
22-
} else if (3500 < value && value < 10000) {
22+
} else if (3500 < value && value < 5000) {
2323
return "⚔️ Legendary";
24-
} else {
25-
return "🎖️ GOD";
24+
} else if (5000 <= value && value < 10000) {
25+
return "🎖️ Guru"
26+
} else if (10000 <= value && value < 20000) {
27+
return "🏆 Omniscient";
28+
} else if (20000 <= value && value < 50000) {
29+
return "👑 God";
30+
} else if (50000 <= value) {
31+
return "👸 Root King";
2632
}
2733
}
2834

@@ -31,12 +37,20 @@ export const getInfoHacker = async (player: User, interaction: ChatInputCommandI
3137
if (!user) {
3238
await interaction.reply("Người dùng này chưa có trong hệ thống! Submit ít nhất một Flag để được thêm vào hệ thống");
3339
} else {
34-
const teamName = user.idTeam ? (await teamModel.findOne({ idTeam: user.idTeam }))?.name : "Chưa có team";
40+
let listTeam: (string | null)[] = user.idTeam
41+
? await Promise.all(user.idTeam.map(async (currentValue: String, index: number) => {
42+
const team = await teamModel.findOne({ idTeam: currentValue });
43+
return team ? team.name + " - " + team.idTeam : null;
44+
}))
45+
: ["Chưa có team"];
46+
47+
listTeam = listTeam.join("\n\t\t") as unknown as (string | null)[];
48+
3549
const infoHacker = `***Biệt danh***: ${user.nameUser}` +
3650
`***\nCấp độ***: ${user.level}` +
3751
`***\nSố flag đã submit***: ${user.numberFlags}` +
3852
`***\nĐiểm số***: ${user.point}`
39-
+ `***\nTeam***: ${teamName}`;
53+
+ `***\nTeam***: ${listTeam}`;
4054
const embed = createEmbed(`Thông tin của hacker: "***${player.globalName}***"`,
4155
infoHacker
4256
);
@@ -62,64 +76,65 @@ export const joinTeam = async (hacker: User, idTeam: String | null, interaction:
6276
if (!user) {
6377
await interaction.reply("Người dùng này chưa có trong hệ thống! Submit ít nhất một Flag để được thêm vào hệ thống");
6478
} else {
65-
if (user.idTeam) {
66-
await interaction.reply("Bạn đã có team rồi!");
67-
} else {
68-
const team = await teamModel.findOne({ idTeam: idTeam });
69-
if (team) {
70-
user.idTeam = team.idTeam;
71-
await user.save();
72-
await interaction.reply("Đã tham gia team thành công!");
73-
} else {
74-
await interaction.reply("Team không tồn tại!");
79+
const team = await teamModel.findOne({ idTeam: idTeam });
80+
if (team) {
81+
user.idTeam = [];
82+
if (team.idTeam) {
83+
user.idTeam.push(team.idTeam);
7584
}
85+
await user.save();
86+
await interaction.reply("Đã tham gia team thành công!");
87+
} else {
88+
await interaction.reply("Team không tồn tại!");
7689
}
7790
}
7891
}
7992

8093
export const createTeam = async (hacker: User, nameTeam: String, description: String, interaction: ChatInputCommandInteraction<CacheType>) => {
8194
try {
8295
const user = await playerModel.findOne({ idUser: hacker.id });
83-
if (!user) {
84-
await interaction.reply("Người dùng này chưa có trong hệ thống! Submit ít nhất một Flag để được thêm vào hệ thống");
85-
} else {
86-
if (user.idTeam) {
87-
await interaction.reply("Bạn đã có team rồi!");
96+
if (user) {
97+
const team = await teamModel.findOne({ name: nameTeam });
98+
if (team) {
99+
await interaction.reply("Tên team đã tồn tại!");
88100
} else {
89-
const team = await teamModel.findOne({ name: nameTeam });
90-
if (team) {
91-
await interaction.reply("Tên team đã tồn tại!");
101+
const idTeam = Math.random().toString(36).substring(2, 14);
102+
const newTeam = new teamModel({
103+
idTeam: idTeam,
104+
name: nameTeam,
105+
description: description,
106+
score: 0,
107+
contests: [],
108+
members: [user]
109+
});
110+
await newTeam.save();
111+
112+
if (user.idTeam) {
113+
user.idTeam.push(idTeam);
92114
} else {
93-
const idTeam = Math.random().toString(36).substring(2, 14);
94-
const newTeam = new teamModel({
95-
idTeam: idTeam,
96-
name: nameTeam,
97-
description: description,
98-
score: 0,
99-
contests: [],
100-
members: [user]
101-
});
102-
await newTeam.save();
103-
user.idTeam = newTeam.idTeam;
104-
await user.save();
105-
await interaction.reply("Tạo team thành công!");
115+
user.idTeam = [idTeam];
106116
}
117+
118+
await user.save();
119+
await interaction.reply("Tạo team thành công!");
107120
}
121+
} else {
122+
await interaction.reply("Người dùng này chưa có trong hệ thống! Submit ít nhất một Flag để được thêm vào hệ thống");
108123
}
109124
} catch (error) {
125+
console.log(error);
126+
110127
await interaction.reply("Có lỗi xảy ra! Không thể tạo team bây giờ.");
111128
}
112129
}
113130

114131
export const leaveTeam = async (hacker: User, interaction: ChatInputCommandInteraction<CacheType>) => {
115132
const user = await playerModel.findOne({ idUser: hacker.id });
116-
if (!user) {
117-
await interaction.reply("Người dùng này chưa có trong hệ thống! Submit ít nhất một Flag để được thêm vào hệ thống");
118-
} else {
119-
if (user.idTeam) {
133+
if (user) {
134+
if ((user.idTeam?.length ?? 0) > 0) {
120135
const team = await teamModel.findOne({ idTeam: user.idTeam });
121136
if (team) {
122-
user.idTeam = "";
137+
user.idTeam = [];
123138
await user.save();
124139
await interaction.reply("Đã rời team thành công!");
125140
} else {
@@ -128,8 +143,11 @@ export const leaveTeam = async (hacker: User, interaction: ChatInputCommandInter
128143
} else {
129144
await interaction.reply("Bạn chưa có team!");
130145
}
146+
} else {
147+
await interaction.reply("Người dùng này chưa có trong hệ thống! Submit ít nhất một Flag để được thêm vào hệ thống");
131148
}
132149
}
150+
133151
// export const getAllChallengeSolved = async (hacker: User, interaction: ChatInputCommandInteraction<CacheType>){
134152

135153
// }

index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@ import colors from 'colors';
66

77
require('dotenv').config();
88

9-
connectDB();
9+
try {
10+
connectDB();
1011

11-
client.on("guildCreate", guild => {
12-
const idGuild = guild.id;
13-
registerSlashCommand(idGuild);
14-
console.log(colors.green(`[+] Bot joined guild: ${guild.name}, with ID: ${idGuild}`));
15-
})
12+
client.on("guildCreate", guild => {
13+
const idGuild = guild.id;
14+
registerSlashCommand(idGuild);
15+
console.log(colors.green(`[+] Bot joined guild: ${guild.name}, with ID: ${idGuild}`));
16+
})
1617

17-
client.once('ready', () => {
18-
client.guilds.cache.forEach(guild => {
19-
registerSlashCommand(guild.id)
18+
client.once('ready', () => {
19+
client.guilds.cache.forEach(guild => {
20+
registerSlashCommand(guild.id)
21+
});
22+
console.log(colors.green('[+] Bot is Ready!'));
2023
});
21-
console.log(colors.green('[+] Bot is Ready!'));
22-
});
2324

24-
ctfCommand.command(client);
25+
ctfCommand.command(client);
2526

26-
client.login(process.env.TOKEN_BOT).then((data: string) => console.log(colors.green("[+] Server is Connected!"))).catch((err: string) => { console.log(colors.red("[-] Cannot connect to server")); console.log(err); });
27+
client.login(process.env.TOKEN_BOT).then((data: string) => console.log(colors.green("[+] Server is Connected!"))).catch((err: string) => { console.log(colors.red("[-] Cannot connect to server")); console.log(err); });
28+
} catch (error) {
29+
console.log(colors.red("[-] Error: " + error));
30+
}

interface/model.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface Players extends mongoose.Document {
2121
point: Number,
2222
level?: String,
2323
numberFlags: Number,
24-
idTeam?: String,
24+
idTeam?: String[],
2525
}
2626

2727
export interface Score extends mongoose.Document {

model/flag.model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const FlagsSchema = new mongoose.Schema<Flags>({
4343
},
4444
category: {
4545
type: String,
46+
},
47+
idContest: {
48+
type: String,
4649
}
4750

4851
}, { timestamps: true });

model/player.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const PlayerSchema = new mongoose.Schema<Players>({
2929
},
3030

3131
idTeam: {
32-
type: String,
33-
default: null
32+
type: Array<String> (),
33+
default: []
3434
},
3535

3636
}, { timestamps: true });

0 commit comments

Comments
 (0)