-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from TogetherCrew/add-isBot-to-guildMember-db…
…-migration Add is bot to guild member db migration
- Loading branch information
Showing
19 changed files
with
131 additions
and
52 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
src/migrations/db/1695210587863-add-isgeneratedbyweebhook-to-rawinfo-schema.ts
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/migrations/db/1706110397838-add-isbot-to-guildmember-schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dotenv/config'; | ||
import { connectDB } from '../../database'; | ||
import isBotLogic from '../utils/isBotLogic'; | ||
import { DatabaseManager } from '@togethercrew.dev/db'; | ||
import { Client, GatewayIntentBits } from 'discord.js'; | ||
import config from '../../config'; | ||
const { Guilds, GuildMembers, GuildMessages, GuildPresences, DirectMessages } = GatewayIntentBits; | ||
|
||
|
||
export const up = async () => { | ||
const client = new Client({ | ||
intents: [Guilds, GuildMembers, GuildMessages, GuildPresences, DirectMessages], | ||
}); | ||
|
||
await client.login(config.discord.botToken); | ||
await connectDB(); | ||
const connection1 = await DatabaseManager.getInstance().getTenantDb('1023936505321881641'); | ||
const connection2 = await DatabaseManager.getInstance().getTenantDb('949124961187016764'); | ||
await isBotLogic(connection1, client, '1023936505321881641'); | ||
await isBotLogic(connection2, client, '949124961187016764'); | ||
}; | ||
|
||
export const down = async () => { | ||
// TODO: Implement rollback logic if needed | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Client, Snowflake } from 'discord.js'; | ||
import { GuildMember } from 'discord.js'; | ||
import { Connection } from 'mongoose'; | ||
import parentLogger from '../../config/logger'; | ||
import { guildMemberService } from '../../database/services'; | ||
import { IGuildMember, } from '@togethercrew.dev/db'; | ||
|
||
const logger = parentLogger.child({ module: 'Migration-isBot' }); | ||
|
||
/** | ||
* Iterates over a list of guild members and pushes extracted data from each guild member to an array. | ||
* @param {GuildMember[]} guildMembersArray - An array of guild members from which data is to be extracted. | ||
* @returns {Promise<IGuildMember[]>} - A promise that resolves to the updated array containing the extracted data. | ||
*/ | ||
function pushMembersToArray(arr: IGuildMember[], guildMembersArray: GuildMember[]): IGuildMember[] { | ||
for (const guildMember of guildMembersArray) { | ||
arr.push(guildMemberService.getNeededDateFromGuildMember(guildMember)); | ||
} | ||
return arr; | ||
} | ||
|
||
/** | ||
* | ||
* @param {Connection} connection - Mongoose connection object for the database. | ||
* @param {Snowflake} guildId - The identifier of the guild to extract information from. | ||
*/ | ||
export default async function isBotLogic(connection: Connection, client: Client, guildId: Snowflake) { | ||
logger.info({ guild_id: guildId }, 'add-isBot-to-guilbMember-schema migration is running'); | ||
try { | ||
const botGuildMembers = []; | ||
const noneBotGuildMembers = []; | ||
|
||
const guild = await client.guilds.fetch(guildId); | ||
const membersToStore: IGuildMember[] = []; | ||
const fetchedMembers = await guild.members.fetch(); | ||
const guildMembers = pushMembersToArray(membersToStore, [...fetchedMembers.values()]); | ||
|
||
console.log(guildMembers.length, guildId) | ||
if (guildMembers) { | ||
for (const guildMember of guildMembers) { | ||
if (guildMember.isBot) { | ||
botGuildMembers.push(guildMember.discordId); | ||
} else { | ||
noneBotGuildMembers.push(guildMember.discordId); | ||
} | ||
} | ||
} | ||
|
||
if (botGuildMembers.length > 0) { | ||
await guildMemberService.updateGuildMembers( | ||
connection, | ||
{ discordId: { $in: botGuildMembers } }, | ||
{ isBot: true } | ||
); | ||
} | ||
|
||
if (noneBotGuildMembers.length > 0) { | ||
await guildMemberService.updateGuildMembers( | ||
connection, | ||
{ discordId: { $in: noneBotGuildMembers } }, | ||
{ isBot: false } | ||
); | ||
} | ||
|
||
const mergedArray = botGuildMembers.concat(noneBotGuildMembers); | ||
if (mergedArray.length > 0) { | ||
await guildMemberService.updateGuildMembers( | ||
connection, | ||
{ discordId: { $nin: mergedArray } }, | ||
{ isBot: null } | ||
); | ||
} | ||
|
||
} catch (err) { | ||
logger.error({ guild_id: guildId, err }, 'add-isBot-to-guilbMember-schema migration is failed'); | ||
} | ||
logger.info({ guild_id: guildId }, 'add-isBot-to-guilbMember-schema migration is done'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters