Skip to content

Commit

Permalink
Merge pull request #19 from srm-kzilla/gap/refactor/architechture-change
Browse files Browse the repository at this point in the history
Refactor: Architecture Change
  • Loading branch information
harshgoel05 authored Feb 14, 2021
2 parents 3cb6a37 + eda37c1 commit d499fa9
Show file tree
Hide file tree
Showing 17 changed files with 490 additions and 326 deletions.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ crafted with <span style="color: #8b0000;">&hearts;</span> by your friends on th
- **Common Commands**

- `#kzjack help` - Get everything what Jack can do
- `#kzjack meme` - Jack sends you a crazyyyy meme
- `#kzjack joke` - Bored? Wait for jack to send you a joke, perfect joke indeed.
- `#kzjack shrink <URL>` - Shrink a looooong URL using [KZILLA.XYZ](https://kzilla.xyz/)
- **`#kzjack help` - Get everything what Jack can do**
- **`#kzjack meme` - Jack sends you a crazyyyy meme**
- **`#kzjack joke` - Bored? Wait for jack to send you a joke, perfect joke indeed.**
- **`#kzjack shrink <URL>` - Shrink a looooong URL using [KZILLA.XYZ](https://kzilla.xyz/)**
- **Example:** `#kzjack shrink https://srmkzilla.net`

- **Text Channel Commands**

- `#kzjack membercount` - Get members count for the channel
- **`#kzjack membercount` - Get members count for the channel**

- **Moderators Channel Commands**

- `#kzjack flush` -Flush all cache
- `#kzjack certificate <event-slug>` - Start a certificate thread
- `#kzjack announce <? here|everyone > <channel> {<?title>} <description>` - Announcements
- `#kzjack poll create <channel> {<Some Question>} [[<Option 1>],[<Option 2>],[<Option 3>]]` - Start a Poll
- `#kzjack poll result <Poll ID>` - Get Poll Results
- **`#kzjack flush` -Flush all cache**
- **`#kzjack certificate <event-slug>` - Start a certificate thread**
- `#kzjack certificate my-event`
- **`#kzjack announce <? here|everyone > <#channel> {<?title>} <description>` - Announcements**
- **Example:** `#kzjack announce #announcements {Hello World!} How are you?`
- **Example:** `#kzjack announce here #announcements {Hello World!} How are you?`
- **Example:** `#kzjack announce everyone #announcements {Hello World!} How are you?`
- **`#kzjack poll create <#channel> {<Some Question>} [[<Option 1>],[<Option 2>],[<Option 3>]]` - Start a Poll**
- **Example:** `#kzjack poll create #polls {Who's the best?} [[Jack],[Yvette]]`
- **`#kzjack poll result <Poll ID>` - Get Poll Results**
- **Example:** `#kzjack poll result s3cRe7ID`

## Installation 🔧

Expand Down
17 changes: 15 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { initDbClient, initEventDbClient } from "./src/utils/database";
import { initCache, refreshKeys } from "./src/utils/nodecache";
import { handleMemberJoin, handleMemberLeave } from "./src/helper/memberLogs";
import { serverLogger } from "./src/utils/logger";
import { checkForAccessByRoles } from "./src/helper/roleAuth";
import { incomingMessageSchema } from "./src/models/incomingMessage";
/******************************************
Initialize Server
*******************************************/
Expand All @@ -34,21 +36,32 @@ async function createServer() {
*******************************************/
if (!message.author.bot) {
if (message.content.split(" ")[0] == COMMANDS.prefix) {
const messageType: incomingMessageSchema = {
channelType: message.channel.type,
incomingUser: {
username: message.author.username,
discriminator: message.author.discriminator,
id: message.author.id,
isMod: await checkForAccessByRoles(message.member, [
`${process.env.OPERATOR_ROLE_ID}`,
]),
},
};
switch (message.channel.type) {
/******************************************
Text channel
*******************************************/
case "text": {
//check for our command
handleIncomingChannelCommand(message);
handleIncomingChannelCommand(message, messageType);

break;
}
/******************************************
DM channel
*******************************************/
case "dm": {
handleIncomingDMCommand(message);
handleIncomingDMCommand(message, messageType);
break;
}
default: {
Expand Down
77 changes: 47 additions & 30 deletions src/controllers/incomingMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,41 @@ import { handleJokes, handleMemes } from "../helper/jokes";
import { handleShrinkURLMessage } from "../helper/kzillaXYZ";
import { handleGetMemberCount } from "../helper/memberCount";
import { getEvent } from "../utils/nodecache";
import { COMMANDS, CONSTANTS } from "../utils/constants";
import { COMMANDS, CONSTANTS, ERRORS } from "../utils/constants";
import { serverLogger } from "../utils/logger";
import { flushCache } from "../helper/flushCache";
import { createPoll, getResult } from "../helper/polls";
import {
getHelpMessage,
invalidCommand,
internalError,
} from "../utils/messages";
import { createBasicEmbed, getHelpMessage } from "../utils/messages";
import { sendDirectMessageToUser } from "./sendMessageHandler";
import { incomingMessageSchema } from "../models/incomingMessage";
/**
* Handles all incoming commands in channel
* Handles all incoming commands in a text channel
*
* @param {Message} incomingMessage
* @param {Message} incomingMessage The incoming message
* @param {incomingMessageSchema} messageType The incoming message type
*/
export async function handleIncomingChannelCommand(incomingMessage: Message) {
export async function handleIncomingChannelCommand(
incomingMessage: Message,
messageType: incomingMessageSchema
) {
try {
const messageCommand = incomingMessage.content.split(" ")[1];

switch (messageCommand) {
case COMMANDS.certificate: {
getCertificateChannelMessage(incomingMessage);
getCertificateChannelMessage(incomingMessage, messageType);
break;
}
case COMMANDS.shrinkURL: {
handleShrinkURLMessage(incomingMessage);
handleShrinkURLMessage(incomingMessage, messageType);
break;
}
case COMMANDS.membercount: {
handleGetMemberCount(incomingMessage);
handleGetMemberCount(incomingMessage, messageType);
break;
}
case COMMANDS.announce: {
handleAnnouncements(incomingMessage);
handleAnnouncements(incomingMessage, messageType);
serverLogger(
"success",
incomingMessage.content.split(" ").splice(0, 5),
Expand All @@ -47,73 +48,89 @@ export async function handleIncomingChannelCommand(incomingMessage: Message) {
break;
}
case COMMANDS.joke: {
handleJokes(incomingMessage);
handleJokes(incomingMessage, messageType);
break;
}
case COMMANDS.memes: {
handleMemes(incomingMessage);
handleMemes(incomingMessage, messageType);
break;
}
case COMMANDS.cacheflush: {
flushCache(incomingMessage);
flushCache(incomingMessage, messageType);
break;
}
case COMMANDS.createPoll: {
if (incomingMessage.content.split(" ")[2] == "create")
createPoll(incomingMessage);
else getResult(incomingMessage);
createPoll(incomingMessage, messageType);
else getResult(incomingMessage, messageType);
break;
}
case COMMANDS.help: {
incomingMessage.channel.send(getHelpMessage());
incomingMessage.channel.send(getHelpMessage(messageType));
serverLogger("success", incomingMessage.content, "Help Message");
break;
}
default:
incomingMessage.channel.send(invalidCommand());
incomingMessage.channel.send(
`<@${messageType.incomingUser.id}>`,
createBasicEmbed(ERRORS.INVALID_COMMAND, "ERROR")
);
serverLogger("user-error", incomingMessage.content, "Invalid Command");
break;
}
} catch (err) {
serverLogger("error", incomingMessage.content, err);
incomingMessage.channel.send(internalError());
incomingMessage.channel.send(
`<@${messageType.incomingUser.id}>`,
createBasicEmbed(ERRORS.INTERNAL_ERROR(messageType.channelType), "ERROR")
);
}
}

/**
* Handles all incoming commands in Direct Message
*
* @param {Message} incomingMessage
* @param {Message} incomingMessage The incoming message
* @param {incomingMessageSchema} messageType The incoming message type
*/
export function handleIncomingDMCommand(incomingMessage: Message) {
export function handleIncomingDMCommand(
incomingMessage: Message,
messageType: incomingMessageSchema
) {
try {
const messageCommand = incomingMessage.content.split(" ")[1];
switch (messageCommand) {
case COMMANDS.shrinkURL: {
handleShrinkURLMessage(incomingMessage);
handleShrinkURLMessage(incomingMessage, messageType);
break;
}
case COMMANDS.help: {
incomingMessage.channel.send(getHelpMessage());
incomingMessage.channel.send(getHelpMessage(messageType));
serverLogger("success", incomingMessage.content, "Help Message");
break;
}
case COMMANDS.joke: {
handleJokes(incomingMessage);
handleJokes(incomingMessage, messageType);
break;
}
case COMMANDS.memes: {
handleMemes(incomingMessage);
handleMemes(incomingMessage, messageType);
break;
}
default:
incomingMessage.channel.send(invalidCommand());
incomingMessage.channel.send(
`<@${messageType.incomingUser.id}>`,
createBasicEmbed(ERRORS.INVALID_COMMAND, "ERROR")
);
serverLogger("user-error", incomingMessage.content, "Invalid Command");
break;
}
} catch (err) {
serverLogger("error", incomingMessage.content, err);
incomingMessage.channel.send(internalError());
incomingMessage.channel.send(
`<@${messageType.incomingUser.id}>`,
createBasicEmbed(ERRORS.INTERNAL_ERROR(messageType.channelType), "ERROR")
);
}
}

Expand All @@ -140,7 +157,7 @@ export async function handleIncomingReaction(
user,
message,
event.slug,
CONSTANTS.certificateUserDirectMessage(event.name)
CONSTANTS.certificateUserDirectMessage(event.name, user.username)
);
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/controllers/sendMessageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Message, MessageReaction, User } from "discord.js";
import { createBasicEmbed, internalError } from "../utils/messages";
import { createBasicEmbed } from "../utils/messages";
import { ERRORS } from "../utils/constants";
import { certificateEmojifilter } from "../utils/filters";
import { handleIncomingReaction } from "./incomingMessageHandler";
import { serverLogger } from "../utils/logger";
import { eventSchema } from "../models/event";
import { getEvent } from "../utils/nodecache";
import { certificateDMHandler } from "../helper/certificate";
import { emailSchema } from "../models/email";
Expand Down Expand Up @@ -32,9 +31,7 @@ export async function sendDirectMessageToUser(
return true;
} catch (err) {
serverLogger("user-error", dm.content, "Malformed Email");
dm.channel.send(
createBasicEmbed("Invalid Email!", ERRORS.INVALID_EMAIL, "ERROR")
);
dm.channel.send(createBasicEmbed(ERRORS.INVALID_EMAIL, "ERROR"));
return false;
}
},
Expand All @@ -49,7 +46,16 @@ export async function sendDirectMessageToUser(
});
} catch (err) {
serverLogger("error", message, "DM Blocked");
message.channel.send(ERRORS.DM_BLOCKED(user));
message.channel.send(
`<@${user.id}>`,
createBasicEmbed(
{
title: ERRORS.DM_BLOCKED.title,
message: ERRORS.DM_BLOCKED.message(user),
},
"ERROR"
)
);
}
}

Expand Down Expand Up @@ -78,10 +84,12 @@ export async function sendReactableMessage(
);

eventCollector.on("collect", (reaction: MessageReaction, user: User) => {
handleIncomingReaction(user, reaction, event.slug, message);
handleIncomingReaction(user, reaction, event.slug, reaction.message);
});
} catch (err) {
serverLogger("error", incomingMessage.content, err);
incomingMessage.channel.send(internalError());
incomingMessage.channel.send(
createBasicEmbed(ERRORS.INTERNAL_ERROR("dm"), "ERROR")
);
}
}
37 changes: 21 additions & 16 deletions src/helper/announcement.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { Message, TextChannel, NewsChannel } from "discord.js";
import { serverLogger } from "../utils/logger";
import {
announcementMessage,
invalidChannel,
invalidCommand,
unauthorizedUser,
} from "../utils/messages";
import { checkForAccessByRoles } from "../helper/roleAuth";
import { COMMANDS } from "../utils/constants";
import { announcementMessage, createBasicEmbed } from "../utils/messages";
import { COMMANDS, ERRORS } from "../utils/constants";
import { incomingMessageSchema } from "../models/incomingMessage";

/**
* Handles all announcements
*
* @param {Message} incomingMessage
* @param {incomingMessageSchema} messageType
*/

export async function handleAnnouncements(incomingMessage: Message) {
export async function handleAnnouncements(
incomingMessage: Message,
messageType: incomingMessageSchema
) {
try {
const isAllowed = await checkForAccessByRoles(incomingMessage.member, [
`${process.env.OPERATOR_ROLE_ID}`,
]);
if (isAllowed) {
if (messageType.incomingUser.isMod) {
const regex = new RegExp(
`^${COMMANDS.prefix} ${COMMANDS.announce}( here | everyone | )<#.+> \{.*\} (.|\n)+$`,
"g"
Expand Down Expand Up @@ -72,18 +68,27 @@ export async function handleAnnouncements(incomingMessage: Message) {
incomingMessage.content.split(" ").splice(0, 5),
"Invalid command"
);
incomingMessage.channel.send(invalidCommand());
incomingMessage.channel.send(
`<@${messageType.incomingUser.id}>`,
createBasicEmbed(ERRORS.INVALID_COMMAND, "ERROR")
);
}
} else {
serverLogger(
"user-error",
incomingMessage.content.split(" ").splice(0, 5),
"Unauthorized User"
);
incomingMessage.channel.send(unauthorizedUser());
incomingMessage.channel.send(
`<@${messageType.incomingUser.id}>`,
createBasicEmbed(ERRORS.UNAUTHORIZED_USER, "ERROR")
);
}
} catch (err) {
incomingMessage.channel.send(invalidChannel());
incomingMessage.channel.send(
`<@${messageType.incomingUser.id}>`,
createBasicEmbed(ERRORS.INVALID_CHANNEL, "ERROR")
);
serverLogger(
"user-error",
incomingMessage.content.split(" ").splice(0, 5),
Expand Down
Loading

0 comments on commit d499fa9

Please sign in to comment.