Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: diffと削除のメッセージを一定時間後に削除 #485

Merged
merged 2 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/adaptor/proxy/middleware/message-convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ const observableMessage = (
authorId: getAuthorSnowflake(raw),
author: raw.author?.username || '名無し',
content: raw.content || '',
async sendToSameChannel(message: string): Promise<void> {
await raw.channel.send(message);
async sendEphemeralToSameChannel(message: string): Promise<void> {
const FIVE_SECONDS_MS = 5000;
const sent = await raw.channel.send(message);

void sent
.awaitReactions({
time: FIVE_SECONDS_MS,
max: 1,
filter: (_reaction, user) => !user.bot,
dispose: true,
errors: ['time']
})
.catch(async () => {
if (sent.deletable) {
await sent.delete();
}
});
},
async replyMessage(message): Promise<void> {
await raw.reply(message);
Expand Down
1 change: 1 addition & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const features = FEATURE.split(',');
const intents = [
GatewayIntentBits.Guilds, // GUILD_CREATE による初期化
GatewayIntentBits.GuildMessages, // ほとんどのメッセージに反応する機能
GatewayIntentBits.GuildMessageReactions, // タイマー削除をリアクションでキャンセルする機能
GatewayIntentBits.GuildVoiceStates, // VoiceDiff 機能
GatewayIntentBits.GuildEmojisAndStickers // EmojiLog機能
];
Expand Down
9 changes: 5 additions & 4 deletions src/service/deletion-repeater.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { expect, it, vi } from 'vitest';

import { DeletionRepeater } from './deletion-repeater.js';

it('react to deleted message', async () => {
const responder = new DeletionRepeater(() => false);
await responder.on('DELETE', {
author: 'Baba',
content: 'Wall Is Stop',
sendToSameChannel: (message) => {
sendEphemeralToSameChannel: (message) => {
expect(message)
.toEqual(`Babaさん、メッセージを削除しましたね?私は見ていましたよ。内容も知っています。
\`\`\`
Expand All @@ -23,7 +24,7 @@ it('must not react', async () => {
await responder.on('CREATE', {
author: 'Baba',
content: 'Wall Is Not Stop',
sendToSameChannel: fn
sendEphemeralToSameChannel: fn
});
expect(fn).not.toHaveBeenCalled();
});
Expand All @@ -36,7 +37,7 @@ it("must not react if it's ignore target", async () => {
await responder.on('DELETE', {
author: 'Baba',
content: 'Wall Is Stop',
sendToSameChannel: fn
sendEphemeralToSameChannel: fn
});
expect(fn).not.toHaveBeenCalled();
});
Expand All @@ -48,7 +49,7 @@ it("must react if it's not ignore target", async () => {
await responder.on('DELETE', {
author: 'Baba',
content: 'Wall Is Not Stop',
sendToSameChannel: (message) => {
sendEphemeralToSameChannel: (message) => {
expect(message)
.toEqual(`Babaさん、メッセージを削除しましたね?私は見ていましたよ。内容も知っています。
\`\`\`
Expand Down
6 changes: 3 additions & 3 deletions src/service/deletion-repeater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export interface DeletionObservable {
readonly content: string;

/**
* `message` のメッセージをこのメッセージと同じチャンネルに送信する。
* すぐ消えてしまう `message` のメッセージをこのメッセージと同じチャンネルに送信する。
*
* @param {string} message
* @returns {Promise}
* @memberof Observable
*/
sendToSameChannel(message: string): Promise<void>;
sendEphemeralToSameChannel(message: string): Promise<void>;
}

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ export class DeletionRepeater<M extends DeletionObservable>
return;
}

await message.sendToSameChannel(`${author}さん、メッセージを削除しましたね?私は見ていましたよ。内容も知っています。
await message.sendEphemeralToSameChannel(`${author}さん、メッセージを削除しましたね?私は見ていましたよ。内容も知っています。
\`\`\`
${content}
\`\`\``);
Expand Down
17 changes: 9 additions & 8 deletions src/service/difference-detector.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, it, vi } from 'vitest';

import { DifferenceDetector } from './difference-detector.js';

it('react to edited message', async () => {
Expand All @@ -8,11 +9,11 @@ it('react to edited message', async () => {
'UPDATE',
{
content: 'LGBT',
sendToSameChannel: fn
sendEphemeralToSameChannel: fn
},
{
content: 'LGTM',
sendToSameChannel: (message) => {
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
- LGBT
Expand All @@ -28,14 +29,14 @@ it('react to edited message', async () => {
content: `pika
peka
`,
sendToSameChannel: fn
sendEphemeralToSameChannel: fn
},
{
content: `pika
peka
poka
`,
sendToSameChannel: (message) => {
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
+ poka
Expand All @@ -49,11 +50,11 @@ poka
{
content: `草
草`,
sendToSameChannel: fn
sendEphemeralToSameChannel: fn
},
{
content: `草`,
sendToSameChannel: (message) => {
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
- 草
Expand All @@ -70,15 +71,15 @@ poka
松屋
やよい軒
ロッテリア ロイヤルホスト`,
sendToSameChannel: fn
sendEphemeralToSameChannel: fn
},
{
content: `山陰に無い店
松屋
やよい軒
ロッテリア ロイヤルホスト
サイゼリヤ(鳥取にはある)`,
sendToSameChannel: (message) => {
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
+ サイゼリヤ(鳥取にはある)
Expand Down
6 changes: 3 additions & 3 deletions src/service/difference-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export interface EditingObservable {
readonly content: string;

/**
* `message` のメッセージをこのメッセージと同じチャンネルに送信する。
* すぐ消えてしまう `message` のメッセージをこのメッセージと同じチャンネルに送信する。
*
* @param {string} message
* @returns {Promise}
* @memberof Observable
*/
sendToSameChannel(message: string): Promise<void>;
sendEphemeralToSameChannel(message: string): Promise<void>;
}

const diffComposer = (before: string, after: string): string => {
Expand Down Expand Up @@ -56,7 +56,7 @@ export class DifferenceDetector
if (composed === '') {
return;
}
await after.sendToSameChannel(`見てたぞ
await after.sendEphemeralToSameChannel(`見てたぞ
\`\`\`diff
${composed}\`\`\``);
}
Expand Down