Skip to content

Commit f484081

Browse files
Implement eslint
1 parent 53e262b commit f484081

File tree

6 files changed

+334
-322
lines changed

6 files changed

+334
-322
lines changed

src/bot.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ import { MyContext } from "./interfaces";
44
import { loadCommands, commandHandler } from "./handlers/CommandHandler";
55

66
(async function () {
7-
const context: MyContext = {
8-
client: new Client({
9-
intents: ["GUILDS"],
10-
presence: {
11-
activities: [{ type: "WATCHING", name: "Discord.JS channel" }],
12-
status: "online",
13-
},
14-
// For DMs, now on dms a partial channel is received
15-
partials: ["CHANNEL"],
16-
}),
17-
commands: new Collection(),
18-
cooldownCounter: new Collection(),
19-
};
20-
const docsBot = context.client;
21-
await loadCommands(context);
22-
docsBot.on("error", console.error);
23-
docsBot.on("warn", console.warn);
24-
docsBot.on("ready", (client) => {
25-
console.log(`Logged in as ${client.user.tag} (${client.user.id})`);
26-
});
27-
docsBot.on("interactionCreate", commandHandler.bind(null, context));
7+
const context: MyContext = {
8+
client: new Client({
9+
intents: ["GUILDS"],
10+
presence: {
11+
activities: [{ type: "WATCHING", name: "Discord.JS channel" }],
12+
status: "online",
13+
},
14+
// For DMs, now on dms a partial channel is received
15+
partials: ["CHANNEL"],
16+
}),
17+
commands: new Collection(),
18+
cooldownCounter: new Collection(),
19+
};
20+
const docsBot = context.client;
21+
await loadCommands(context);
22+
docsBot.on("error", console.error);
23+
docsBot.on("warn", console.warn);
24+
docsBot.on("ready", (client) => {
25+
console.info(`Logged in as ${client.user.tag} (${client.user.id})`);
26+
});
27+
docsBot.on("interactionCreate", commandHandler.bind(null, context));
2828

29-
docsBot.login(process.env.TOKEN);
29+
docsBot.login(process.env.TOKEN);
3030
})();

src/commands/docs/djs.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,61 @@ import { SlashCommandBuilder } from "@discordjs/builders";
33
import Doc, { sources } from "discord.js-docs";
44
import { checkEmbedLimits } from "../../utils/embedUtils";
55

6-
const supportedBranches = Object.keys(sources).map((branch) => [capitalize(branch), branch] as [string, string]);
6+
const supportedBranches = Object.keys(sources).map(
7+
(branch) => [capitalize(branch), branch] as [string, string]
8+
);
79

