Skip to content

Commit

Permalink
refactor: add laugh module
Browse files Browse the repository at this point in the history
  • Loading branch information
peterleiva committed Nov 4, 2021
1 parent 6b17e8b commit 2a732f3
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"private": true,
"type": "module",
"imports": {
"#joke": "./src/joke/index.js"
"#joke": "./src/joke/index.js",
"#laugh": "./src/laugh/index.js"
},
"scripts": {
"start": "node .",
Expand Down
2 changes: 1 addition & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from "events";
import { laugh } from "./commands/index.js";
import { laugh } from "#laugh";

function isMention(ctx) {
return ctx.message.entities?.some(entity => entity.type === "mention");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Photo } from "../models/index.js";
import { command } from "./command.js";
import { command } from "#laugh";

export function image(ctx) {
command(ctx, Photo.aggregate(), doc => new Photo(doc));
Expand Down
1 change: 0 additions & 1 deletion src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from "./help.js";
export * from "./text.js";
export * from "./emoji.js";
export * from "./image.js";
export * from "./laugh.js";
export * from "./meme.js";
6 changes: 0 additions & 6 deletions src/commands/laugh.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/commands/text.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Text as TextModel } from "../models/index.js";
import { command } from "./command.js";
import { command } from "#laugh";

export async function reply(ctx, match) {
command(ctx, TextModel.aggregate().match(match), doc => new TextModel(doc));
Expand Down
6 changes: 6 additions & 0 deletions src/commands/command.js → src/laugh/command.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import createDebug from "debug";
import Laugh from "./laugh.js";

const debug = createDebug("command");

export async function command(ctx, Aggregation, creator) {
Expand All @@ -7,3 +9,7 @@ export async function command(ctx, Aggregation, creator) {
debug("docs found in aggregation: %o", docs);
docs.forEach(doc => creator(doc).reply(ctx));
}

export function laugh(ctx) {
command(ctx, Laugh.aggregate(), doc => new Laugh(doc));
}
2 changes: 2 additions & 0 deletions src/laugh/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./command.js";
export { default as Laugh } from "./laugh.js";
File renamed without changes.
3 changes: 2 additions & 1 deletion src/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Telegraf } from "telegraf";
import { env, database } from "./utils/index.js";
import { shutdown } from "./shutdown.js";
import { Bot } from "./bot.js";
import { text, helper, emoji, image, laugh, meme } from "./commands/index.js";
import { text, helper, emoji, image, meme } from "./commands/index.js";
import { joke } from "#joke";
import { laugh } from "#laugh";

function gracefullyStop(bot) {
const signals = ["SIGINT", "SIGTERM"];
Expand Down
1 change: 0 additions & 1 deletion src/models/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as Photo } from "./photo.js";
export { default as Text } from "./text.js";
export { default as Laugh } from "./laugh.js";
2 changes: 1 addition & 1 deletion src/models/photo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose from "mongoose";
import Laugh from "./laugh.js";
import { Laugh } from "#laugh";

const { Schema } = mongoose;

Expand Down
2 changes: 1 addition & 1 deletion src/models/text.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose from "mongoose";
import Laugh from "./laugh.js";
import { Laugh } from "#laugh";
import { multiplier, numberParser } from "../utils/index.js";

const { Schema } = mongoose;
Expand Down

0 comments on commit 2a732f3

Please sign in to comment.