Skip to content

Commit

Permalink
Merge branch 'main' into qol
Browse files Browse the repository at this point in the history
  • Loading branch information
ker0olos authored Sep 19, 2024
2 parents 34782d0 + 7062417 commit 012550f
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 1 deletion.
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 @@ function remove({ token, spot, userId, guildId }: {
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;
}

const refId = utils.captureException(err);

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

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

0 comments on commit 012550f

Please sign in to comment.