forked from somaniarushi/outliers-donuts
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from Yumat10/jest
Intitial jest tests + src folder structure
- Loading branch information
Showing
16 changed files
with
14,545 additions
and
6,778 deletions.
There are no files selected for viewing
File renamed without changes.
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,8 @@ | ||
export type Config = { | ||
prefix: string | ||
botCommunicationChannelID: string | ||
guildID: string | ||
matchingChannelName: string | ||
matchingCategoryName: string | ||
blackList: string[] | ||
} |
File renamed without changes.
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,8 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: './tsconfig.json', | ||
}, | ||
}, | ||
} |
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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
{ | ||
"name": "donut-bot", | ||
"version": "1.0.0", | ||
"main": "index.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "node ./main.ts", | ||
"debug": "nodemon ./main.ts", | ||
"lint": "eslint . --ext .ts" | ||
}, | ||
"dependencies": { | ||
"better-sqlite3": "^7.5.3", | ||
"cron": "^2.0.0", | ||
"discord.js": "^13.8.0", | ||
"dotenv": "^16.0.1", | ||
"mongodb": "^4.7.0", | ||
"node-gyp": "^9.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/cron": "^2.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.30.5", | ||
"@typescript-eslint/parser": "^5.30.5", | ||
"eslint": "^8.19.0", | ||
"nodemon": "^2.0.16", | ||
"typescript": "^4.7.4" | ||
} | ||
"name": "donut-bot", | ||
"version": "1.0.0", | ||
"main": "index.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "node ./main.ts", | ||
"debug": "nodemon ./main.ts", | ||
"lint": "eslint . --ext .ts", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"better-sqlite3": "^7.5.3", | ||
"cron": "^2.0.0", | ||
"discord.js": "^13.8.0", | ||
"dotenv": "^16.0.1", | ||
"mongodb": "^4.7.0", | ||
"node-gyp": "^9.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/cron": "^2.0.0", | ||
"@types/jest": "^28.1.4", | ||
"@typescript-eslint/eslint-plugin": "^5.30.5", | ||
"@typescript-eslint/parser": "^5.30.5", | ||
"eslint": "^8.19.0", | ||
"jest": "^28.1.2", | ||
"nodemon": "^2.0.16", | ||
"ts-jest": "^28.0.5", | ||
"typescript": "^4.7.4" | ||
} | ||
} |
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,88 @@ | ||
import { Guild, OverwriteResolvable, Permissions } from 'discord.js' | ||
import { Config } from '../../config/configType' | ||
|
||
/** | ||
* Create private channels with the paired users | ||
* | ||
* @param {[string[]]} userIDGroups Array of grouped User ID's of the form [[user_1_ID, user_2_ID], [user_3_ID, user_4_ID], ...] | ||
* @returns {Promise<void>} | ||
*/ | ||
type createPrivateChannelsArgs = { | ||
userIDGroups: [string[]] | ||
guild: Guild | ||
config: Config | ||
interval: number | ||
} | ||
export async function createPrivateChannels({ | ||
guild, | ||
config, | ||
userIDGroups, | ||
interval, | ||
}: createPrivateChannelsArgs): Promise<void> { | ||
if (!guild) return | ||
// Get the category to place the channel under | ||
const channelCategory = guild.channels.cache.find( | ||
(c) => | ||
c.type === 'GUILD_CATEGORY' && | ||
c.name === config.matchingCategoryName | ||
) | ||
|
||
if (!channelCategory) throw Error('Matching category not found in Guild') | ||
|
||
// Iterate over userID pairings and create DM group | ||
for (const userIDPair of userIDGroups) { | ||
// Construct permission overwrite for each user in the pair | ||
const userPermissionOverWrites: OverwriteResolvable[] = userIDPair.map( | ||
(userID) => { | ||
return { | ||
type: 'member', | ||
id: userID, | ||
allow: Permissions.ALL, | ||
} | ||
} | ||
) | ||
// Create private channel | ||
const channel = await guild.channels.create( | ||
config.matchingChannelName, | ||
{ | ||
parent: channelCategory.id, | ||
permissionOverwrites: [ | ||
{ | ||
id: guild.roles.everyone, | ||
deny: Permissions.ALL, | ||
}, | ||
// Add the overwrites for the pair of users | ||
...userPermissionOverWrites, | ||
], | ||
} | ||
) | ||
const userIDTag = userIDPair.map((userID) => `<@${userID}>`).join(' ') | ||
channel.send(`Hey ${userIDTag} 👋, | ||
You have been matched! | ||
Schedule a call, go for a walk or do whatever else. | ||
The channel will automatically be closed after ${interval} days. | ||
`) | ||
} | ||
} | ||
|
||
/** | ||
* Helper function to delete all private channels | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
type deleteMatchingChannelsArgs = { | ||
guild: Guild | ||
config: Config | ||
} | ||
export async function deleteMatchingChannels({ | ||
guild, | ||
config, | ||
}: deleteMatchingChannelsArgs): Promise<void> { | ||
const matchingChannels = guild.channels.cache.filter( | ||
(channel) => channel.name === config.matchingChannelName | ||
) | ||
const matchingChannelsArray = Array.from(matchingChannels.values()) | ||
for (let i = 0; i < matchingChannelsArray.length; i++) { | ||
await matchingChannelsArray[i].delete() | ||
} | ||
} |
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,41 @@ | ||
import { Guild } from 'discord.js' | ||
import { Config } from '../../config/configType' | ||
|
||
type getParticipatingUserIDsArgs = { | ||
guild: Guild | ||
roles: string[] | ||
config: Config | ||
} | ||
|
||
export async function getParticipatingUserIDs({ | ||
guild, | ||
roles, | ||
config, | ||
}: getParticipatingUserIDsArgs): Promise<Set<string>> { | ||
if (!guild) return | ||
try { | ||
await guild.members.fetch() | ||
const participatingUserIDs: Set<string> = new Set() | ||
console.log('Roles', roles) | ||
for (const roleName of roles) { | ||
const Role = guild.roles.cache.find((role) => role.name == roleName) | ||
const usersWithRole: string[] = guild.roles.cache | ||
.get(Role.id || '') | ||
.members.filter((m) => { | ||
if (!config.blackList.includes(m.user.id)) { | ||
return false | ||
} | ||
return true | ||
}) | ||
.map((m) => m.user.id) | ||
for (const user of usersWithRole) { | ||
participatingUserIDs.add(user) | ||
} | ||
} | ||
|
||
return participatingUserIDs | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
return | ||
} |
Oops, something went wrong.