Skip to content

Commit

Permalink
Merge pull request #35 from pattyjogal/eslint
Browse files Browse the repository at this point in the history
Add ESLint Features
  • Loading branch information
pattyjogal authored Oct 1, 2021
2 parents f3c270c + a685548 commit b7d60f0
Show file tree
Hide file tree
Showing 11 changed files with 1,122 additions and 100 deletions.
26 changes: 26 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"env": {
"browser": false,
"es2021": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "variable",
"format": ["strictCamelCase", "StrictPascalCase"]
},
{
"selector": "function",
"format": ["strictCamelCase"]
}
]
}
}
13 changes: 12 additions & 1 deletion .github/workflows/push-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ jobs:
# Make sure the actual branch is checked out when running on pull requests
ref: ${{ github.head_ref }}

- name: Setup Node Environment
uses: actions/setup-node@v2
with:
node-version: '16'

- name: Prettify code
uses: creyD/[email protected]
with:
# This part is also where you can pass other options, for example:
prettier_options: --write **/*.{js,md}
prettier_options: --write **/*.{js,md}

- name: Install Dev Dependencies
run: yarn install --production=false

- name: Run ESLint
run: yarn run eslint . --ext .js,.jsx,.ts,.tsx
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"build": "tsc -p ."
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0",
"ts-node": "^10.2.1",
"typescript": "^4.4.2"
}
Expand Down
10 changes: 4 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Client, Intents } from "discord.js";
import { config } from "dotenv";
import { Db } from "mongodb";

