Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/schiltz3/db migrate #48

Merged
merged 81 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
e0dc1a4
Move utils to correct locations
schiltz3 Sep 13, 2022
76eee73
Add cleanCompSciString and remove functionality from cleanChannelString
schiltz3 Sep 13, 2022
25714bc
deepsource
schiltz3 Sep 13, 2022
324907b
Add migration command
schiltz3 Sep 14, 2022
ab7aeee
Update Schema and interfaces to reflect
schiltz3 Sep 14, 2022
f1e5bbd
remove test function
schiltz3 Sep 14, 2022
639dc66
Update roles and channels from migrated db
schiltz3 Sep 14, 2022
9fba8bf
Add Missing DUPE field
schiltz3 Sep 14, 2022
b3fb122
get dupe name
schiltz3 Sep 14, 2022
48c33bb
Update string cleaning
schiltz3 Sep 14, 2022
ff798f7
Swap from CODE to NAME
schiltz3 Sep 14, 2022
5a7cbaa
Apply auto formatting changes
schiltz3 Sep 15, 2022
4c8db8f
Remove export
schiltz3 Sep 16, 2022
b7d41e3
use temp variable for current course in loop
schiltz3 Sep 16, 2022
59bb8a7
Bugfix: replace continuous hunks of whitespace with a dash
schiltz3 Sep 16, 2022
5d2a6d3
remove multiple -'s
schiltz3 Sep 16, 2022
0837955
remove brackets that match singular items
schiltz3 Sep 16, 2022
d60803a
group contiguous characters
schiltz3 Sep 16, 2022
e243cf1
catch newlines
schiltz3 Sep 16, 2022
d78e189
use rate limiter
schiltz3 Sep 16, 2022
f2c39eb
Don't drop ROLE_NAME
schiltz3 Sep 16, 2022
4ca6a21
Create correct course name
schiltz3 Sep 16, 2022
e83e57d
limit names to 100 characters
schiltz3 Sep 16, 2022
363a6b1
Create channels with both title and info
schiltz3 Sep 16, 2022
1a3e707
Use limiter
schiltz3 Sep 16, 2022
cec4660
Add get Topic function
schiltz3 Sep 16, 2022
1c997ed
Deepsource
schiltz3 Sep 16, 2022
c148db8
Clean course and rewrite createRoles
schiltz3 Sep 16, 2022
3b1f9fe
Fix message
schiltz3 Sep 16, 2022
5820739
Fix Schema
schiltz3 Sep 16, 2022
e68ae43
use defered reply
schiltz3 Sep 16, 2022
7c07cb0
Only reply if the role is actually assigned
schiltz3 Sep 16, 2022
e491d49
Filter on ROLE_NAME rather than NAME
schiltz3 Sep 16, 2022
654d4a3
Merge branch 'staging' into feature/schiltz3/db-migrate
schiltz3 Sep 16, 2022
7c6981b
Revert "use defered reply"
schiltz3 Sep 16, 2022
2bb9fb9
revert
schiltz3 Dec 27, 2022
5037366
reword embed utils
schiltz3 Dec 27, 2022
cfe4100
renamed channel utils
schiltz3 Dec 27, 2022
7d924a1
renamed role utils
schiltz3 Dec 27, 2022
d1a0296
Apply auto formatting changes
schiltz3 Dec 27, 2022
10ce1e0
Auto stash before merge of "feature/schiltz3/db-migrate" and "origin/…
schiltz3 Dec 27, 2022
5f23810
Apply auto formatting changes
schiltz3 Dec 27, 2022
b3d5339
Merge branch 'staging' into feature/schiltz3/db-migrate
schiltz3 Dec 29, 2022
db672ee
Ingore specific dev commands not allowed on prod
schiltz3 Dec 29, 2022
e55af2b
fix merge issue
schiltz3 Dec 29, 2022
28a198d
Apply auto formatting changes
schiltz3 Dec 29, 2022
84f6b9f
Merge branch 'staging' into feature/schiltz3/db-migrate
schiltz3 Jan 5, 2023
4f8280c
Apply auto formatting changes
schiltz3 Jan 5, 2023
5fd5bcb
remove incorrect tests , fix broken ones, and add new tests to reflec…
schiltz3 Jan 5, 2023
89fe708
Apply auto formatting changes
schiltz3 Jan 5, 2023
cf09808
deepsource
schiltz3 Jan 5, 2023
6f82202
Deepsource
schiltz3 Jan 5, 2023
fd8e9e7
Merge branch 'staging' into feature/schiltz3/db-migrate
schiltz3 Jan 6, 2023
d103b7c
rename and add jsdoc
schiltz3 Jan 7, 2023
b45e2b5
Merge branch 'staging' into feature/schiltz3/db-migrate
schiltz3 Jan 9, 2023
aaf5b57
fix typo
schiltz3 Jan 10, 2023
2b7632e
fix getTopic
schiltz3 Jan 10, 2023
e7a660e
Add tests
schiltz3 Jan 10, 2023
e5f8073
move migrateDB to the new folder
schiltz3 Jan 10, 2023
3d35a4c
move embeds
schiltz3 Jan 13, 2023
ce46502
rework regex for cleaning strings
schiltz3 Jan 13, 2023
55d394a
allow to match contiguous special characters
schiltz3 Jan 14, 2023
5c7df0e
fix import
schiltz3 Jan 14, 2023
7d3cb66
keep tests in order
schiltz3 Jan 14, 2023
53faf2f
cleanRoleString tests
schiltz3 Jan 14, 2023
cb6ace2
Apply auto formatting changes
schiltz3 Jan 14, 2023
e3f9ac9
start mocking discord.js
schiltz3 Jan 14, 2023
e896a0d
remove mocks
schiltz3 Jan 15, 2023
1ab5f05
fix regex
schiltz3 Jan 15, 2023
0311a5b
add active flag
schiltz3 Jan 15, 2023
9b11092
deepsource
schiltz3 Jan 15, 2023
62fac09
document
schiltz3 Jan 15, 2023
dbc4c2b
fix spelling
schiltz3 Jan 15, 2023
6743ddb
prefer Channel ID's over channel names
schiltz3 Jan 15, 2023
1e11a67
use === instead of ==
schiltz3 Jan 15, 2023
3c866ab
replace playantares with antaresnetwork
schiltz3 Jan 15, 2023
48e8700
Clean up and document roles
schiltz3 Jan 17, 2023
7f0af11
add documentation
schiltz3 Jan 17, 2023
d6f0999
swap to using name instead of ROLE_NAME
schiltz3 Jan 17, 2023
54d4a64
fix name
schiltz3 Jan 17, 2023
606276e
more documentation
schiltz3 Jan 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
*.env
node_modules
unused-commands
d.py/
d.py/

