Skip to content

Commit

Permalink
Bugfix/schiltz3/deepsource (#28)
Browse files Browse the repository at this point in the history
# Fix all deepsource issues

- Replace any with generics
- Move non-export functions to before use
- Remove unused variables
- Add missing `await`s
- Correctly type `resolve` in `sleep`
- Remove non-null assertion postfixes for strict null checking
  • Loading branch information
schiltz3 authored Sep 6, 2022
2 parents 1f391bf + bdc1a1c commit 718c5f7
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 52 deletions.
52 changes: 27 additions & 25 deletions commands/owner/csClassPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ import chalk from "chalk";
import { ICommand } from "wokcommands";
import { classModel, IClass } from "../../models/classModel";
import { checkForRoles } from "../../rolesOps";
import { sleep } from "../../util";

// 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) {
class_chunks.push(list.slice(i, i + max_list_len));
}
return class_chunks;
}

// consumes a Class and returns Message Selec tOption data
function create_option_from_class(_class: IClass): MessageSelectOptionData {
return {
label: _class.CODE,
value: _class.CODE,
description: _class.TITLE,
};
}
export default {
name: "csClassPoll",
category: "owner",
Expand All @@ -20,7 +38,11 @@ export default {
ownerOnly: true,

callback: async ({ client, interaction: msgInt }) => {
if (!checkForRoles(msgInt.guild!)) {
if (msgInt.guild === null) {
console.log(chalk.red("No guild"));
return;
}
if (!(await checkForRoles(msgInt.guild))) {
msgInt.reply(
"Please run the `/ createRoles` command in this server to create the necessary roles for this poll!"
);
Expand All @@ -30,7 +52,7 @@ export default {
const classes = await classModel.find({}).sort({ CODE: 1 });
const class_chunks = split_list(classes, 25);

let rows: MessageActionRow[] = [];
const rows: MessageActionRow[] = [];
for (let index = 0; index < class_chunks.length; index++) {
const menu = new MessageSelectMenu();
menu.setCustomId(`csClassPoll+${index}`);
Expand Down Expand Up @@ -63,12 +85,10 @@ export default {

msgInt.reply({ embeds: [infoEmbed], components: row_chunks[index] });
} else {
msgInt.channel!.send({ components: row_chunks[index] });
msgInt.reply({ components: row_chunks[index] });
}
// await on a new promise that resolves itself after a delay of 200 ms
await new Promise((resolve) => {
setTimeout(resolve, 200);
});

await sleep(200);
}

// Log the command usage
Expand All @@ -83,21 +103,3 @@ export default {
);
},
} as ICommand;

// Splits any size list into lists of at most `max_list_len`
function split_list(list: Array<any>, max_list_len: number) {
let class_chunks = [];
for (let i = 0; i < list.length; i += max_list_len) {
class_chunks.push(list.slice(i, i + max_list_len));
}
return class_chunks;
}

// consumes a Class and returns Message Selec tOption data
function create_option_from_class(_class: IClass): MessageSelectOptionData {
return {
label: _class.CODE,
value: _class.CODE,
description: _class.TITLE,
};
}
6 changes: 5 additions & 1 deletion commands/owner/staffPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ export default {
)
);

if (msgInt.guild === null) {
console.log(chalk.red("No guild"));
return;
}
// Send the embed and message component rows
if (!checkForRoles(msgInt.guild!)) {
if (!(await checkForRoles(msgInt.guild))) {
msgInt.reply(
"Please run the `/createRoles` command in this server to create the necessary roles for this poll!"
);
Expand Down
10 changes: 7 additions & 3 deletions commands/owner/yearPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ export default {
)
);

if (msgInt.guild === null) {
console.log(chalk.red("No guild"));
return;
}
// Send the embed and message component rows
if (!checkForRoles(msgInt.guild!)) {
msgInt.reply({
if (!(await checkForRoles(msgInt.guild))) {
await msgInt.reply({
content:
"Please run the /createRoles command in this server to create the necessary roles for this poll!",
ephemeral: true,
});
} else {
msgInt.reply({ embeds: [infoEmbed], components: [row] });
await msgInt.reply({ embeds: [infoEmbed], components: [row] });
}

// Log the command usage
Expand Down
2 changes: 1 addition & 1 deletion commands/user/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {
.setDescription(description)
.setFooter({ text: footer, iconURL: footerIcon });

interaction.reply({ embeds: [Embed] });
await interaction.reply({ embeds: [Embed] });

// Log the command usage
console.log(
Expand Down
2 changes: 1 addition & 1 deletion commands/user/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default {
.setDescription(description)
.addFields(fields)
.setFooter({ text: footer, iconURL: footerIcon });
interaction.reply({ embeds: [Embed] });
await interaction.reply({ embeds: [Embed] });

// Log the command usage
console.log(
Expand Down
2 changes: 1 addition & 1 deletion commands/user/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
.setFooter({ text: footer, iconURL: footerIcon });

// Return the embed
interaction.reply({ embeds: [Embed] });
await interaction.reply({ embeds: [Embed] });

// Log the command usage
console.log(
Expand Down
2 changes: 1 addition & 1 deletion commands/user/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {
.setDescription(description)
.setFooter({ text: footer, iconURL: footerIcon });

interaction.reply({ embeds: [Embed] });
await interaction.reply({ embeds: [Embed] });

// Log the command usage
console.log(
Expand Down
10 changes: 3 additions & 7 deletions commands/user/uptime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from "chalk";
import { MessageEmbed, TextChannel } from "discord.js";
import { MessageEmbed } from "discord.js";
import { ICommand } from "wokcommands";

export default {
Expand All @@ -12,12 +12,8 @@ export default {
requiredPermissions: ["SEND_MESSAGES"],

callback: async ({ client, interaction }) => {
// Command information
const id = interaction.user.id;
const chan = interaction.channel as TextChannel;

// Computed values
const time = client.uptime!;
const time = client.uptime !== null ? client.uptime : 0;
const days = Math.floor(time / 86400000);
const hours = Math.floor(time / 3600000) % 24;
const minutes = Math.floor(time / 60000) % 60;
Expand All @@ -38,7 +34,7 @@ export default {
.setFooter({ text: footer, iconURL: footerIcon });

// Return the embed
interaction.reply({ embeds: [Embed] });
await interaction.reply({ embeds: [Embed] });

// Log the command usage
console.log(
Expand Down
2 changes: 1 addition & 1 deletion commands/user/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
.setFooter({ text: footer, iconURL: footerIcon });

// Return the embed
interaction.reply({ embeds: [Embed] });
await interaction.reply({ embeds: [Embed] });

// Log the command usage
console.log(
Expand Down
2 changes: 1 addition & 1 deletion features/status-changer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client } from "discord.js";

export default async (client: Client) => {
export default (client: Client) => {
const statusOptions = [
`/help | Ping: ${client.ws.ping}ms`,
`V.${process.env.VERSION}`,
Expand Down
2 changes: 1 addition & 1 deletion features/statuspage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Client } from "discord.js";
export default (client: Client): void => {
const updateStatus = async () => {
// This function is called every 1 minutes and pings the network status page for uptime monitoring
axios.get(
await axios.get(
`https://${process.env.UPTIME_KUMA_MONITOR_DOMAIN}/api/push/${process.env.UPTIME_KUMA_MONITOR_ID}?msg=OK&ping=${client.ws.ping}`
);
setTimeout(updateStatus, 1000 * 60);
Expand Down
11 changes: 7 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ const client = new DiscordJs.Client({
],
});

client.on("ready", () => {
client.on("ready", async () => {
if (client.user) {
console.log(chalk.green(`Logged in as ${client.user.tag}!`));
console.log(
chalk.yellow.bold(`I am running version: ${process.env.VERSION}`)
);
// Check to make sure the roles exist in all servers
console.log("Checking if all roles exist in servers.");
client.guilds.cache.forEach(async (guild) => {
checkForRoles(guild);
});

await Promise.all(
client.guilds.cache.map((guild) => {
checkForRoles(guild);
})
);
}

const dbOptions = {
Expand Down
11 changes: 6 additions & 5 deletions rolesOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export async function checkIfCollectionsExist<T>(model: Model<T>) {
}
}
async function dbQuery() {
const classes = await classModel.find({});
const staff = await staffModel.find({});
const years = await yearModel.find({});
return [classes, staff, years];
return await Promise.all([
classModel.find({}),
staffModel.find({}),
yearModel.find({}),
]);
}

export async function getUsersRoles(member: GuildMember): Promise<string> {
Expand Down Expand Up @@ -120,7 +121,7 @@ export async function checkForRoles(guild: Guild): Promise<boolean> {
// Check if all roles exist in a guild
// Return true if they do, false if they don't

let collection: boolean[] = [];
const collection: boolean[] = [];

for (const group of await dbQuery()) {
for (const element of group) {
Expand Down
6 changes: 6 additions & 0 deletions util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function sleep(ms: number) {
// Create new promise that resolves itself after a delay of <ms>
return new Promise((resolve: (args: void) => void) =>
setTimeout(resolve, ms)
);
}

0 comments on commit 718c5f7

Please sign in to comment.