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(commands): added /party clear #420

Merged
merged 1 commit into from
Sep 19, 2024
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
21 changes: 21 additions & 0 deletions __snapshots__/update_commands.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,13 @@ snapshot[`commands 1`] = `
"description": "party management commands",
"description_localizations": {},
"options": [
{
"name": "clear",
"description": "remove all characters from your party",
"type": 1,
"required": false,
"description_localizations": {}
},
{
"name": "view",
"description": "view your current party",
Expand Down Expand Up @@ -1891,6 +1898,13 @@ snapshot[`commands 1`] = `
"description": "party management commands",
"description_localizations": {},
"options": [
{
"name": "clear",
"description": "remove all characters from your party",
"type": 1,
"required": false,
"description_localizations": {}
},
{
"name": "view",
"description": "view your current party",
Expand Down Expand Up @@ -2095,6 +2109,13 @@ snapshot[`commands 1`] = `
"description": "party management commands",
"description_localizations": {},
"options": [
{
"name": "clear",
"description": "remove all characters from your party",
"type": 1,
"required": false,
"description_localizations": {}
},
{
"name": "view",
"description": "view your current party",
Expand Down
47 changes: 47 additions & 0 deletions db/assignParty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,50 @@ describe('db.unassignCharacter()', () => {
});
});
});

describe('db.clearParty()', () => {
beforeEach(async () => {
mongod = await MongoMemoryServer.create();
client = new Mongo(mongod.getUri());
config.mongoUri = mongod.getUri();
});

afterEach(async () => {
delete config.mongoUri;
await client.close();
await mongod.stop();
});

it('normal', async () => {
const { insertedId: inventoryInsertedId } = await client.inventories()
.insertOne(
{
userId: 'user-id',
guildId: 'guild-id',
party: {
member1Id: 'character-1',
member2Id: 'character-2',
member3Id: 'character-3',
member4Id: 'character-4',
member5Id: 'character-5',
},
} as any,
);

await db.clearParty('user-id', 'guild-id');

const inventoryUpdated = await client.inventories().findOne(
{ _id: inventoryInsertedId },
);

assertObjectMatch(inventoryUpdated!, {
party: {
member1Id: null,
member2Id: null,
member3Id: null,
member4Id: null,
member5Id: null,
},
});
});
});
28 changes: 28 additions & 0 deletions db/assignParty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,31 @@ export async function unassignCharacter(
await db.close();
}
}

export async function clearParty(
userId: string,
guildId: string,
): Promise<void> {
const db = new Mongo();

try {
await db.connect();

await db.inventories().updateOne(
{ userId, guildId },
{
$set: {
party: {
member1Id: null,
member2Id: null,
member3Id: null,
member4Id: null,
member5Id: null,
},
},
},
);
} finally {
await db.close();
}
}
2 changes: 2 additions & 0 deletions db/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {

import {
assignCharacter,
clearParty,
swapSpots,
unassignCharacter,
} from '~/db/assignParty.ts';
Expand Down Expand Up @@ -185,6 +186,7 @@ const db = {
assignCharacter,
swapSpots,
unassignCharacter,
clearParty,
//
acquireSkill,
//
Expand Down
1 change: 1 addition & 0 deletions i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
"/party assign": "assign a character to your party",
"/party swap": "swap characters spots with each others",
"/party remove": "remove a character from your party",
"/party clear": "remove all characters from your party",

"/coll show": "check and view how complete are a user collections",
"/coll stars": "view user stars",
Expand Down
3 changes: 3 additions & 0 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ function pages(
`- \`/party remove\` \`/team remove\` \`/p remove\`: _${
i18n.get('/party remove', locale)
}_`,
`- \`/party clear\` \`/team clear\` \`/p clear\`: _${
i18n.get('/party clear', locale)
}_`,
'',
`- \`/collection show\` \`/coll show\` \`/mm show\`: _${
i18n.get('/coll show', locale)
Expand Down
7 changes: 7 additions & 0 deletions src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ export const handler = async (r: Request) => {
guildId,
userId: member.user.id,
}).send();
case 'clear': {
return party.clear({
token,
guildId,
userId: member.user.id,
}).send();
}
default: {
const user = options['user'] as string ?? member.user.id;

Expand Down
31 changes: 31 additions & 0 deletions src/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,42 @@
return discord.Message.spinner(true);
}

function clear({ token, userId, guildId }: {
token: string;
userId: string;
guildId: string;
}): discord.Message {
const locale = user.cachedUsers[userId]?.locale ??
user.cachedGuilds[guildId]?.locale;

Promise.resolve()
.then(async () => {
await db.clearParty(userId, guildId);

const inventory = await db.getInventory(guildId, userId);

return (await embed({ guildId, party: inventory.party, locale }))
.patch(token);
})
.catch(async (err) => {
if (!config.sentry) {
throw err;
}

Check warning on line 378 in src/party.ts

View check run for this annotation

Codecov / codecov/patch

src/party.ts#L374-L378

Added lines #L374 - L378 were not covered by tests

const refId = utils.captureException(err);

Check warning on line 380 in src/party.ts

View check run for this annotation

Codecov / codecov/patch

src/party.ts#L380

Added line #L380 was not covered by tests

await discord.Message.internal(refId).patch(token);
});

Check warning on line 383 in src/party.ts

View check run for this annotation

Codecov / codecov/patch

src/party.ts#L382-L383

Added lines #L382 - L383 were not covered by tests

return discord.Message.spinner(true);
}

const party = {
view,
assign,
swap,
remove,
clear,
};

export default party;
2 changes: 1 addition & 1 deletion tests/__snapshots__/help.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ snapshot[`/help 7`] = `
"author": {
"name": "Essential Commands"
},
"description": "- \`/gacha\` \`/w\`: _start a new gacha pull_\\n- \`/q\`: _start a gacha pull but with no animations_\\n- \`/pull\` \`/guaranteed\`: _use a guaranteed pull you have_\\n\\n- \`/now\` \`/tu\`: _check what you can do right now_\\n- \`/search\` \`/anime\` \`/manga\` \`/series\`: _search for a specific series_\\n- \`/character\` \`/char\`: _search for a specific character_\\n\\n- \`/merge\` \`/synthesize\`: _sacrifice characters to pull a new more shiny character_\\n\\n- \`/party view\` \`/team view\` \`/p view\`: _view your current party_\\n- \`/party assign\` \`/team assign\` \`/p assign\`: _assign a character to your party_\\n- \`/party swap\` \`/team swap\` \`/p swap\`: _swap characters spots with each others_\\n- \`/party remove\` \`/team remove\` \`/p remove\`: _remove a character from your party_\\n\\n- \`/collection show\` \`/coll show\` \`/mm show\`: _check and view how complete are a user collections_\\n- \`/collection stars\` \`/coll stars\` \`/mm stars\`: _view user stars_\\n- \`/collection media\` \`/coll media\` \`/mm media\`: _view user characters in a specific series_\\n- \`/collection sum\` \`/coll sum\` \`/mm sum\`: _view the sum numbers of a user collection_\\n\\n- \`/steal\`: _steal a character from another user_\\n- \`/trade\` \`/offer\`: _trade characters with another user_\\n- \`/give\` \`/gift\`: _give characters to another user_\\n\\n- \`/battle tower\`: _Climb the tower to glory and become a fable_\\n- \`/battle challenge\`: _Challenge the tower's next floor_\\n- \`/reclear\`: _Reclear your current tower floor to gain exp_"
"description": "- \`/gacha\` \`/w\`: _start a new gacha pull_\\n- \`/q\`: _start a gacha pull but with no animations_\\n- \`/pull\` \`/guaranteed\`: _use a guaranteed pull you have_\\n\\n- \`/now\` \`/tu\`: _check what you can do right now_\\n- \`/search\` \`/anime\` \`/manga\` \`/series\`: _search for a specific series_\\n- \`/character\` \`/char\`: _search for a specific character_\\n\\n- \`/merge\` \`/synthesize\`: _sacrifice characters to pull a new more shiny character_\\n\\n- \`/party view\` \`/team view\` \`/p view\`: _view your current party_\\n- \`/party assign\` \`/team assign\` \`/p assign\`: _assign a character to your party_\\n- \`/party swap\` \`/team swap\` \`/p swap\`: _swap characters spots with each others_\\n- \`/party remove\` \`/team remove\` \`/p remove\`: _remove a character from your party_\\n- \`/party clear\` \`/team clear\` \`/p clear\`: _remove all characters from your party_\\n\\n- \`/collection show\` \`/coll show\` \`/mm show\`: _check and view how complete are a user collections_\\n- \`/collection stars\` \`/coll stars\` \`/mm stars\`: _view user stars_\\n- \`/collection media\` \`/coll media\` \`/mm media\`: _view user characters in a specific series_\\n- \`/collection sum\` \`/coll sum\` \`/mm sum\`: _view the sum numbers of a user collection_\\n\\n- \`/steal\`: _steal a character from another user_\\n- \`/trade\` \`/offer\`: _trade characters with another user_\\n- \`/give\` \`/gift\`: _give characters to another user_\\n\\n- \`/battle tower\`: _Climb the tower to glory and become a fable_\\n- \`/battle challenge\`: _Challenge the tower's next floor_\\n- \`/reclear\`: _Reclear your current tower floor to gain exp_"
}
],
"attachments": [],
Expand Down
83 changes: 83 additions & 0 deletions tests/handler.commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,89 @@ Deno.test('party command handlers', async (test) => {
signatureStub.restore();
}
});

