Skip to content

Commit

Permalink
Merge branch 'master' into hiring-looking-channel
Browse files Browse the repository at this point in the history
  • Loading branch information
LamboCreeper authored Nov 20, 2023
2 parents b9fd2b5 + 9ee4549 commit aff7ada
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 268 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @lambocreeper @jason2605 @ajsaraujo @theboxmage
* @lambocreeper @jason2605 @theboxmage @saramaebee
582 changes: 324 additions & 258 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
},
"dependencies": {
"@codesupport/inherited-config": "^1.0.2",
"axios": "^0.21.2",
"axios-cache-adapter": "^2.5.0",
"axios": "1.6.0",
"axios-cache-interceptor": "^1.3.2",
"discord.js": "^14.8.0",
"discordx": "^11.7.6",
"dotenv": "^16.3.1",
Expand Down
1 change: 1 addition & 0 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"COMMAND_PREFIX": "?",
"MEMBER_ROLE": "592088198746996768",
"MOD_ROLE": "490594428549857281",
"SHOWCASE_CHANNEL_ID": "240892912186032129",
"AUTHENTICATION_MESSAGE_ID": "592316062796873738",
"AUTHENTICATION_MESSAGE_CHANNEL": "592087564295471105",
"INSTANT_ANSWER_HIGHLIGHTS": [
Expand Down
27 changes: 27 additions & 0 deletions src/event/handlers/ShowcaseDiscussionThreadHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Events, Message } from "discord.js";
import EventHandler from "../../abstracts/EventHandler";
import getConfigValue from "../../utils/getConfigValue";

class ShowcaseDiscussionThreadHandler extends EventHandler {
constructor() {
super(Events.MessageCreate);
}

async handle(message: Message): Promise<void> {
if (message.channelId !== getConfigValue("SHOWCASE_CHANNEL_ID")) return;

const username = message.member?.nickname ?? message.member?.user.username;

try {
await message.startThread({
name: `Discuss ${username}'s Showcase Post`
});
} catch (error) {
console.error("Failed to create thread for showcase post", {
error
});
}
}
}

export default ShowcaseDiscussionThreadHandler;
11 changes: 5 additions & 6 deletions src/services/AdventOfCodeService.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import axios from "axios";
import cache from "axios-cache-adapter";
import { setupCache } from "axios-cache-interceptor";
import { AOCLeaderBoard, AOCMember } from "../interfaces/AdventOfCode";

export default class AdventOfCodeService {
// eslint-disable-next-line no-use-before-define
private static instance: AdventOfCodeService;
private api = axios.create({
adapter: cache.setupCache({
maxAge: 15 * 60 * 1000
}).adapter
});
private api = setupCache(axios);

static getInstance(): AdventOfCodeService {
if (!this.instance) {
Expand All @@ -27,6 +23,9 @@ export default class AdventOfCodeService {
headers: {
"Cookie": `session=${ADVENT_OF_CODE_TOKEN};`,
"User-Agent": `github.com/codesupport/discord-bot by ${process.env.CONTACT_EMAIL}`
},
cache: {
ttl: 15 * 60 * 1000
}
});

Expand Down
49 changes: 49 additions & 0 deletions test/event/handlers/ShowcaseDiscussionThreadHandlerTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect } from "chai";
import { createSandbox, SinonSandbox } from "sinon";
import { CustomMocks } from "@lambocreeper/mock-discord.js";
import { Events } from "discord.js";
import ShowcaseDiscussionThreadHandler from "../../../src/event/handlers/ShowcaseDiscussionThreadHandler";
import EventHandler from "../../../src/abstracts/EventHandler";
import getConfigValue from "../../../src/utils/getConfigValue";

describe("ShowcaseDiscussionThreadHandler", () => {
describe("constructor()", () => {
it("creates a handler for messageCreate", () => {
const handler = new ShowcaseDiscussionThreadHandler();

expect(handler.getEvent()).to.equal(Events.MessageCreate);
});
});

describe("handle()", () => {
let sandbox: SinonSandbox;
let handler: EventHandler;

beforeEach(() => {
sandbox = createSandbox();
handler = new ShowcaseDiscussionThreadHandler();
});

it("does nothing if the message is not sent in the showcase channel", async () => {
const message = CustomMocks.getMessage({ channel_id: "not-the-showcase-channel" });
const startThreadStub = sandbox.stub(message, "startThread");

await handler.handle(message);

expect(startThreadStub.called).to.be.false;
});

it("creates a thread if the message is sent in the showcase channel", async () => {
const message = CustomMocks.getMessage({ channel_id: getConfigValue("SHOWCASE_CHANNEL_ID") });
const startThreadStub = sandbox.stub(message, "startThread");

await handler.handle(message);

expect(startThreadStub.called).to.be.true;
});

afterEach(() => {
sandbox.restore();
});
});
});
3 changes: 2 additions & 1 deletion test/services/AdventOfCodeServiceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe("AdventOfCodeService", () => {
await aoc.getLeaderBoard("leaderboard", 2021);

expect(axiosGet.called).to.be.true;
expect(axiosGet.args[0][1]?.cache).to.deep.equal({ ttl: 900000 });
});

it("throws an error if the API responds when not authorized", async () => {
Expand All @@ -102,7 +103,7 @@ describe("AdventOfCodeService", () => {
});
});

describe("getSingelPlayer()", () => {
describe("getSinglePlayer()", () => {
let sandbox: SinonSandbox;
let aoc: AdventOfCodeService;

Expand Down

0 comments on commit aff7ada

Please sign in to comment.