Skip to content

Commit

Permalink
feat(/packs installed): link to view pack in browser (#421)
Browse files Browse the repository at this point in the history
* feat(/packs installed): link to view pack in browser
  • Loading branch information
ker0olos authored Sep 19, 2024
1 parent 7062417 commit 00eac27
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ DEFAULT_SERVER_DUPES=1
XMAS=1
FOOLS=1


PACKS_URL=https://packs.deno.dev

# /now notice

NOTICE=
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const config: {
appId?: string;
publicKey?: string;
mongoUri?: string;
packsUrl?: string;
sentry?: string;
origin?: string;
notice?: string;
Expand All @@ -24,6 +25,7 @@ const config: {
appId: undefined,
publicKey: undefined,
mongoUri: undefined,
packsUrl: undefined,
sentry: undefined,
origin: undefined,
notice: undefined,
Expand Down Expand Up @@ -56,6 +58,8 @@ export async function initConfig(): Promise<void> {

config.mongoUri = Deno.env.get('MONGO_URI');

config.packsUrl = Deno.env.get('PACKS_URL');

config.notice = Deno.env.get('NOTICE');

// feature flags
Expand Down
19 changes: 3 additions & 16 deletions src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,24 +725,11 @@ export const handler = async (r: Request) => {
.send();
}
case 'uninstall': {
const list = await packs.all({
filter: true,
return (await packs.uninstallDialog({
guildId,
});

const pack = list.find(({ manifest }) =>
manifest.id === options['id'] as string
);

if (!pack) {
throw new Error('404');
}

return packs.uninstallDialog({
userId: member.user.id,
pack,
})
.send();
packId: options['id'] as string,
})).send();
}
}
break;
Expand Down
26 changes: 22 additions & 4 deletions src/packs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,29 @@ function packEmbed(pack: Pack): discord.Embed {
return embed;
}

function uninstallDialog(
{ pack, userId }: { pack: Pack; userId: string },
): discord.Message {
async function uninstallDialog(
{ userId, guildId, packId }: {
userId: string;
guildId: string;
packId: string;
},
): Promise<discord.Message> {
const locale = user.cachedUsers[userId]?.locale;

if (!config.communityPacks) {
throw new NonFetalError(
i18n.get('maintenance-packs', locale),
);
}

const list = await packs.all({ filter: true, guildId });

const pack = list.find(({ manifest }) => manifest.id === packId);

if (!pack) {
throw new Error('404');
}

const message = new discord.Message()
.addEmbed(packEmbed(pack));

Expand Down Expand Up @@ -163,7 +181,7 @@ async function pages(
s = `${manifest.title} | ${s}`;
}

return `${i + 1}. ${s}`;
return `${i + 1}. [${s}](${config.packsUrl}/${manifest.id})`;
}).join('\n'),
);
} else {
Expand Down
4 changes: 4 additions & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Deno.test('init', async (test) => {
'app_id',
'public_key',
'mongo_uri',
'packs_url',
'notice_message',
'0',
'0',
Expand All @@ -62,6 +63,7 @@ Deno.test('init', async (test) => {
publicKey: 'public_key',
sentry: 'sentry_dsn',
mongoUri: 'mongo_uri',
packsUrl: 'packs_url',
notice: 'notice_message',
global: true,
gacha: true,
Expand Down Expand Up @@ -126,6 +128,7 @@ Deno.test('init', async (test) => {
'app_id',
'public_key',
'mongo_uri',
'packs_url',
'notice_message',
'0',
'0',
Expand All @@ -151,6 +154,7 @@ Deno.test('init', async (test) => {
publicKey: 'public_key',
sentry: 'sentry_dsn',
mongoUri: 'mongo_uri',
packsUrl: 'packs_url',
notice: 'notice_message',
gacha: false,
global: false,
Expand Down
11 changes: 10 additions & 1 deletion tests/handler.commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8447,6 +8447,7 @@ Deno.test('community packs command handlers', async (test) => {
() => Promise.resolve([{ manifest } as any]),
);

config.communityPacks = true;
config.publicKey = 'publicKey';

try {
Expand Down Expand Up @@ -8525,6 +8526,7 @@ Deno.test('community packs command handlers', async (test) => {
}, json);
} finally {
delete config.publicKey;
delete config.communityPacks;

listStub.restore();
validateStub.restore();
Expand All @@ -8538,7 +8540,11 @@ Deno.test('community packs command handlers', async (test) => {
token: 'token',
type: discord.InteractionType.Command,
guild_id: 'guild_id',

member: {
user: {
id: 'user_id',
},
},
data: {
name: 'packs',
options: [{
Expand Down Expand Up @@ -8567,6 +8573,8 @@ Deno.test('community packs command handlers', async (test) => {

config.publicKey = 'publicKey';

config.communityPacks = true;

try {
const request = new Request('http://localhost:8000', {
body,
Expand Down Expand Up @@ -8627,6 +8635,7 @@ Deno.test('community packs command handlers', async (test) => {
}, json);
} finally {
delete config.publicKey;
delete config.communityPacks;

listStub.restore();
validateStub.restore();
Expand Down
11 changes: 9 additions & 2 deletions tests/packs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,7 @@ Deno.test('/installed packs', async (test) => {
() => Promise.resolve([pack, pack]),
);

config.packsUrl = 'http://localhost:8080/packs';
config.communityPacks = true;

try {
Expand All @@ -2107,12 +2108,15 @@ Deno.test('/installed packs', async (test) => {
components: [],
embeds: [{
type: 'rich',
description: '1. `pack_id`\n2. `pack_id`',
description:
'1. [`pack_id`](http://localhost:8080/packs/pack_id)\n' +
'2. [`pack_id`](http://localhost:8080/packs/pack_id)',
}],
},
});
} finally {
delete config.communityPacks;
delete config.packsUrl;

listStub.restore();
}
Expand All @@ -2133,6 +2137,7 @@ Deno.test('/installed packs', async (test) => {
() => Promise.resolve([pack]),
);

config.packsUrl = 'http://localhost:8080/packs';
config.communityPacks = true;

try {
Expand All @@ -2148,12 +2153,14 @@ Deno.test('/installed packs', async (test) => {
components: [],
embeds: [{
type: 'rich',
description: '1. Title | `pack-id`',
description:
'1. [Title | `pack-id`](http://localhost:8080/packs/pack-id)',
}],
},
});
} finally {
delete config.communityPacks;
delete config.packsUrl;

listStub.restore();
}
Expand Down

0 comments on commit 00eac27

Please sign in to comment.