Skip to content

Commit 6c10137

Browse files
authored
Bugfix/schiltz3/deepsource (#88)
# Attempt to fix all open deepsource problems ## Needs testing Fixed various discouraged patterns in code. The code worked but was considered bad practice # Changes * Remove unnecessary escape characters * Ignore issue: no return from map * Use object literal shorthand when returning dummy text channel * Avoid improper shadowing * use === instead of == * Remove unused arg * Update tests to correspond to this branch's function. One should still fail
2 parents 6606b40 + 545b98c commit 6c10137

File tree

6 files changed

+63
-56
lines changed

6 files changed

+63
-56
lines changed

__tests__/utils/channels.tests.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import { cleanChannelString } from "../../utils/channels";
2-
import { test, expect, describe, it } from "@jest/globals";
3-
4-
//cleanChannelString
5-
6-
describe("cleanChannelString", () => {
7-
it("should lowercase the string", () => {
8-
expect(cleanChannelString("ABCD")).toEqual("abcd");
9-
});
10-
11-
it("should remove special characters", () => {
12-
expect(cleanChannelString("a!b@c#d$")).toEqual("abcd");
13-
});
14-
15-
it('should replace "compsci " with "cs"', () => {
16-
expect(cleanChannelString("compsci")).toEqual("cs");
17-
});
18-
19-
it("should replace spaces and parentheses with hyphens", () => {
20-
expect(cleanChannelString("a b (c)")).toEqual("a-b-c");
21-
});
22-
it("should return an empty string for an empty input", () => {
23-
expect(cleanChannelString("")).toBe("");
24-
});
25-
26-
it("cleanChannelString should return an empty string for a string with only special characters", () => {
27-
expect(cleanChannelString("!@#$%^&*")).toBe("");
28-
});
29-
30-
it("should handle a mix of all transformations", () => {
31-
expect(cleanChannelString("Compsci 123!")).toEqual("cs-123");
32-
});
33-
});
1+
import { cleanChannelString } from "../../utils/channels";
2+
import { expect, describe, it } from "@jest/globals";
3+
4+
//cleanChannelString
5+
6+
describe("cleanChannelString", () => {
7+
it("should lowercase the string", () => {
8+
expect(cleanChannelString("ABCD")).toEqual("abcd");
9+
});
10+
11+
it("should remove special characters", () => {
12+
expect(cleanChannelString("a!b@c#d$")).toEqual("abcd");
13+
});
14+
15+
it('should replace "compsci " with "cs"', () => {
16+
expect(cleanChannelString("compsci ")).toEqual("cs");
17+
});
18+
19+
it("should replace spaces and parentheses with hyphens", () => {
20+
expect(cleanChannelString("a b (c)")).toEqual("a-b-c");
21+
});
22+
it("should return an empty string for an empty input", () => {
23+
expect(cleanChannelString("")).toBe("");
24+
});
25+
26+
it("cleanChannelString should return an empty string for a string with only special characters", () => {
27+
expect(cleanChannelString("!@#$%^&*")).toBe("");
28+
});
29+
30+
it("should handle a mix of all transformations", () => {
31+
expect(cleanChannelString("Compsci a%$ 123!")).toEqual("csa-123");
32+
});
33+
});

commands/owner/createRoles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"],
1616
ownerOnly: true,
1717

18-
callback: async ({ client, interaction: msgInt }) => {
18+
callback: async ({ interaction: msgInt }) => {
1919
console.log(chalk.green("Creating roles..."));
2020
console.log(
2121
chalk.red("------------------------------------------------------")

commands/owner/csCreateChannels.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function create_default_embed(
3636
function cleanRoleString(role_name: string): string {
3737
const clean_role_name: string = role_name
3838
.toLowerCase()
39-
.replace(/[`~!@#$%^&*))|+=?;:'",.<>\{\}\[\]\\\/]/gi, "")
39+
.replace(/[`~!@#$%^&*))|+=?;:'",.<>{}[\]\\/]/gi, "")
4040
.replace(/[ (]/gi, "-");
4141
return clean_role_name;
4242
}
@@ -163,15 +163,17 @@ export default {
163163
courses[index].save();
164164

165165
// Ping members who have this role
166-
const role = msgInt.guild.roles.cache.find((role) => {
166+
const found_role = msgInt.guild.roles.cache.find((role) => {
167167
return (
168168
cleanRoleString(role.name) ==
169169
cleanChannelString(courses[index].CODE)
170170
);
171171
});
172-
if (role !== undefined) {
172+
if (found_role !== undefined) {
173173
//Ping member
174-
new_channel.send(`Hey! <@&${role.id}> here is a channel for you!`);
174+
new_channel.send(
175+
`Hey! <@&${found_role.id}> here is a channel for you!`
176+
);
175177
}
176178
} else if (
177179
channel.parent !== null &&
@@ -194,15 +196,17 @@ export default {
194196
courses[index].CHANNEL_ID = channel.id;
195197
courses[index].save();
196198
// Ping members who have this role
197-
const role = msgInt.guild.roles.cache.find((role) => {
199+
const found_role = msgInt.guild.roles.cache.find((role) => {
198200
return (
199201
cleanRoleString(role.name) ==
200202
cleanChannelString(courses[index].CODE)
201203
);
202204
});
203-
if (role !== undefined) {
205+
if (found_role !== undefined) {
204206
//Ping member
205-
channel.send(`Hey! <@&${role.id}> here is the channel for you!`);
207+
channel.send(
208+
`Hey! <@&${found_role.id}> here is the channel for you!`
209+
);
206210
}
207211
} else if (
208212
channel.parent !== null &&

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ client.on("ready", async () => {
3838

3939
await Promise.all(
4040
client.guilds.cache.map((guild) => {
41-
checkForRoles(guild);
41+
checkForRoles(guild); // skipcq JS-0042
4242
})
4343
);
4444
}

utils/channels.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import chalk from "chalk";
44
export function cleanChannelString(s: string): string {
55
return s
66
.toLowerCase()
7-
.replace(/[`~!@#$%^&*))|+=?;:'",.<>\{\}\[\]\\\/]/gi, "")
7+
.replace(/[`~!@#$%^&*))|+=?;:'",.<>{}[\]\\/]/gi, "")
88
.replace("compsci ", "cs")
99
.replace(/[ (]/gi, "-");
1010
}
@@ -16,17 +16,20 @@ export function checkForChannel(guild: Guild, channel_name: string) {
1616
}
1717

1818
export async function findCategory(guild: Guild, category_name: string) {
19-
let category = guild.channels.cache.find((category) => {
20-
return category.name == category_name;
19+
let found_category = guild.channels.cache.find((category) => {
20+
return category.name === category_name;
2121
});
2222

23-
if (category === undefined || category.type !== "GUILD_CATEGORY") {
24-
category = await guild.channels.create(category_name, {
23+
if (
24+
found_category === undefined ||
25+
found_category.type !== "GUILD_CATEGORY"
26+
) {
27+
found_category = await guild.channels.create(category_name, {
2528
type: "GUILD_CATEGORY",
2629
});
2730
}
2831

29-
return category;
32+
return found_category;
3033
}
3134
export async function createTextChannel(
3235
guild: Guild,
@@ -36,11 +39,11 @@ export async function createTextChannel(
3639
category_name?: string
3740
) {
3841
//Determine which arg to use
39-
let channel_parent: CategoryChannel | undefined;
42+
let parent: CategoryChannel | undefined;
4043
if (category !== undefined) {
41-
channel_parent = category;
44+
parent = category;
4245
} else if (category_name !== undefined) {
43-
channel_parent = await findCategory(guild, category_name);
46+
parent = await findCategory(guild, category_name);
4447
} else {
4548
throw Error(
4649
"Must specify either channel_category or channel_category_name"
@@ -49,8 +52,8 @@ export async function createTextChannel(
4952

5053
return guild.channels.create(cleanChannelString(name), {
5154
type: "GUILD_TEXT",
52-
topic: topic,
53-
parent: channel_parent,
55+
topic,
56+
parent,
5457
});
5558
}
5659

@@ -62,7 +65,7 @@ export async function moveChannel(
6265
if (
6366
channel.parent === null ||
6467
channel.parent === undefined ||
65-
channel.parent?.name != category_name
68+
channel.parent?.name !== category_name
6669
) {
6770
const category = await findCategory(guild, category_name);
6871
channel.setParent(category);
@@ -81,7 +84,7 @@ export function concatCategoryName(
8184
category_name: string,
8285
category_number: number
8386
) {
84-
return category_number == 0
87+
return category_number === 0
8588
? category_name
8689
: `${category_name} ${category_number}`;
8790
}

utils/roles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export async function checkForRoles(guild: Guild): Promise<boolean> {
128128
const name = guild.roles.cache.find((r) => r.name === element.ROLE_NAME);
129129
const id = guild.roles.cache.find((r) => r.id === element.ROLE_ID);
130130

131-
if (name == undefined && id == undefined) {
131+
if (name === undefined && id === undefined) {
132132
console.log(
133133
chalk.red.bold(
134134
`Role ${element.ROLE_NAME} does not exist in ${guild.name}, Please run the /createRoles command in that server.`

0 commit comments

Comments
 (0)