Skip to content

Commit

Permalink
Merge pull request #16 from NID-roid/feat/reaction-agent
Browse files Browse the repository at this point in the history
Refactor handleMessageCreate to handle message replies and add sasudai reaction
  • Loading branch information
k-taro56 authored Jun 3, 2024
2 parents 1d6a7b2 + 4da1973 commit 55e1212
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export const handleMessageCreate =
sasudaiReaction(message);
}

if (message.reference?.messageId) {
const repliedMessage = await message.fetchReference();
if (message.content === '!daihyo') {
message.delete();
sasudaiReaction(repliedMessage);
}
}

if (message.content === '!sasudai') {
message.reply('https://x.com/STECH_FES/status/1773995315420631265');
sasudaiReaction(message);
Expand Down
48 changes: 48 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('handleMessageCreate', () => {
const mockReact = jest.fn();
const mockReply = jest.fn();
const mockDisplayAvatarURL = jest.fn();
const mockDelete = jest.fn();
const client = { user: {} } as unknown as Client;
const handleMessageCreateCurried = handleMessageCreate(client);

Expand All @@ -37,19 +38,41 @@ describe('handleMessageCreate', () => {
channelType,
isBot = false,
isMentionedMe = false,
hasReference = false,
}: {
content: string;
channelType: ChannelType;
isBot?: boolean;
isMentionedMe?: boolean;
hasReference?: boolean;
}) => {
const fetchReference = hasReference
? jest.fn().mockResolvedValue(
createMockMessage({
content: '',
channelType: ChannelType.GuildText,
}),
)
: undefined;

const reference = hasReference
? {
messageId: '1223834970863177769',
channelId: '1223834970863177769',
guildId: '1223834970863177769',
}
: undefined;

return {
content,
author: { bot: isBot, displayAvatarURL: mockDisplayAvatarURL },
channel: { type: channelType },
react: mockReact,
reply: mockReply,
mentions: { users: { has: () => isMentionedMe } },
delete: mockDelete,
fetchReference: fetchReference,
reference: reference,
} as unknown as Message;
};

Expand All @@ -72,6 +95,7 @@ describe('handleMessageCreate', () => {
}

expect(mockReply).not.toHaveBeenCalled();
expect(mockDelete).not.toHaveBeenCalled();
});

it('should react with specific emojis when content includes "代表"', async () => {
Expand All @@ -87,6 +111,21 @@ describe('handleMessageCreate', () => {
expect(fetch).not.toHaveBeenCalled();
}
expectReactionsToHaveBeenCalled(mockReact);

expect(mockDelete).not.toHaveBeenCalled();
});

it('should delete the message and react to the replied message if the command is used', async () => {
const message = createMockMessage({
content: '!daihyo',
channelType: ChannelType.GuildText,
hasReference: true,
});

await handleMessageCreateCurried(message);

expect(mockDelete).toHaveBeenCalled();
expectReactionsToHaveBeenCalled(mockReact);
});

it('replies with a specific URL and reacts when the message content is "!sasudai"', async () => {
Expand All @@ -103,6 +142,7 @@ describe('handleMessageCreate', () => {
expectReactionsToHaveBeenCalled(mockReact);

expect(fetch).not.toHaveBeenCalled();
expect(mockDelete).not.toHaveBeenCalled();
});

it('should reply to direct messages if not from a bot', async () => {
Expand All @@ -121,6 +161,8 @@ describe('handleMessageCreate', () => {
expect(mockReply).toHaveBeenCalledWith(
process.env.DM_MESSAGE_CONTENT ?? '',
);

expect(mockDelete).not.toHaveBeenCalled();
});

it('should not reply if the message author is a bot', async () => {
Expand All @@ -135,6 +177,7 @@ describe('handleMessageCreate', () => {
expect(mockReply).not.toHaveBeenCalled();

expect(fetch).not.toHaveBeenCalled();
expect(mockDelete).not.toHaveBeenCalled();
});

it('should reply to mentions if not from a bot', async () => {
Expand All @@ -151,6 +194,7 @@ describe('handleMessageCreate', () => {
);

expect(fetch).not.toHaveBeenCalled();
expect(mockDelete).not.toHaveBeenCalled();
});

it('should use default empty string if DM_MESSAGE_CONTENT is not defined', async () => {
Expand All @@ -164,6 +208,8 @@ describe('handleMessageCreate', () => {
await handleMessageCreateCurried(message);

expect(mockReply).toHaveBeenCalledWith('');

expect(mockDelete).not.toHaveBeenCalled();
});

it('should use default empty string if MENTION_MESSAGE_CONTENT is not defined', async () => {
Expand All @@ -179,5 +225,7 @@ describe('handleMessageCreate', () => {
await handleMessageCreateCurried(message);

expect(mockReply).toHaveBeenCalledWith('');

expect(mockDelete).not.toHaveBeenCalled();
});
});

0 comments on commit 55e1212

Please sign in to comment.