Skip to content

Commit

Permalink
feat: Discord の新ユーザーネームシステムに対応する (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meru authored Jul 22, 2023
1 parent 58f56be commit fb76ba9
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/adaptor/discord/member-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export class DiscordMemberStats
joinedAt,
createdAt,
bot: member.user.bot,
tag: member.user.tag,
/**
* "#0" がついたユーザーネーム(新システム)から取り除くの処理は user-info.ts 側で行う
* https://github.com/approvers/OreOreBot2/issues/914
*/
userName: member.user.tag,
hoistRoleId: hoistRoleId
};
}
Expand Down
98 changes: 93 additions & 5 deletions src/service/command/user-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('UserInfo', () => {
joinedAt: new Date(20050902),
createdAt: new Date(20050902),
bot: false,
tag: 'm2en#0092',
userName: 'meru#0',
hoistRoleId: '865951894173515786' as Snowflake
}
: null
Expand Down Expand Up @@ -57,8 +57,8 @@ describe('UserInfo', () => {
inline: true
},
{
name: 'ユーザー名+Discord Tag',
value: 'm2en#0092',
name: 'ユーザーネーム',
value: 'meru',
inline: true
},
{
Expand Down Expand Up @@ -120,8 +120,8 @@ describe('UserInfo', () => {
inline: true
},
{
name: 'ユーザー名+Discord Tag',
value: 'm2en#0092',
name: 'ユーザーネーム',
value: 'meru',
inline: true
},
{
Expand Down Expand Up @@ -175,3 +175,91 @@ describe('UserInfo', () => {
expect(fetchStats).toHaveBeenCalledOnce();
});
});

// "UserInfo" として定義してもいいが、見通し良くするために新しいスイートを定義する
describe('BotInfo', () => {
afterEach(() => {
vi.restoreAllMocks();
});

const repo: UserStatsRepository = {
fetchUserStats: (id) =>
Promise.resolve(
id === '279614913129742338'
? {
color: 'f08f8f',
displayName: 'さいな',
joinedAt: new Date(20230721),
createdAt: new Date(20230721),
bot: true,
userName: 'mikuro#2796',
hoistRoleId: '865951894173515786' as Snowflake
}
: null
)
};
const userInfo = new UserInfo(repo);

it('gets info bot user (old username system)', async () => {
const fetchStats = vi.spyOn(repo, 'fetchUserStats');
const fn = vi.fn();

await userInfo.on(
createMockMessage(
parseStringsOrThrow(
['userinfo', '279614913129742338'],
userInfo.schema
),
fn
)
);

expect(fn).toHaveBeenCalledWith({
title: 'ユーザーの情報',
description: '司令官、頼まれていた <@279614913129742338> の情報だよ',
fields: [
{
name: 'ID',
value: '279614913129742338',
inline: true
},
{
name: '表示名',
value: 'さいな',
inline: true
},
{
name: 'ユーザーネーム',
value: 'mikuro#2796',
inline: true
},
{
name: 'プロフィールカラー',
value: 'f08f8f',
inline: true
},
{
name: 'ユーザ種別',
value: 'ボット',
inline: true
},
{
name: 'メンバーリストの分類ロール',
value: '<@&865951894173515786>',
inline: true
},
{
name: 'サーバー参加日時',
value: `<t:20230>(<t:20230:R>)`,
inline: true
},
{
name: 'アカウント作成日時',
value: `<t:20230>(<t:20230:R>)`,
inline: true
}
]
});
expect(fetchStats).toHaveBeenCalledOnce();
});
});
17 changes: 13 additions & 4 deletions src/service/command/user-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface UserStats {
joinedAt?: Date;
createdAt: Date;
bot: boolean;
tag: string;
userName: string;
hoistRoleId?: Snowflake | undefined;
}

Expand Down Expand Up @@ -76,7 +76,7 @@ export class UserInfo implements CommandResponder<typeof SCHEMA> {
joinedAt,
createdAt,
bot,
tag,
userName,
hoistRoleId
}: UserStats,
userId: string
Expand All @@ -93,8 +93,8 @@ export class UserInfo implements CommandResponder<typeof SCHEMA> {
inline: true
},
{
name: 'ユーザー名+Discord Tag',
value: tag,
name: 'ユーザーネーム',
value: createUserNameDisplay(userName),
inline: true
},
{
Expand Down Expand Up @@ -138,10 +138,19 @@ function fetchUserId(arg: string, senderId: string): string {
}
return arg;
}

function createHoistRoleDisplay(hoistRoleId: Snowflake | undefined): string {
if (!hoistRoleId) {
return 'なし';
}

return `<@&${hoistRoleId}>`;
}

function createUserNameDisplay(userName: string): string {
if (userName.endsWith('#0')) {
// Discordのユーザー名に"#"を使うことは出来ないのでそのまま replace しても問題ない
return userName.replace('#0', '');
}
return userName;
}

0 comments on commit fb76ba9

Please sign in to comment.