Skip to content

Commit

Permalink
added /reward pulls for server mods (#426)
Browse files Browse the repository at this point in the history
* feat(utils/db): add script to reset player data

* feat(commands): added `/reward pulls`
  • Loading branch information
ker0olos authored Jan 22, 2025
1 parent 0ea16f6 commit 0fae36c
Show file tree
Hide file tree
Showing 11 changed files with 878 additions and 7 deletions.
33 changes: 33 additions & 0 deletions __snapshots__/update_commands.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2875,6 +2875,39 @@ snapshot[`commands 1`] = `
}
]
},
{
"name": "reward",
"description": "reward commands",
"default_member_permissions": 32,
"description_localizations": {},
"options": [
{
"name": "pulls",
"description": "reward a user with normal pulls",
"type": 1,
"required": false,
"description_localizations": {},
"options": [
{
"name": "user",
"description": "the user you want to reward",
"type": 6,
"required": true,
"description_localizations": {}
},
{
"name": "amount",
"description": "the amount of pulls you want to give the user",
"type": 4,
"required": true,
"min_value": 1,
"max_value": 99,
"description_localizations": {}
}
]
}
]
},
{
"name": "stats",
"description": "manage your characters stat points and skills",
Expand Down
31 changes: 31 additions & 0 deletions db/_reset_player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Mongo } from '~/db/mod.ts';

if (import.meta.main) {
// deno-lint-ignore no-non-null-assertion
const db = new Mongo(Deno.env.get('MONGO_URI')!);

const userId = '910124289372610560';
const guildId = '1288361915978092545';

const result1 = await db.characters().deleteMany({
userId,
guildId,
});

console.log(result1.deletedCount);

const result2 = await db.inventories().deleteMany({
userId,
guildId,
});

console.log(result2.deletedCount);

const result3 = await db.users().deleteMany({
discordId: userId,
});

console.log(result3.deletedCount);

await db.close();
}
15 changes: 9 additions & 6 deletions db/addTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function addPulls(
userId: string,
guildId: string,
amount: number,
free: boolean = false,
): Promise<void> {
const db = new Mongo();

Expand All @@ -38,13 +39,15 @@ export async function addPulls(

session.startTransaction();

const { modifiedCount } = await db.users().updateOne({
discordId: userId,
availableTokens: { $gte: amount },
}, { $inc: { availableTokens: -amount } });
if (!free) {
const { modifiedCount } = await db.users().updateOne({
discordId: userId,
availableTokens: { $gte: amount },
}, { $inc: { availableTokens: -amount } });

if (!modifiedCount) {
throw new Error('INSUFFICIENT_TOKENS');
if (!modifiedCount) {
throw new Error('INSUFFICIENT_TOKENS');
}
}

await db.inventories().updateOne(
Expand Down
9 changes: 9 additions & 0 deletions i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
"you-bought-pulls": "You bought **{0}** {1} {2}",
"you-bought-guarantee": "You bought a **{0}**{1}pull {2}",

"admin-reward": "You want to reward {0} **{1}** {2} {3}?",
"rewarded-pulls": "You rewarded {0} **{1}** {2} {3}",
"got-rewarded-pulls": "{0} rewarded you **{1}** {2} {3}",

"you-empty-collection": "You don't have any {0}characters",
"you-empty-media-collection": "You don't have any characters from {0}",

Expand Down Expand Up @@ -334,12 +338,15 @@

"/help": "need more information? we got you",

"/reward pulls": "reward a user with normal pulls",

"$debug": "display the nerdy stuff",
"$media-title": "the title of the series",
"$character-name": "the name of the character",
"$skill-name": "the name of the required skill",
"$media-characters": "view the characters from the series",
"$user-list": "the user of the list",
"$user-reward": "the user you want to reward",
"$user-party": "the user of the party",
"$user-likes-list": "the user of the likes list",
"$user-likes-filter-owned": "filter out all the characters already owned by you",
Expand All @@ -361,6 +368,8 @@
"$buy-keys-amount": "the amount of keys you want to buy",
"$buy-guaranteed-stars": "the star rating you want to buy",

"$reward-pulls": "the amount of pulls you want to give the user",

"$party-spot": "the spot where you want this character",
"$party-swap-1": "the first target of the swap",
"$party-swap-2": "the second target of the swap",
Expand Down
2 changes: 1 addition & 1 deletion src/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export enum MessageType {
Pong = 1,
New = 4,
Loading = 5,
Defer = 6,
Defer = 6, // For components
Update = 7,
Suggestions = 8,
Modal = 9,
Expand Down
54 changes: 54 additions & 0 deletions src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import stats from '~/src/stats.ts';
import battle from '~/src/battle.ts';
import tower from '~/src/tower.ts';
import help from '~/src/help.ts';
import reward from '~/src/reward.ts';
import serverOptions from '~/src/serverOptions.ts';

import _skills, { skills } from '~/src/skills.ts';
Expand Down Expand Up @@ -840,6 +841,25 @@ export const handler = async (r: Request) => {
nick: userId !== member.user.id,
}).send();
}
case 'reward': {
// deno-lint-ignore no-non-null-assertion
switch (subcommand!) {
case 'pulls': {
const targetId = options['user'] as string ?? member.user.id;
const amount = options['amount'] as number ?? 1;

return reward.pulls({
amount,
targetId,
userId: member.user.id,
}).send();
}
default: {
break;
}
}
break;
}
default: {
break;
}
Expand Down Expand Up @@ -1372,6 +1392,40 @@ export const handler = async (r: Request) => {
.setType(discord.MessageType.Update)
.send();
}
case 'reward': {
// deno-lint-ignore no-non-null-assertion
const item = customValues![0];

// deno-lint-ignore no-non-null-assertion
const userId = customValues![1];

// deno-lint-ignore no-non-null-assertion
const targetId = customValues![2];

// deno-lint-ignore no-non-null-assertion
const amount = parseInt(customValues![3]);

if (userId && userId !== member.user.id) {
throw new NoPermissionError();
}

switch (item) {
case 'pulls': {
return (await reward.confirmPulls({
amount,
guildId,
targetId,
userId,
token,
}))
.setType(discord.MessageType.Defer)
.send();
}
default:
break;
}
break;
}
default:
break;
}
Expand Down
102 changes: 102 additions & 0 deletions src/reward.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import user from '~/src/user.ts';

import i18n from '~/src/i18n.ts';

import * as discord from '~/src/discord.ts';

import db from '~/db/mod.ts';

function pulls(
{ userId, targetId, amount }: {
userId: string;
targetId: string;
amount: number;
},
): discord.Message {
const locale = user.cachedUsers[userId]?.locale;

const message = new discord.Message();

message.addEmbed(
new discord.Embed()
.setDescription(
i18n.get(
'admin-reward',
locale,
`<@${targetId}>`,
amount,
amount > 1 ? i18n.get('pulls', locale) : i18n.get('pull', locale),
discord.emotes.add,
),
),
);

message.addComponents([
new discord.Component().setId(
'reward',
'pulls',
userId,
targetId,
`${amount}`,
)
.setLabel(i18n.get('confirm', locale)),
new discord.Component().setId('cancel', userId)
.setStyle(discord.ButtonStyle.Red)
.setLabel(i18n.get('cancel', locale)),
]);

return message;
}

async function confirmPulls({ userId, targetId, guildId, amount, token }: {
userId: string;
targetId: string;
guildId: string;
amount: number;
token: string;
}): Promise<discord.Message> {
const locale = user.cachedUsers[userId]?.locale;

await db.addPulls(targetId, guildId, amount, true);

const message = new discord.Message();

message
.addEmbed(new discord.Embed().setDescription(
i18n.get(
'rewarded-pulls',
locale,
`<@${targetId}>`,
amount,
amount > 1 ? i18n.get('pulls', locale) : i18n.get('pull', locale),
discord.emotes.add,
),
));

const newMessage = new discord.Message().setContent(`<@${targetId}>`);

newMessage
.addEmbed(new discord.Embed().setDescription(
i18n.get(
'got-rewarded-pulls',
locale,
`<@${userId}>`,
amount,
amount > 1 ? i18n.get('pulls', locale) : i18n.get('pull', locale),
discord.emotes.add,
),
));

message.patch(token).then(() => {
newMessage.followup(token);
});

return new discord.Message();
}

const reward = {
pulls,
confirmPulls,
};

export default reward;
Loading

0 comments on commit 0fae36c

Please sign in to comment.