await test.step('party clear', async () => {
const body = JSON.stringify({
id: 'id',
token: 'token',
type: discord.InteractionType.Command,
guild_id: 'guild_id',
member: {
user: {
id: 'user_id',
},
},
data: {
name: 'party',
options: [{
type: 1,
name: 'clear',
}],
},
});

const validateStub = stub(utils, 'validateRequest', () => ({} as any));

const signatureStub = stub(utils, 'verifySignature', ({ body }) => ({
valid: true,
body,
} as any));

const partyStub = stub(party, 'clear', () => ({
send: () => true,
} as any));

config.publicKey = 'publicKey';

try {
const request = new Request('http://localhost:8000', {
body,
method: 'POST',
headers: {
'X-Signature-Ed25519': 'ed25519',
'X-Signature-Timestamp': 'timestamp',
},
});

const response = await handler(request);

assertSpyCall(validateStub, 0, {
args: [
request,
{
POST: {
headers: ['X-Signature-Ed25519', 'X-Signature-Timestamp'],
},
},
],
});

assertSpyCall(signatureStub, 0, {
args: [{
body,
signature: 'ed25519',
timestamp: 'timestamp',
publicKey: 'publicKey',
}],
});

assertSpyCall(partyStub, 0, {
args: [{
token: 'token',
userId: 'user_id',
guildId: 'guild_id',
}],
});

assertEquals(response, true as any);
} finally {
delete config.publicKey;

partyStub.restore();
validateStub.restore();
signatureStub.restore();
}
});
});

Deno.test('collection command handlers', async (test) => {
Expand Down
Loading