Skip to content

Commit

Permalink
Merge pull request #2 from Yumat10/jest
Browse files Browse the repository at this point in the history
Intitial jest tests + src folder structure
  • Loading branch information
Yumat10 authored Jul 9, 2022
2 parents d274574 + 0867389 commit 51f62af
Show file tree
Hide file tree
Showing 16 changed files with 14,545 additions and 6,778 deletions.
File renamed without changes.
8 changes: 8 additions & 0 deletions config/configType.ts
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.
8 changes: 8 additions & 0 deletions jest.config.js
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',
},
},
}
7 changes: 5 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
import { MongoClient, Collection, Document } from 'mongodb'
import cron from 'cron'

import devConfig from './config_dev.json'
import prodConfig from './config.json'
import devConfig from './config/config_dev.json'
import prodConfig from './config/config.json'
require('dotenv').config()

let config = devConfig
Expand Down Expand Up @@ -389,6 +389,9 @@ client.on('messageCreate', async (message) => {
message.channel.send('/pause => pause bot')
message.channel.send('/resume or /start => resume or start bot')
message.channel.send('/alive => check if bot is still alive')
message.channel.send(
'/nextDate => get date of the next round of matching'
)
}

if (command === 'status') {
Expand Down
18,739 changes: 12,000 additions & 6,739 deletions package-lock.json

Large diffs are not rendered by default.

54 changes: 29 additions & 25 deletions package.json
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"
}
}
88 changes: 88 additions & 0 deletions src/discord/matchingChannels.ts
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()
}
}
41 changes: 41 additions & 0 deletions src/discord/participatingUserIDs.ts
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
}
Loading

0 comments on commit 51f62af

Please sign in to comment.