Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
as6325400 committed Feb 5, 2024
1 parent 0aec863 commit 81f284a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ dotenv.config();

await BOT.login(process.env.BOT_TOKEN);

const query : Query = await Query.init();
await User.loadUser(USERID_SET);

BOT.on("ready", () => {
console.log("Bot is ready!");
channel = BOT.channels.cache.get(process.env.BOT_CHANNEL_ID!) as TextChannel;
});

const query : Query = await Query.init();
await User.loadUser(USERID_SET);


BOT.on("messageCreate", async (message) => {
handleMessage(message, query);
});
Expand All @@ -36,8 +37,12 @@ dotenv.config();
// eslint-disable-next-line no-constant-condition
while (true) {
for (const user of User.getUserSet()) {
// console.log(user);
const newProblemSets = await getAcceptSet(user.Id);
if (!user.equalSets(newProblemSets)) {
const diff = user.diffSets(newProblemSets);
console.log(diff);
if (diff.size > 0) {
console.log(`User ${user.Name} has new accepted problems!`);
channel?.send(`User ${user.Name} has new accepted problems!`);
const diff = user.diffSets(newProblemSets);
for (const problem of diff) {
Expand All @@ -51,6 +56,6 @@ dotenv.config();
user.updateAccept(diff);
}
}
await new Promise(resolve => setTimeout(resolve, 1000 * 60));
await new Promise(resolve => setTimeout(resolve, 1000 * 5));
}
})();
32 changes: 29 additions & 3 deletions src/messageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { Query } from "./query";
import { User } from "./user";
import { getUserExist } from "./webcrawler";

function isNumber(input: string): boolean {
const regex = /^[0-9]+$/;
return regex.test(input);
}

export async function handleMessage(message: Message, query: Query, ) {
if (message.author.bot || !message.content.startsWith(process.env.BOT_PREFIX!)) return;
const command : string[] = message.content.split(" ");
if (message.author.bot || command[0] != process.env.BOT_PREFIX!) return;
if (command.length < 2) return;
if (command[1] === "tagsets") {

Expand All @@ -27,18 +32,39 @@ export async function handleMessage(message: Message, query: Query, ) {
return;
}

if(command.length > 3) {
message?.reply("Please input the correct user id!");
return;
}

if(command[2].length > 7){
message?.reply("Please input the correct user id!");
return;
}

if(isNumber(command[2]) === false) {
message?.reply("Please input the correct user id!");
return;
}

if (getUserExist(command[2]) === null) {
message?.reply("User does not exist!");
return;
}

command[2] = parseInt(command[2]).toString();

const newUser: User | string = await User.createUser(command[2]);

if (typeof newUser === "string") {
message?.reply(newUser);
}
else {
message?.reply(`User ${newUser.Name} has been added!`);
else if (typeof newUser === "object"){
console.log(newUser);
message?.reply(`User ${newUser.Name} add success!`);
}
else if (newUser === undefined) {
message?.reply("User does not exist!");
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ export class User{
return diff;
}

public equalSets (newSet : Set<string>): boolean {
return this.diffSets(newSet).size === 0;
}

public updateAccept(newAcceptProblem: Set<string>): void {
for (const problem of newAcceptProblem) {
this.AcceptProblem.add(problem);
}
this.Accept = this.AcceptProblem.size;
}


Expand Down
2 changes: 1 addition & 1 deletion src/webcrawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function getUserExist(id: string): Promise<string | null> {
const html = response.data;
const $ = cheerio.load(html);
const name = $("h1").text().split(" ")[1];
return name;
return name == undefined ? null : name;
} catch (error) {
console.error("Error fetching or parsing:", error);
return null;
Expand Down

0 comments on commit 81f284a

Please sign in to comment.