Skip to content

Commit

Permalink
improved logging and added clearing command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pdzly committed Oct 20, 2024
1 parent 0b1a062 commit 202b2d8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
31 changes: 29 additions & 2 deletions src/commands/verifyCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,11 @@ This message is automated! Please dont reply to this message!`,
oldMember,
newMember,
]: ArgsOf<"guildMemberUpdate">) {
console.log(newMember.pending, oldMember.pending);
if (!oldMember.user.bot && (oldMember.pending && !newMember.pending || !newMember.guild.features.includes("MEMBER_VERIFICATION_GATE_ENABLED"))) {
if (
!oldMember.user.bot &&
((oldMember.pending && !newMember.pending) ||
!newMember.guild.features.includes("MEMBER_VERIFICATION_GATE_ENABLED"))
) {
const config = await this.communityConfigService.getCommunityConfig(
newMember.guild.id
);
Expand Down Expand Up @@ -425,4 +428,28 @@ Have fun!`
);
}
}

@Slash({
description: "Clear all broken connections",
name: "clearconnections",
defaultMemberPermissions: ["ManageRoles"],
})
async clearconnections(
@SlashOption({
name: "dryrun",
description: "Don't actually clear the connections, just show them",
type: ApplicationCommandOptionType.Boolean,
required: true,
})
dryrun: boolean,
interaction: CommandInteraction
) {
interaction.deferReply({ ephemeral: true });
var cleared_connections =
this.verifiedUserService.clearBrokenConnections(dryrun);

await interaction.reply(
`Cleared ${(await cleared_connections).length} connections!`
);
}
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function start() {
// Log in with your bot token
await bot.login(process.env.BOT_TOKEN);
} else {
console.log("BOT_TOKEN NOT FOUND. Doesnt starting discord Bot.");
console.log("BOT_TOKEN NOT FOUND. Starting server only.");
}
startServer();

Expand Down
18 changes: 11 additions & 7 deletions src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,17 @@ app.get("/verify/:code", async (req, res) => {
res.status(500).send("Error: Community not configured");
return;
}

verifiedService
.createConnection(user, discordUser.user)
.then((x) => {
console.log("Created connection", x);
})
.catch((x) => console.log("Error creating connection", x));
try {
await verifiedService.createConnection(user, discordUser.user);
console.log(
`Connection created for ${user.person_view.person.name} and ${discordUser.user.username} in ${discordUser.guild.name}!`
);
} catch (e) {
console.log(
`Error creating connection for ${user.person_view.person.name} and ${discordUser.user.username} in ${discordUser.guild.name}!`
);
console.error(e);
}

res.send("Successfully authenticated! You can close this page now!");
} catch (e) {
Expand Down
50 changes: 35 additions & 15 deletions src/services/verifiedUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class verifiedUserService {
try {
const discordUser = user.discordUser;
if (!discordUser) {
console.log(
console.error(
`User ${
user.discordUser.username || user.discordUser.id
} not found!`
Expand Down Expand Up @@ -91,27 +91,22 @@ class verifiedUserService {
await member.roles.remove(role);
}
})
.catch((e) => {
console.log("User not found! " + user.discordUser.id);
console.log(e);
.catch(async (e) => {
if(e instanceof DiscordAPIError){
console.log("Discord API Error - Quarantine Role");
}else{
console.error(e);
}
});
await sleep(1000);
});

user.discordUser = discordUser;
user.lemmyUser = lemmyUser.person_view.person;
await this.repository.save(user);
console.log(
`Updated ${user.discordUser.username || user.discordUser.id}`
);
} catch (e) {
if(e instanceof DiscordAPIError){
console.log("Discord API Error");
console.log(e);
if(e.code === 10007){
console.log("Removing user from database");
await this.removeConnection(undefined, undefined, user.discordUser);
}
console.log("Discord API Error - Quarantine Role");
}
console.log(e);
}
Expand Down Expand Up @@ -150,7 +145,7 @@ class verifiedUserService {
user = await guild?.members.fetch(discordUser.id);
} catch (e) {
console.log("Failed to fetch user from guild - applyRoles");
console.log(e);
return;
}
if (!user) return;
try {
Expand Down Expand Up @@ -312,7 +307,7 @@ class verifiedUserService {
await this.applyRoles(lemmyDetails, discordUser);
const conn = await this.getConnection(lemmyDetails.person_view.person);
if (conn) {
return;
return conn;
}
const createdConfig = this.repository.create();
const lemmyUser = lemmyDetails.person_view;
Expand Down Expand Up @@ -477,6 +472,31 @@ class verifiedUserService {
});
return purgedUsers;
}

async clearBrokenConnections(dryrun: boolean = false) {
// when a user leaves the server, we need to remove the connection

const users = await this.repository.findAll();
const purgedUsers: { user: User; reason: string }[] = [];

await asyncForEach(users, async (user) => {
try {
const member = await bot.users.fetch(user.discordUser.id);
if (!member) {
if (!dryrun) {
await this.removeConnection(user.id);
}
purgedUsers.push({
user: user.discordUser,
reason: "User is not visible anymore!",
});
}
} catch (e) {
console.log(e);
}
});
return purgedUsers;
}
}

export default verifiedUserService;

0 comments on commit 202b2d8

Please sign in to comment.