# Ignore specific dev commands not allowed in production
commands/owner/deleteChannels.ts
commands/owner/deleteRoles.ts
138 changes: 129 additions & 9 deletions __tests__/utils/channels.tests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { cleanChannelString } from "../../src/utils/channels";
import { expect, describe, it } from "@jest/globals";
import {
cleanChannelString,
concatCategoryName,
moveChannel,
getCourseName,
getTopic,
} from "../../src/utils/channels";
import { expect, describe, it, beforeEach } from "@jest/globals";
import { Guild, GuildChannel } from "discord.js";
import { IClass } from "../../src/models/classModel";

//cleanChannelString

describe("cleanChannelString", () => {
it("should lowercase the string", () => {
expect(cleanChannelString("ABCD")).toEqual("abcd");
Expand All @@ -12,13 +19,14 @@ describe("cleanChannelString", () => {
expect(cleanChannelString("a!b@c#d$")).toEqual("abcd");
});

it('should replace "compsci " with "cs"', () => {
expect(cleanChannelString("compsci ")).toEqual("cs");
it("should replace whitespace with a hyphen", () => {
expect(cleanChannelString("a b c")).toEqual("a-b-c");
});

it("should replace spaces and parentheses with hyphens", () => {
expect(cleanChannelString("a b (c)")).toEqual("a-b-c");
it("should remove consecutive hyphens", () => {
expect(cleanChannelString("a--b--c")).toBe("a-b-c");
});

it("should return an empty string for an empty input", () => {
expect(cleanChannelString("")).toBe("");
});
Expand All @@ -27,7 +35,119 @@ describe("cleanChannelString", () => {
expect(cleanChannelString("!@#$%^&*")).toBe("");
});

it("should handle a mix of all transformations", () => {
expect(cleanChannelString("Compsci a%$ 123!")).toEqual("csa-123");
it("should truncate the string to 100 characters", () => {
expect(cleanChannelString("a".repeat(200))).toHaveLength(100);
});
});

describe("getTopic", () => {
const default_string = "This is a test";
let course: IClass = {
TITLE: default_string,
INFO: default_string,
id: default_string,
NAME: default_string,
DUPE: false,
ACTIVE: true,
ROLE_NAME: default_string,
ROLE_ID: default_string,
CHANNEL_ID: default_string,
};
const extra_characters = 3;
beforeEach(() => {
course = {
TITLE: default_string,
INFO: default_string,
id: default_string,
NAME: default_string,
DUPE: false,
ACTIVE: true,
ROLE_NAME: default_string,
ROLE_ID: default_string,
CHANNEL_ID: default_string,
};
});

it("should return a truncated string of the concatenated title and info", () => {
course.TITLE = "Introduction to Mathematics";
expect(getTopic(course)).toHaveLength(
course.TITLE.length + course.INFO.length + extra_characters
);
});

it("should not return a string longer than 1024 characters", () => {
course.TITLE = "a".repeat(600);
course.INFO = "b".repeat(600);
expect(getTopic(course)).toHaveLength(1024);
});

it("should return a string of length 1024 if TITLE is 1025 characters", () => {
course.TITLE = "a".repeat(1025);
course.INFO = "";
expect(getTopic(course)).toHaveLength(1024);
});

it("should return a string of length 1024 if INFO is 1025 characters", () => {
course.TITLE = "";
course.INFO = "b".repeat(1025);
expect(getTopic(course)).toHaveLength(1024);
});
});

// getCourseName
describe("getCourseName", () => {
const default_string = "This is a test";
it("should return the course name if DUPE is false", () => {
const course: IClass = {
id: default_string,
NAME: "Math 101",
TITLE: "Introduction to Mathematics",
INFO: default_string,
DUPE: false,
ACTIVE: true,
ROLE_NAME: default_string,
ROLE_ID: default_string,
CHANNEL_ID: default_string,
};
expect(getCourseName(course)).toBe("Math 101");
});

it("should return the concatenated course name and title if DUPE is true", () => {
const course: IClass = {
id: default_string,
NAME: "Math 101",
TITLE: "Introduction to Mathematics",
INFO: default_string,
DUPE: true,
ACTIVE: true,
ROLE_NAME: default_string,
ROLE_ID: default_string,
CHANNEL_ID: default_string,
};
expect(getCourseName(course)).toBe("Math 101-Introduction to Mathematics");
});
});

//moveChannel
// Can only test pure functionality
describe("moveChannel", () => {
it("should not move the channel if it is already in the specified category", async () => {
// Set up the test so that the channel is already in the specified category
const guild = {} as Guild;
const category_name = "abcd";
const channel = { parent: { name: category_name } } as GuildChannel;
// Ensure that the channel was not moved
expect(await moveChannel(guild, channel, category_name)).toBe(0);
});
});

//concatCategoryName
describe("concatCategoryName", () => {
it("should return the category name if the category number is 0", () => {
expect(concatCategoryName("abcd", 0)).toBe("abcd");
});

it("should return the category name and number if the category number is not 0", () => {
expect(concatCategoryName("abcd", 1)).toBe("abcd 1");
});
});
40 changes: 40 additions & 0 deletions __tests__/utils/roles.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect, describe, it } from "@jest/globals";
import { cleanRoleString } from "../../src/utils/roles";

//cleanRoleString
describe("cleanRoleString", () => {
it("should lowercase the string", () => {
expect(cleanRoleString("ABCD")).toEqual("abcd");
});

it("should remove special characters", () => {
expect(cleanRoleString("a!b@c#d$")).toEqual("abcd");
});

it("should remove newlines", () => {
expect(cleanRoleString("\na\n")).toEqual("a");
});

it("should replace whitespace with a hyphen", () => {
expect(cleanRoleString("a b c")).toEqual("a-b-c");
});

it("should remove consecutive hyphens", () => {
expect(cleanRoleString("a--b--c")).toBe("a-b-c");
});

it("should return an empty string for an empty input", () => {
expect(cleanRoleString("")).toBe("");
});

it("cleanRoleString should return an empty string for a string with only special characters", () => {
expect(cleanRoleString("!@#$%^&*")).toBe("");
});

it("should truncate the string to 100 characters", () => {
expect(cleanRoleString("a".repeat(200))).toHaveLength(100);
});
it("should be able to handle a comboination of everything", () => {
expect(cleanRoleString("a! -- b@c\n#d$e%")).toEqual("a-bcde");
});
});
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"homepage": "https://github.com/Antares-Network/CSSC-Bot#readme",
"dependencies": {
"axios": "^0.27.2",
"bottleneck": "^2.19.5",
"chalk": "4.1.2",
"discord.js": "^13.9.2",
"mongoose": "^6.6.0",
Expand Down
11 changes: 7 additions & 4 deletions src/commands/owner/csClassPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {
import chalk from "chalk";
import { ICommand } from "wokcommands";
import { classModel, IClass } from "../../models/classModel";
import { checkForRoles } from "../../utils/roles";
import { checkForRoles, cleanRoleString } from "../../utils/roles";
import { sleep } from "../../utils/sleep";
import { getCourseName } from "../../utils/channels";

// Splits any size list into lists of at most `max_list_len`
/** */
function split_list<T>(list: T[], max_list_len: number): T[][] {
const class_chunks: T[][] = [];
for (let i = 0; i < list.length; i += max_list_len) {
Expand All @@ -21,9 +23,10 @@ function split_list<T>(list: T[], max_list_len: number): T[][] {

// consumes a Class and returns Message Selec tOption data
function create_option_from_class(_class: IClass): MessageSelectOptionData {
const clean_name = cleanRoleString(getCourseName(_class));
return {
label: _class.CODE,
value: _class.CODE,
label: clean_name,
value: clean_name,
description: _class.TITLE,
};
}
Expand All @@ -49,7 +52,7 @@ export default {
return;
}

const classes = await classModel.find({}).sort({ CODE: 1 });
const classes = await classModel.find({}).sort({ NAME: 1 });
const class_chunks = split_list(classes, 25);

const rows: MessageActionRow[] = [];
Expand Down
Loading