Skip to content

Commit

Permalink
feat: メッセージの変更差分の改善 (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Jan 24, 2023
1 parent 8cf8bc1 commit e3950be
Show file tree
Hide file tree
Showing 8 changed files with 500 additions and 828 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"@discordjs/voice": "^0.14.0",
"cli-argparse": "^1.1.2",
"date-fns": "^2.29.3",
"difflib-ts": "^1.0.3",
"discord.js": "^14.7.1",
"dotenv": "^16.0.3",
"fast-diff": "^1.2.0",
"nanoid": "^4.0.0",
"tweetnacl": "^1.0.3",
"yaml": "^2.1.3"
Expand Down
2 changes: 1 addition & 1 deletion src/model/judging-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const judgingStatuses = [
'AC'
] as const;

export type JudgingStatus = typeof judgingStatuses[number];
export type JudgingStatus = (typeof judgingStatuses)[number];

export function isJudgingStatus(str: string): str is JudgingStatus {
return (judgingStatuses as readonly string[]).includes(str);
Expand Down
2 changes: 1 addition & 1 deletion src/service/command/meme/ojaru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MemeTemplate } from '../../../model/meme-template.js';

const ojaruFlags = ['g'] as const;

export const ojaru: MemeTemplate<typeof ojaruFlags[number], never> = {
export const ojaru: MemeTemplate<(typeof ojaruFlags)[number], never> = {
commandNames: ['ojaru'],
description:
'あっぱれおじゃる様!見事ミーム構文を使いこなされました!`-g`オプションを使用なさってデンボの口調の変更も可能でございます!',
Expand Down
4 changes: 2 additions & 2 deletions src/service/command/meme/takopi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const takopiFlags = ['f'] as const;
const takopiOptions = ['c'] as const;

export const takopi: MemeTemplate<
typeof takopiFlags[number],
typeof takopiOptions[number]
(typeof takopiFlags)[number],
(typeof takopiOptions)[number]
> = {
commandNames: ['takopi'],
description:
Expand Down
2 changes: 1 addition & 1 deletion src/service/command/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const assetKeys = [
'POTATO'
] as const;

export type AssetKey = typeof assetKeys[number];
export type AssetKey = (typeof assetKeys)[number];

export interface RandomGenerator {
/**
Expand Down
52 changes: 39 additions & 13 deletions src/service/difference-detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ it('react to edited message', async () => {
content: 'LGTM',
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
- LGBT
+ LGTM
\`\`\``);
> LG~~B~~T*M*`);
return Promise.resolve();
}
}
Expand All @@ -38,9 +35,10 @@ poka
`,
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
+ poka
\`\`\``);
> pika
> peka
> *poka*
> `);
return Promise.resolve();
}
}
Expand All @@ -56,9 +54,8 @@ poka
content: `草`,
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
- 草
\`\`\``);
> 草
> ~~草~~`);
return Promise.resolve();
}
}
Expand All @@ -81,12 +78,41 @@ poka
サイゼリヤ(鳥取にはある)`,
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
\`\`\`diff
+ サイゼリヤ(鳥取にはある)
\`\`\``);
> 山陰に無い店
> 松屋
> やよい軒
> ロッテリア ロイヤルホスト
> *サイゼリヤ(鳥取にはある)*`);
return Promise.resolve();
}
}
);

await responder.on(
'UPDATE',
{
content: `waw
wiw
wuw
wew
wow`,
sendEphemeralToSameChannel: fn
},
{
content: `waw
wuw
wow`,
sendEphemeralToSameChannel: (message) => {
expect(message).toEqual(`見てたぞ
> waw
> w~~i~~*u*w
> w~~u~~*o*w
> ~~wew~~
> ~~wow~~`);
return Promise.resolve();
}
}
);

expect(fn).not.toHaveBeenCalled();
});
36 changes: 22 additions & 14 deletions src/service/difference-detector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Differ } from 'difflib-ts';
import diff from 'fast-diff';

import type { MessageUpdateEventResponder } from '../runner/index.js';

Expand Down Expand Up @@ -28,18 +28,26 @@ export interface EditingObservable {
}

const diffComposer = (before: string, after: string): string => {
const differ = new Differ();
const changes = differ.compare(before.split('\n'), after.split('\n'));
let composed = '';
for (const change of changes) {
if (change.startsWith('-')) {
if (composed !== '') {
composed += '---------------------------------\n';
}
composed += `${change.trimEnd()}\n`;
const beforeLines: (string | undefined)[] = before.split('\n');
const afterLines: (string | undefined)[] = after.split('\n');
for (let i = 0; i < Math.max(beforeLines.length, afterLines.length); ++i) {
if (i != 0) {
composed += '\n';
}
if (change.startsWith('+')) {
composed += `${change.trimEnd()}\n`;
const changes = diff(beforeLines[i] ?? '', afterLines[i] ?? '');
for (const [type, diff] of changes) {
switch (type) {
case -1:
composed += `~~${diff}~~`;
break;
case 0:
composed += diff;
break;
case 1:
composed += `*${diff}*`;
break;
}
}
}
return composed;
Expand All @@ -57,8 +65,8 @@ export class DifferenceDetector
if (composed === '') {
return;
}
await after.sendEphemeralToSameChannel(`見てたぞ
\`\`\`diff
${composed}\`\`\``);
await after.sendEphemeralToSameChannel(
`見てたぞ\n${composed}`.replaceAll('\n', '\n> ')
);
}
}
Loading

0 comments on commit e3950be

Please sign in to comment.