Skip to content

Commit

Permalink
fix: optimize updating data
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 16, 2024
1 parent 7f17b06 commit 53696cc
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,32 @@ export default (client: EClient): void => {
function convertDatabaseUser(db: Settings, guild: Guild) {
if (db.get(guild.id, "converted")) return;
const users = db.get(guild.id, "user");
if (!users) return;
if (!users) {
db.set(guild.id, true, "converted");
return;
}
const defaultChannel = db.get(guild.id, "managerId");
const privateChannel = db.get(guild.id, "privateChannel");
for (const [userId, userData] of Object.entries(users)) {
console.warn(`Converting ${userId} => ${JSON.stringify(userData)} in ${guild.name}`);
for (const index in userData) {
const data = userData[index];
if (!Array.isArray(data.messageId)) {
if (!privateChannel || !defaultChannel) {
console.error("Missing channel for the conversion");
//remove the user
let toUpdate = false;
if (data.isPrivate && privateChannel) {
data.messageId = [data.messageId, privateChannel];
toUpdate = true;
} else if (defaultChannel) {
toUpdate = true;
data.messageId = [data.messageId, defaultChannel];
}
if (toUpdate) db.set(guild.id, data, `user.${userId}.${index}`);
else {
console.warn(
`No channel to update for ${userId}/${data.charName} => Deleting it`
);
db.delete(guild.id, `user.${userId}.${index}`);
continue;
}
if (data.isPrivate && privateChannel)
data.messageId = [data.messageId, privateChannel];
else if (defaultChannel) data.messageId = [data.messageId, defaultChannel];
db.set(guild.id, data, `user.${userId}.${index}`);
}
}
}
Expand Down

0 comments on commit 53696cc

Please sign in to comment.