import cmd_register from "./commands/register";
import {
cmd_tenmans,
cmdTenmans,
handleButton as tenmansHandleButton,
handleVoteCleaning,
} from "./commands/tenmans";
Expand All @@ -19,8 +18,7 @@ async function initBotConfig(client: Client, db: Db) {

if (!botConfigDoc) {
// Persist default configuration data
const defaultChannelId =
process.env.DEFAULT_QUEUEMSG_CHANNELID;
const defaultChannelId = process.env.DEFAULT_QUEUEMSG_CHANNELID;
const minVoteCount = process.env.MIN_VOTE_COUNT;
const hoursTillVoteClose = process.env.HOURS_TO_CLOSE;

Expand All @@ -41,7 +39,7 @@ async function initBotConfig(client: Client, db: Db) {
setInterval(handleVoteCleaning, 300000); // Clean vote embeds every 5 minutes
}

export default (db: Db) => {
export default (db: Db): Client => {
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.on("ready", async () => {
Expand All @@ -56,7 +54,7 @@ export default (db: Db) => {
if (interaction.isCommand()) {
const actions = {
register: cmd_register,
tenmans: cmd_tenmans,
tenmans: cmdTenmans,
config: cmdConfig,
};

Expand Down
6 changes: 3 additions & 3 deletions src/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export type RepliableInteraction =
export abstract class MessageExecutable<T extends RepliableInteraction> {
constructor(protected interaction: T, protected db: Db) {}

abstract execute(): Promise<any>;
abstract execute(): Promise<void>;
}

export abstract class RegisteredUserExecutable<
T extends RepliableInteraction
> extends MessageExecutable<T> {
protected user: Member;

async execute(): Promise<any> {
async execute(): Promise<void> {
const user = (await this.db.collection("members").findOne({
discordId: this.interaction.user.id,
})) as Member;
Expand All @@ -39,5 +39,5 @@ export abstract class RegisteredUserExecutable<
this.afterUserExecute();
}

abstract afterUserExecute(): Promise<any>;
abstract afterUserExecute(): Promise<void>;
}
6 changes: 3 additions & 3 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import botConfig from "../config/botConfig";

class DefaultChannelSubcommand extends MessageExecutable<CommandInteraction> {
async execute() {
const interaction_user = this.interaction.user;
const interactionUser = this.interaction.user;
const role = this.interaction.guild.roles.cache.find(
(role) => role.name === "Admin"
);

if (
!this.interaction.guild.members.cache
.get(interaction_user.id)
.get(interactionUser.id)
.roles.cache.has(role.id)
) {
this.interaction.reply({
Expand Down Expand Up @@ -54,7 +54,7 @@ class DefaultChannelSubcommand extends MessageExecutable<CommandInteraction> {
}
}

export async function cmdConfig(interaction, db: Db) {
export async function cmdConfig(interaction: CommandInteraction, db: Db): Promise<void> {
const commands: {
[key: string]: {
new (
Expand Down
8 changes: 4 additions & 4 deletions src/commands/register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandInteraction, Message } from "discord.js";
import { CommandInteraction } from "discord.js";
import { Db } from "mongodb";

import { MessageExecutable } from "./command";
Expand All @@ -12,7 +12,7 @@ class NewUserSubcommand extends MessageExecutable<CommandInteraction> {
});

// Exit if data for a user already exists
if (!!existingUser) {
if (existingUser) {
this.interaction.reply({
content:
"I already know **exactly** who you are. No need to register again, my friend!",
Expand Down Expand Up @@ -86,7 +86,7 @@ class UpdateUserSubcommand extends MessageExecutable<CommandInteraction> {
}
}

function cmd_register(interaction, db: Db) {
function cmdRegister(interaction: CommandInteraction, db: Db): void {
const commands = {
"new": NewUserSubcommand,
"update": UpdateUserSubcommand
Expand All @@ -97,4 +97,4 @@ function cmd_register(interaction, db: Db) {
subcmd.execute();
}

export default cmd_register;
export default cmdRegister;
30 changes: 18 additions & 12 deletions src/commands/tenmans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "./command";

let tenmansQueue: Member[] = [];
let time: String | null;
let time: string | null;
let activeTenmansMessage: Message | null;
let activeVoteMessage: Message | null;
let voteClosingTime: Date | null;
Expand All @@ -46,7 +46,7 @@ abstract class BaseQueueAction<
/// like updating related embed messages, etc.
abstract updateUserInterface();

async afterUserExecute(): Promise<any> {
async afterUserExecute(): Promise<void> {
const errorMessage = this.verifyQueueId();
if (errorMessage) {
this.interaction.reply({
Expand Down Expand Up @@ -143,7 +143,7 @@ class VoteQueueButtonAction extends VoteQueueAction<ButtonInteraction> {
// Verify that a pingable role exists for 10 mans on this server
const roleId = await this.interaction.guild.roles
.fetch()
.then((roles: Collection<String, Role>) => {
.then((roles: Collection<string, Role>) => {
for (const role of roles.values()) {
if (role.name === "10 Mans") {
return role.id;
Expand Down Expand Up @@ -243,15 +243,15 @@ class ManualRemoveUserToQueue extends StandardQueueAction<CommandInteraction> {
}

class SubcommandTenmansStart extends MessageExecutable<CommandInteraction> {
async execute(): Promise<any> {
const interaction_user = this.interaction.user;
async execute(): Promise<void> {
const interactionUser = this.interaction.user;
const role = this.interaction.guild.roles.cache.find(
(role) => role.name === "Admin"
);

if (
!this.interaction.guild.members.cache
.get(interaction_user.id)
.get(interactionUser.id)
.roles.cache.has(role.id)
) {
this.interaction.reply({
Expand Down Expand Up @@ -282,16 +282,16 @@ class SubcommandTenmansStart extends MessageExecutable<CommandInteraction> {
}

class TenmansCloseSubcommand extends MessageExecutable<CommandInteraction> {
async execute(): Promise<any> {
async execute(): Promise<void> {
// Verify privilege
const interaction_user = this.interaction.user;
const interactionUser = this.interaction.user;
const role = this.interaction.guild.roles.cache.find(
(role) => role.name === "Admin"
);

if (
!this.interaction.guild.members.cache
.get(interaction_user.id)
.get(interactionUser.id)
.roles.cache.has(role.id)
) {
this.interaction.reply({
Expand Down Expand Up @@ -378,7 +378,10 @@ class TenmansVoteSubcommand extends RegisteredUserExecutable<CommandInteraction>
}
}

export async function cmd_tenmans(interaction: CommandInteraction, db: Db) {
export async function cmdTenmans(
interaction: CommandInteraction,
db: Db
): Promise<void> {
const commands: {
[key: string]: {
new (
Expand Down Expand Up @@ -413,7 +416,10 @@ export async function cmd_tenmans(interaction: CommandInteraction, db: Db) {
command.execute();
}

export async function handleButton(interaction: ButtonInteraction, db: Db) {
export async function handleButton(
interaction: ButtonInteraction,
db: Db
): Promise<void> {
const commands: {
[key: string]: {
new (
Expand Down Expand Up @@ -505,7 +511,7 @@ const createVoteQueueActionRow = (queueId) => {
);
};

export async function handleVoteCleaning() {
export async function handleVoteCleaning(): Promise<void> {
if (activeVoteMessage) {
// Close vote if it has expired
if (voteClosingTime < new Date()) {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ const rest = new REST({ version: "9" }).setToken(process.env.CYPHERBOT_TOKEN);
console.error(error);
}

const db = await setupDB();
const db = await setupDb();

client(db).login(process.env.CYPHERBOT_TOKEN);
})();

// DB Setup
async function setupDB() {
async function setupDb() {
const client = await MongoClient.connect(process.env.DB_CONN_STRING, {
ignoreUndefined: true,
});
Expand Down
2 changes: 0 additions & 2 deletions src/models/member.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ObjectId } from "mongodb";

export default class Member {
constructor(
public gameTag: string,
Expand Down
Loading

0 comments on commit b7d60f0

Please sign in to comment.