810
const command: Command = {
911
data: new SlashCommandBuilder()
1012
.setName("djs")
11-
.setDescription("Search discord.js documentations, supports builders, voice, collection and rpc documentations")
13+
.setDescription(
14+
"Search discord.js documentations, supports builders, voice, collection and rpc documentations"
15+
)
1216
.addStringOption((option) =>
1317
option
1418
.setName("query")
15-
.setDescription("Enter the phrase you'd like to search for, e.g: client#isready")
16-
.setRequired(true),
19+
.setDescription(
20+
"Enter the phrase you'd like to search for, e.g: client#isready"
21+
)
22+
.setRequired( true )
1723
)
1824
.addStringOption((option) =>
1925
option
2026
.setName("source")
21-
.setDescription("Select which branch/repository to get documentation off of")
27+
.setDescription(
28+
"Select which branch/repository to get documentation off of"
29+
)
2230
.addChoices(supportedBranches)
23-
.setRequired(false),
31+
.setRequired( false )
2432
),
25-
async execute(interaction, context) {
33+
async execute(interaction) {
2634
const query = interaction.options.getString("query");
2735
// The Default source should be stable
2836
const source: keyof typeof sources =
29-
(interaction.options.getString("source") as keyof typeof sources) || "stable";
37+
( interaction.options.getString( "source" ) as keyof typeof sources ) ||
38+
"stable";
3039

3140
const doc = await Doc.fetch(source, { force: true });
3241

3342
const resultEmbed = doc.resolveEmbed(query);
34-
43+
3544
const notFoundEmbed = doc.baseEmbed();
3645
notFoundEmbed.description = "Didn't find any results for that query";
37-
if (!resultEmbed || (resultEmbed.description === "")) {
46+
if ( !resultEmbed || resultEmbed.description === "" ) {
47+
3848
const timeStampDate = new Date(notFoundEmbed.timestamp);
3949
const embedObj = { ...notFoundEmbed, timestamp: timeStampDate };
50+
4051
interaction.editReply({ embeds: [embedObj] }).catch(console.error);
41-
return
52+
return;
4253
}
4354

4455
const timeStampDate = new Date(resultEmbed.timestamp);
45-
let embedObj = {...resultEmbed, timestamp: timeStampDate};
46-
if(!checkEmbedLimits([resultEmbed])) {
56+
const embedObj = { ...resultEmbed, timestamp: timeStampDate };
57+
58+
if (!checkEmbedLimits([resultEmbed])) {
4759
// The final fields should be the View Source button
48-
embedObj.fields = [embedObj.fields?.at(-1)]
60+
embedObj.fields = [embedObj.fields?.at( -1 )];
4961
}
5062
interaction.editReply({ embeds: [embedObj] }).catch(console.error);
5163
return;

src/handlers/CommandHandler.ts

Lines changed: 110 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,51 @@ import type { CommandInteraction, Interaction } from "discord.js";
44
import { Permissions, Formatters } from "discord.js";
55

66
export async function commandHandler(
7-
context: MyContext,
8-
interaction: Interaction
7+
context: MyContext,
8+
interaction: Interaction
99
) {
10-
if (interaction.isCommand()) {
11-
await interaction.deferReply();
12-
const command = context.commands.get(interaction.commandName);
13-
if (!command) return interaction.editReply(`Command not found`);
10+
if (interaction.isCommand()) {
11+
await interaction.deferReply();
12+
const command = context.commands.get(interaction.commandName);
13+
if (!command) return interaction.editReply("Command not found");
1414

15-
if (commandPermissionCheck(interaction, command)) return;
16-
if (commandCooldownCheck(interaction, command, context)) return;
17-
try {
18-
await command.execute(interaction, context);
19-
} catch (e) {
20-
console.error(e);
21-
const errorMessage = "An error has occurred";
22-
interaction.editReply(errorMessage).catch(console.error);
15+
if (commandPermissionCheck(interaction, command)) return;
16+
if (commandCooldownCheck(interaction, command, context)) return;
17+
try {
18+
await command.execute(interaction, context);
19+
} catch (e) {
20+
console.error(e);
21+
const errorMessage = "An error has occurred";
22+
interaction.editReply(errorMessage).catch(console.error);
23+
}
2324
}
24-
}
2525
}
2626
/**
2727
* Locally loads the commands to the context for further use
2828
* @param context
2929
* @returns
3030
*/
3131
export function loadCommands(context: MyContext) {
32-
// Promisifies the process of glob
33-
return new Promise((resolve) => {
32+
// Promisifies the process of glob
33+
return new Promise((resolve) => {
3434
// Find all js files
35-
glob(`${__dirname}/../commands/**/*.js`, async (err, files) => {
36-
await Promise.all(
37-
files.map(async (file) => {
38-
const { default: myCommandFile }: { default: Command } = await import(
39-
file
40-
).catch((err) => {
41-
console.error(err);
42-
// Since the return value gets destructured, an empty object is returned
43-
return {};
44-
});
45-
if (!myCommandFile) return;
46-
context.commands.set(myCommandFile.data.name, myCommandFile);
47-
})
48-
);
49-
resolve(undefined);
35+
glob(`${__dirname}/../commands/**/*.js`, async (err, files) => {
36+
await Promise.all(
37+
files.map(async (file) => {
38+
const { default: myCommandFile }: { default: Command } = await import(
39+
file
40+
).catch((err) => {
41+
console.error(err);
42+
// Since the return value gets destructured, an empty object is returned
43+
return {};
44+
});
45+
if (!myCommandFile) return;
46+
context.commands.set(myCommandFile.data.name, myCommandFile);
47+
})
48+
);
49+
resolve(undefined);
50+
});
5051
});
51-
});
5252
}
5353
/**
5454
* Checks if the bot or a user has the needed permissions to run a command
@@ -57,88 +57,89 @@ export function loadCommands(context: MyContext) {
5757
* @returns Whether to cancel the command
5858
*/
5959
function commandPermissionCheck(
60-
interaction: CommandInteraction,
61-
command: Command
60+
interaction: CommandInteraction,
61+
command: Command
6262
): boolean {
63-
const { client, user, channel } = interaction;
64-
if (channel.type === "DM" || !channel) {
65-
if (command.guildOnly) {
66-
interaction
67-
.editReply(
68-
`This is a guild exclusive command, not to be executed in a dm`
69-
)
70-
.catch(console.error);
71-
// For guild only commands that were executed in a dm, cancel the command
72-
return true;
63+
const { client, user, channel } = interaction;
64+
// If the channel is a dm, if it's a partial, channel.type wouldn't exist
65+
if (channel.type === "DM" || !channel) {
66+
if (command.guildOnly) {
67+
interaction
68+
.editReply(
69+
"This is a guild exclusive command, not to be executed in a dm"
70+
)
71+
.catch(console.error);
72+
// For guild only commands that were executed in a dm, cancel the command
73+
return true;
74+
}
75+
// If it's not a guild only command, since permissions aren't a thing on dms, allow execution
76+
return false;
7377
}
74-
// If it's not a guild only command, since permissions aren't a thing on dms, allow execution
75-
return false;
76-
}
77-
if (command.botPermissions) {
78-
const botPermissions = new Permissions(command.botPermissions);
79-
// The required permissions for the bot to run the command, missing in the channel.
80-
const missingPermissions = channel
81-
.permissionsFor(client.user)
82-
.missing(botPermissions);
83-
if (missingPermissions.length > 0) {
84-
interaction
85-
.editReply(
86-
`In order to run this command, I need the following permissions: ${missingPermissions
87-
.map((perm) => `\`${perm}\``)
88-
.join(", ")}`
89-
)
90-
.catch(console.error);
91-
return true;
78+
if (command.botPermissions) {
79+
const botPermissions = new Permissions(command.botPermissions);
80+
// The required permissions for the bot to run the command, missing in the channel.
81+
const missingPermissions = channel
82+
.permissionsFor(client.user)
83+
.missing(botPermissions);
84+
if (missingPermissions.length > 0) {
85+
interaction
86+
.editReply(
87+
`In order to run this command, I need the following permissions: ${missingPermissions
88+
.map((perm) => `\`${perm}\``)
89+
.join(", ")}`
90+
)
91+
.catch(console.error);
92+
return true;
93+
}
9294
}
93-
}
94-
if (command.authorPermissions) {
95-
const authorPermissions = new Permissions(command.authorPermissions);
96-
// The required permissions for the user to run the command, missing in the channel.
97-
const missingPermissions = channel
98-
.permissionsFor(user.id)
99-
.missing(authorPermissions);
100-
if (missingPermissions.length > 0) {
101-
interaction
102-
.editReply(
103-
`In order to run this command, you need: ${missingPermissions
104-
.map((perm) => `\`${perm}\``)
105-
.join(", ")}`
106-
)
107-
.catch(console.error);
108-
return true;
95+
if (command.authorPermissions) {
96+
const authorPermissions = new Permissions(command.authorPermissions);
97+
// The required permissions for the user to run the command, missing in the channel.
98+
const missingPermissions = channel
99+
.permissionsFor(user.id)
100+
.missing(authorPermissions);
101+
if (missingPermissions.length > 0) {
102+
interaction
103+
.editReply(
104+
`In order to run this command, you need: ${missingPermissions
105+
.map((perm) => `\`${perm}\``)
106+
.join(", ")}`
107+
)
108+
.catch(console.error);
109+
return true;
110+
}
109111
}
110-
}
111-
// By default, allow execution;
112-
return false;
112+
// By default, allow execution;
113+
return false;
113114
}
114115
function commandCooldownCheck(
115-
interaction: CommandInteraction,
116-
command: Command,
117-
context: MyContext
116+
interaction: CommandInteraction,
117+
command: Command,
118+
context: MyContext
118119
): boolean {
119-
const { user } = interaction;
120-
if (command.cooldown) {
121-
const id = user.id + "/" + interaction.commandName;
122-
const existingCooldown = context.cooldownCounter.get(id);
123-
if (existingCooldown) {
124-
if (Date.now() >= existingCooldown) {
125-
context.cooldownCounter.delete(id);
126-
return false;
127-
}
128-
interaction
129-
.editReply(
130-
`Please wait ${Formatters.time(
131-
Date.now() + existingCooldown,
132-
"R"
133-
)} before using the command again`
134-
)
135-
.catch(console.error);
136-
return true;
120+
const { user } = interaction;
121+
if (command.cooldown) {
122+
const id = user.id + "/" + interaction.commandName;
123+
const existingCooldown = context.cooldownCounter.get(id);
124+
if (existingCooldown) {
125+
if (Date.now() >= existingCooldown) {
126+
context.cooldownCounter.delete(id);
127+
return false;
128+
}
129+
interaction
130+
.editReply(
131+
`Please wait ${Formatters.time(
132+
Date.now() + existingCooldown,
133+
"R"
134+
)} before using the command again`
135+
)
136+
.catch(console.error);
137+
return true;
138+
}
139+
context.cooldownCounter.set(
140+
user.id + "/" + interaction.commandName,
141+
Date.now() + command.cooldown
142+
);
137143
}
138-
context.cooldownCounter.set(
139-
user.id + "/" + interaction.commandName,
140-
Date.now() + command.cooldown
141-
);
142-
}
143-
return false;
144+
return false;
144145
}

0 commit comments

Comments
 (0)