Compare commits
41 commits
Author | SHA1 | Date | |
---|---|---|---|
|
53656ba0da | ||
|
27b6224b5e | ||
|
e584c1291b | ||
|
976445fa0d | ||
|
27a4019f00 | ||
|
f6c744cdcf | ||
|
a03a62277d | ||
|
75315b3db2 | ||
|
25f605e623 | ||
|
1395a65344 | ||
|
fd16500315 | ||
|
a581bf9d80 | ||
|
1b707c4517 | ||
|
5c6c0e65c3 | ||
|
ebec66607f | ||
|
f12bb11ffb | ||
|
837013835e | ||
|
cfcc8ad100 | ||
|
302a762912 | ||
|
6b9d71758a | ||
|
682f62d42b | ||
|
916244b57c | ||
|
696810e093 | ||
|
be2b837d56 | ||
|
e36ef85507 | ||
|
318b07e206 | ||
|
aa1ee5588a | ||
|
5f513d740e | ||
|
17ea4d9ab4 | ||
|
caa991a259 | ||
|
40dbf8d2c0 | ||
|
b8721c253c | ||
|
836a1d341f | ||
|
5a8ec932b4 | ||
|
b9264e23db | ||
|
de14723df0 | ||
|
f07058d369 | ||
|
f0870f0d61 | ||
|
fca199d9bd | ||
|
dbbfd74c04 | ||
|
6cff759135 |
15
.env.example
15
.env.example
|
@ -7,29 +7,30 @@
|
|||
# any secret values.
|
||||
|
||||
BOT_TOKEN=
|
||||
BOT_VER=0.5.2
|
||||
BOT_VER=0.6.4
|
||||
BOT_AUTHOR=Vylpes
|
||||
BOT_OWNERID=147392775707426816
|
||||
BOT_CLIENTID=682942374040961060
|
||||
BOT_ENV=4
|
||||
BOT_ADMINS=147392775707426816,887272961504071690
|
||||
BOT_LOGLEVEL=info
|
||||
|
||||
ABOUT_FUNDING=
|
||||
ABOUT_REPO=
|
||||
|
||||
DATA_DIR=
|
||||
DATA_DIR=./.temp
|
||||
|
||||
DB_HOST=
|
||||
DB_PORT=
|
||||
DB_NAME=
|
||||
DB_HOST=127.0.0.1
|
||||
DB_PORT=3301
|
||||
DB_NAME=carddrop
|
||||
DB_AUTH_USER=
|
||||
DB_AUTH_PASS=
|
||||
DB_SYNC=
|
||||
DB_LOGGING=
|
||||
DB_DATA_LOCATION=~/.docker
|
||||
DB_DATA_LOCATION=./.temp/database
|
||||
|
||||
DB_CARD_FILE=:memory:
|
||||
|
||||
EXPRESS_PORT=3303
|
||||
EXPRESS_PORT=3302
|
||||
|
||||
GDRIVESYNC_AUTO=true
|
|
@ -12,14 +12,15 @@ jobs:
|
|||
runs-on: node
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v1
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 18.x
|
||||
- run: npm ci
|
||||
- run: npm run build
|
||||
- run: npm test
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn build
|
||||
- run: yarn test
|
||||
- run: yarn lint
|
||||
|
||||
- name: "Copy files over to location"
|
||||
run: cp -r . ${{ secrets.PROD_REPO_PATH }}
|
||||
|
@ -53,12 +54,13 @@ jobs:
|
|||
DATA_DIR: ${{ secrets.PROD_DATA_DIR }}
|
||||
GDRIVESYNC_AUTO: ${{ vars.PROD_GDRIVESYNC_AUTO }}
|
||||
EXPRESS_PORT: ${{ secrets.PROD_EXPRESS_PORT }}
|
||||
BOT_LOGLEVEL: ${{ vars.PROD_BOT_LOGLEVEL }}
|
||||
with:
|
||||
host: ${{ secrets.PROD_SSH_HOST }}
|
||||
username: ${{ secrets.PROD_SSH_USER }}
|
||||
key: ${{ secrets.PROD_SSH_KEY }}
|
||||
port: ${{ secrets.PROD_SSH_PORT }}
|
||||
envs: DB_NAME,DB_AUTH_USER,DB_AUTH_PASS,DB_HOST,DB_PORT,DB_ROOT_HOST,DB_SYNC,DB_LOGGING,DB_DATA_LOCATION,BOT_TOKEN,BOT_VER,BOT_AUTHOR,BOT_OWNERID,BOT_CLIENTID,ABOUT_FUNDING,ABOUT_REPO,BOT_ENV,BOT_ADMINS,DATA_DIR,GDRIVESYNC_AUTO,SERVER_PATH,EXPRESS_PORT
|
||||
envs: DB_NAME,DB_AUTH_USER,DB_AUTH_PASS,DB_HOST,DB_PORT,DB_ROOT_HOST,DB_SYNC,DB_LOGGING,DB_DATA_LOCATION,BOT_TOKEN,BOT_VER,BOT_AUTHOR,BOT_OWNERID,BOT_CLIENTID,ABOUT_FUNDING,ABOUT_REPO,BOT_ENV,BOT_ADMINS,DATA_DIR,GDRIVESYNC_AUTO,SERVER_PATH,EXPRESS_PORT,BOT_LOGLEVEL
|
||||
script: |
|
||||
source .sshrc \
|
||||
&& cd /home/vylpes/apps/card-drop/card-drop_prod \
|
||||
|
|
|
@ -12,14 +12,15 @@ jobs:
|
|||
runs-on: node
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v1
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 18.x
|
||||
- run: npm ci
|
||||
- run: npm run build
|
||||
- run: npm test
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn build
|
||||
- run: yarn test
|
||||
- run: yarn lint
|
||||
|
||||
- name: "Copy files over to location"
|
||||
run: cp -r . ${{ secrets.STAGE_REPO_PATH }}
|
||||
|
@ -53,12 +54,13 @@ jobs:
|
|||
DATA_DIR: ${{ secrets.STAGE_DATA_DIR }}
|
||||
GDRIVESYNC_AUTO: ${{ vars.STAGE_GDRIVESYNC_AUTO }}
|
||||
EXPRESS_PORT: ${{ secrets.STAGE_EXPRESS_PORT }}
|
||||
BOT_LOGLEVEL: ${{ vars.STAGE_BOT_LOGLEVEL }}
|
||||
with:
|
||||
host: ${{ secrets.STAGE_SSH_HOST }}
|
||||
username: ${{ secrets.STAGE_SSH_USER }}
|
||||
key: ${{ secrets.STAGE_SSH_KEY }}
|
||||
port: ${{ secrets.STAGE_SSH_PORT }}
|
||||
envs: DB_NAME,DB_AUTH_USER,DB_AUTH_PASS,DB_HOST,DB_PORT,DB_ROOT_HOST,DB_SYNC,DB_LOGGING,DB_DATA_LOCATION,BOT_TOKEN,BOT_VER,BOT_AUTHOR,BOT_OWNERID,BOT_CLIENTID,ABOUT_FUNDING,ABOUT_REPO,BOT_ENV,BOT_ADMINS,DATA_DIR,GDRIVESYNC_AUTO,SERVER_PATH,EXPRESS_PORT
|
||||
envs: DB_NAME,DB_AUTH_USER,DB_AUTH_PASS,DB_HOST,DB_PORT,DB_ROOT_HOST,DB_SYNC,DB_LOGGING,DB_DATA_LOCATION,BOT_TOKEN,BOT_VER,BOT_AUTHOR,BOT_OWNERID,BOT_CLIENTID,ABOUT_FUNDING,ABOUT_REPO,BOT_ENV,BOT_ADMINS,DATA_DIR,GDRIVESYNC_AUTO,SERVER_PATH,EXPRESS_PORT,BOT_LOGLEVEL
|
||||
script: |
|
||||
source .sshrc \
|
||||
&& cd /home/vylpes/apps/card-drop/card-drop_stage \
|
||||
|
|
|
@ -14,11 +14,12 @@ jobs:
|
|||
runs-on: node
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v1
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 18.x
|
||||
- run: npm ci
|
||||
- run: npm run build
|
||||
- run: npm test
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn build
|
||||
- run: yarn test
|
||||
- run: yarn lint
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -108,5 +108,5 @@ config.json
|
|||
ormconfig.json
|
||||
gdrive-credentials.json
|
||||
data/
|
||||
*.db
|
||||
.temp/
|
||||
*.db
|
2
database/0.6/1715967355818-daily/Up/01-table/User.sql
Normal file
2
database/0.6/1715967355818-daily/Up/01-table/User.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE `user`
|
||||
ADD LastUsedDaily datetime null;
|
11694
package-lock.json
generated
11694
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "card-drop",
|
||||
"version": "0.5.2",
|
||||
"version": "0.6.4",
|
||||
"main": "./dist/bot.js",
|
||||
"typings": "./dist",
|
||||
"scripts": {
|
||||
|
@ -32,6 +32,7 @@
|
|||
"@types/uuid": "^9.0.0",
|
||||
"body-parser": "^1.20.2",
|
||||
"clone-deep": "^4.0.1",
|
||||
"cron": "^3.1.7",
|
||||
"discord.js": "^14.3.0",
|
||||
"dotenv": "^16.0.0",
|
||||
"express": "^4.18.2",
|
||||
|
|
|
@ -39,6 +39,13 @@ export default class CardMetadataFunction {
|
|||
CoreClient.Cards = cardResult.Result!;
|
||||
AppLogger.LogInfo("Functions/CardMetadataFunction", `Loaded ${CoreClient.Cards.flatMap(x => x.cards).length} cards to database`);
|
||||
|
||||
const duplicateCards = CoreClient.Cards.flatMap(x => x.cards)
|
||||
.filter((card, index, self) => self.findIndex(c => c.id === card.id) !== index);
|
||||
|
||||
if (duplicateCards.length > 0) {
|
||||
AppLogger.LogWarn("Functions/CardMetadataFunction", `Duplicate card ids found: ${duplicateCards.flatMap(x => x.id).join(", ")}`);
|
||||
}
|
||||
|
||||
return {
|
||||
IsSuccess: true,
|
||||
};
|
||||
|
|
|
@ -37,7 +37,6 @@ const client = new CoreClient([
|
|||
]);
|
||||
|
||||
Registry.RegisterCommands();
|
||||
Registry.RegisterEvents();
|
||||
Registry.RegisterButtonEvents();
|
||||
|
||||
if (!existsSync(`${process.env.DATA_DIR}/cards`) && process.env.GDRIVESYNC_AUTO && process.env.GDRIVESYNC_AUTO == "true") {
|
||||
|
|
|
@ -5,10 +5,13 @@ import { CoreClient } from "../client/client";
|
|||
import { default as eClaim } from "../database/entities/app/Claim";
|
||||
import AppLogger from "../client/appLogger";
|
||||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||
import User from "../database/entities/app/User";
|
||||
import CardConstants from "../constants/CardConstants";
|
||||
|
||||
export default class Claim extends ButtonEvent {
|
||||
public override async execute(interaction: ButtonInteraction) {
|
||||
if (!interaction.guild || !interaction.guildId) return;
|
||||
if (!interaction.channel) return;
|
||||
|
||||
await interaction.deferUpdate();
|
||||
|
||||
|
@ -19,18 +22,29 @@ export default class Claim extends ButtonEvent {
|
|||
|
||||
AppLogger.LogSilly("Button/Claim", `Parameters: cardNumber=${cardNumber}, claimId=${claimId}, droppedBy=${droppedBy}, userId=${userId}`);
|
||||
|
||||
const user = await User.FetchOneById(User, userId) || new User(userId, CardConstants.StartingCurrency);
|
||||
|
||||
AppLogger.LogSilly("Button/Claim", `${user.Id} has ${user.Currency} currency`);
|
||||
|
||||
if (!user.RemoveCurrency(CardConstants.ClaimCost)) {
|
||||
await interaction.channel.send(`${interaction.user}, Not enough currency! You need ${CardConstants.ClaimCost} currency, you have ${user.Currency}!`);
|
||||
return;
|
||||
}
|
||||
|
||||
const claimed = await eClaim.FetchOneByClaimId(claimId);
|
||||
|
||||
if (claimed) {
|
||||
await interaction.reply("This card has already been claimed");
|
||||
await interaction.channel.send(`${interaction.user}, This card has already been claimed!`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (claimId == CoreClient.ClaimId && userId != droppedBy) {
|
||||
await interaction.reply("The latest dropped card can only be claimed by the user who dropped it");
|
||||
await interaction.channel.send(`${interaction.user}, The latest dropped card can only be claimed by the user who dropped it!`);
|
||||
return;
|
||||
}
|
||||
|
||||
await user.Save(User, user);
|
||||
|
||||
let inventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
|
||||
|
||||
if (!inventory) {
|
||||
|
|
|
@ -8,6 +8,8 @@ import Config from "../database/entities/app/Config";
|
|||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||
import path from "path";
|
||||
import AppLogger from "../client/appLogger";
|
||||
import User from "../database/entities/app/User";
|
||||
import CardConstants from "../constants/CardConstants";
|
||||
|
||||
export default class Reroll extends ButtonEvent {
|
||||
public override async execute(interaction: ButtonInteraction) {
|
||||
|
@ -23,6 +25,20 @@ export default class Reroll extends ButtonEvent {
|
|||
return;
|
||||
}
|
||||
|
||||
let user = await User.FetchOneById(User, interaction.user.id);
|
||||
|
||||
if (!user) {
|
||||
user = new User(interaction.user.id, CardConstants.StartingCurrency);
|
||||
await user.Save(User, user);
|
||||
|
||||
AppLogger.LogInfo("Commands/Drop", `New user (${interaction.user.id}) saved to the database`);
|
||||
}
|
||||
|
||||
if (user.Currency < CardConstants.ClaimCost) {
|
||||
await interaction.reply(`Not enough currency! You need ${CardConstants.ClaimCost} currency, you have ${user.Currency}!`);
|
||||
return;
|
||||
}
|
||||
|
||||
const randomCard = CardDropHelperMetadata.GetRandomCard();
|
||||
|
||||
if (!randomCard) {
|
||||
|
|
157
src/buttonEvents/Sacrifice.ts
Normal file
157
src/buttonEvents/Sacrifice.ts
Normal file
|
@ -0,0 +1,157 @@
|
|||
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, EmbedBuilder } from "discord.js";
|
||||
import { ButtonEvent } from "../type/buttonEvent";
|
||||
import Inventory from "../database/entities/app/Inventory";
|
||||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||
import { CardRarityToString, GetSacrificeAmount } from "../constants/CardRarity";
|
||||
import EmbedColours from "../constants/EmbedColours";
|
||||
import User from "../database/entities/app/User";
|
||||
|
||||
export default class Sacrifice extends ButtonEvent {
|
||||
public override async execute(interaction: ButtonInteraction) {
|
||||
const subcommand = interaction.customId.split(" ")[1];
|
||||
|
||||
switch(subcommand) {
|
||||
case "confirm":
|
||||
await this.confirm(interaction);
|
||||
break;
|
||||
case "cancel":
|
||||
await this.cancel(interaction);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async confirm(interaction: ButtonInteraction) {
|
||||
const userId = interaction.customId.split(" ")[2];
|
||||
const cardNumber = interaction.customId.split(" ")[3];
|
||||
|
||||
if (userId != interaction.user.id) {
|
||||
await interaction.reply("Only the user who created this sacrifice can confirm it.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
|
||||
|
||||
if (!cardInInventory) {
|
||||
await interaction.reply("Unable to find card in inventory.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
|
||||
|
||||
if (!cardData) {
|
||||
await interaction.reply("Unable to find card in the database.");
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await User.FetchOneById(User, userId);
|
||||
|
||||
if (!user) {
|
||||
await interaction.reply("Unable to find user in database.");
|
||||
return;
|
||||
}
|
||||
|
||||
cardInInventory.RemoveQuantity(1);
|
||||
|
||||
await cardInInventory.Save(Inventory, cardInInventory);
|
||||
|
||||
const cardValue = GetSacrificeAmount(cardData.card.type);
|
||||
const cardRarityString = CardRarityToString(cardData.card.type);
|
||||
|
||||
user.AddCurrency(cardValue);
|
||||
|
||||
await user.Save(User, user);
|
||||
|
||||
const description = [
|
||||
`Card: ${cardData.card.name}`,
|
||||
`Series: ${cardData.series.name}`,
|
||||
`Rarity: ${cardRarityString}`,
|
||||
`Quantity Owned: ${cardInInventory.Quantity}`,
|
||||
`Sacrifice Amount: ${cardValue}`,
|
||||
];
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("Card Sacrificed")
|
||||
.setDescription(description.join("\n"))
|
||||
.setColor(EmbedColours.Green)
|
||||
.setFooter({ text: `${interaction.user.username}` });
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents([
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`sacrifice confirm ${interaction.user.id} ${cardNumber}`)
|
||||
.setLabel("Confirm")
|
||||
.setStyle(ButtonStyle.Success)
|
||||
.setDisabled(true),
|
||||
new ButtonBuilder()
|
||||
.setCustomId("sacrifice cancel")
|
||||
.setLabel("Cancel")
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setDisabled(true),
|
||||
]);
|
||||
|
||||
await interaction.update({
|
||||
embeds: [ embed ],
|
||||
components: [ row ],
|
||||
});
|
||||
}
|
||||
|
||||
private async cancel(interaction: ButtonInteraction) {
|
||||
const userId = interaction.customId.split(" ")[2];
|
||||
const cardNumber = interaction.customId.split(" ")[3];
|
||||
|
||||
if (userId != interaction.user.id) {
|
||||
await interaction.reply("Only the user who created this sacrifice can cancel it.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
|
||||
|
||||
if (!cardInInventory) {
|
||||
await interaction.reply("Unable to find card in inventory.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
|
||||
|
||||
if (!cardData) {
|
||||
await interaction.reply("Unable to find card in the database.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardValue = GetSacrificeAmount(cardData.card.type);
|
||||
const cardRarityString = CardRarityToString(cardData.card.type);
|
||||
|
||||
const description = [
|
||||
`Card: ${cardData.card.name}`,
|
||||
`Series: ${cardData.series.name}`,
|
||||
`Rarity: ${cardRarityString}`,
|
||||
`Quantity Owned: ${cardInInventory.Quantity}`,
|
||||
`Sacrifice Amount: ${cardValue}`,
|
||||
];
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("Sacrifice Cancelled")
|
||||
.setDescription(description.join("\n"))
|
||||
.setColor(EmbedColours.Grey)
|
||||
.setFooter({ text: `${interaction.user.username}` });
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents([
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`sacrifice confirm ${interaction.user.id} ${cardNumber}`)
|
||||
.setLabel("Confirm")
|
||||
.setStyle(ButtonStyle.Success)
|
||||
.setDisabled(true),
|
||||
new ButtonBuilder()
|
||||
.setCustomId("sacrifice cancel")
|
||||
.setLabel("Cancel")
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setDisabled(true),
|
||||
]);
|
||||
|
||||
await interaction.update({
|
||||
embeds: [ embed ],
|
||||
components: [ row ],
|
||||
});
|
||||
}
|
||||
}
|
|
@ -22,14 +22,14 @@ export default class Trade extends ButtonEvent {
|
|||
}
|
||||
|
||||
private async AcceptTrade(interaction: ButtonInteraction) {
|
||||
const giveUserId = interaction.customId.split(" ")[2];
|
||||
const receiveUserId = interaction.customId.split(" ")[3];
|
||||
const giveCardNumber = interaction.customId.split(" ")[4];
|
||||
const receiveCardNumber = interaction.customId.split(" ")[5];
|
||||
const user1UserId = interaction.customId.split(" ")[2];
|
||||
const user2UserId = interaction.customId.split(" ")[3];
|
||||
const user1CardNumber = interaction.customId.split(" ")[4];
|
||||
const user2CardNumber = interaction.customId.split(" ")[5];
|
||||
const expiry = interaction.customId.split(" ")[6];
|
||||
const timeoutId = interaction.customId.split(" ")[7];
|
||||
|
||||
AppLogger.LogSilly("Button/Trade/AcceptTrade", `Parameters: giveUserId=${giveUserId}, receiveUserId=${receiveUserId}, giveCardNumber=${giveCardNumber}, receiveCardNumber=${receiveCardNumber}, expiry=${expiry}, timeoutId=${timeoutId}`);
|
||||
AppLogger.LogSilly("Button/Trade/AcceptTrade", `Parameters: user1UserId=${user1UserId}, user2UserId=${user2UserId}, user1CardNumber=${user1CardNumber}, user2CardNumber=${user2CardNumber}, expiry=${expiry}, timeoutId=${timeoutId}`);
|
||||
|
||||
const expiryDate = new Date(expiry);
|
||||
|
||||
|
@ -38,80 +38,80 @@ export default class Trade extends ButtonEvent {
|
|||
return;
|
||||
}
|
||||
|
||||
if (interaction.user.id !== receiveUserId) {
|
||||
if (interaction.user.id !== user2UserId) {
|
||||
await interaction.reply("You are not the user who the trade is intended for");
|
||||
return;
|
||||
}
|
||||
|
||||
const giveItem = CoreClient.Cards
|
||||
const user1Item = CoreClient.Cards
|
||||
.flatMap(x => x.cards)
|
||||
.find(x => x.id === giveCardNumber);
|
||||
.find(x => x.id === user1CardNumber);
|
||||
|
||||
const receiveItem = CoreClient.Cards
|
||||
const user2Item = CoreClient.Cards
|
||||
.flatMap(x => x.cards)
|
||||
.find(x => x.id === receiveCardNumber);
|
||||
.find(x => x.id === user2CardNumber);
|
||||
|
||||
if (!giveItem || !receiveItem) {
|
||||
if (!user1Item || !user2Item) {
|
||||
await interaction.reply("One or more of the items you are trying to trade does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
const giveUser = interaction.client.users.cache.get(giveUserId) || await interaction.client.users.fetch(giveUserId);
|
||||
const receiveUser = interaction.client.users.cache.get(receiveUserId) || await interaction.client.users.fetch(receiveUserId);
|
||||
const user1User = interaction.client.users.cache.get(user1UserId) || await interaction.client.users.fetch(user1UserId);
|
||||
const user2User = interaction.client.users.cache.get(user2UserId) || await interaction.client.users.fetch(user2UserId);
|
||||
|
||||
const giveUserInventory1 = await Inventory.FetchOneByCardNumberAndUserId(giveUserId, giveCardNumber);
|
||||
const receiveUserInventory1 = await Inventory.FetchOneByCardNumberAndUserId(receiveUserId, receiveCardNumber);
|
||||
const user1UserInventory1 = await Inventory.FetchOneByCardNumberAndUserId(user1UserId, user1CardNumber);
|
||||
const user2UserInventory1 = await Inventory.FetchOneByCardNumberAndUserId(user2UserId, user2CardNumber);
|
||||
|
||||
if (!giveUserInventory1 || !receiveUserInventory1) {
|
||||
if (!user1UserInventory1 || !user2UserInventory1) {
|
||||
await interaction.reply("One or more of the items you are trying to trade does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (giveUserInventory1.Quantity < 1 || receiveUserInventory1.Quantity < 1) {
|
||||
if (user1UserInventory1.Quantity < 1 || user2UserInventory1.Quantity < 1) {
|
||||
await interaction.reply("One or more of the items you are trying to trade does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
giveUserInventory1.SetQuantity(giveUserInventory1.Quantity - 1);
|
||||
receiveUserInventory1.SetQuantity(receiveUserInventory1.Quantity - 1);
|
||||
user1UserInventory1.SetQuantity(user1UserInventory1.Quantity - 1);
|
||||
user2UserInventory1.SetQuantity(user2UserInventory1.Quantity - 1);
|
||||
|
||||
await giveUserInventory1.Save(Inventory, giveUserInventory1);
|
||||
await receiveUserInventory1.Save(Inventory, receiveUserInventory1);
|
||||
await user1UserInventory1.Save(Inventory, user1UserInventory1);
|
||||
await user2UserInventory1.Save(Inventory, user2UserInventory1);
|
||||
|
||||
let giveUserInventory2 = await Inventory.FetchOneByCardNumberAndUserId(receiveUserId, giveCardNumber);
|
||||
let receiveUserInventory2 = await Inventory.FetchOneByCardNumberAndUserId(giveUserId, receiveCardNumber);
|
||||
let user1UserInventory2 = await Inventory.FetchOneByCardNumberAndUserId(user1UserId, user2CardNumber);
|
||||
let user2UserInventory2 = await Inventory.FetchOneByCardNumberAndUserId(user2UserId, user1CardNumber);
|
||||
|
||||
if (!giveUserInventory2) {
|
||||
giveUserInventory2 = new Inventory(receiveUserId, giveCardNumber, 1);
|
||||
if (!user1UserInventory2) {
|
||||
user1UserInventory2 = new Inventory(user1UserId, user1CardNumber, 1);
|
||||
} else {
|
||||
giveUserInventory2.SetQuantity(giveUserInventory2.Quantity + 1);
|
||||
user1UserInventory2.SetQuantity(user1UserInventory2.Quantity + 1);
|
||||
}
|
||||
|
||||
if (!receiveUserInventory2) {
|
||||
receiveUserInventory2 = new Inventory(giveUserId, receiveCardNumber, 1);
|
||||
if (!user2UserInventory2) {
|
||||
user2UserInventory2 = new Inventory(user2UserId, user2CardNumber, 1);
|
||||
} else {
|
||||
receiveUserInventory2.SetQuantity(receiveUserInventory2.Quantity + 1);
|
||||
user2UserInventory2.SetQuantity(user2UserInventory2.Quantity + 1);
|
||||
}
|
||||
|
||||
await giveUserInventory2.Save(Inventory, giveUserInventory2);
|
||||
await receiveUserInventory2.Save(Inventory, receiveUserInventory2);
|
||||
await user1UserInventory2.Save(Inventory, user1UserInventory2);
|
||||
await user2UserInventory2.Save(Inventory, user2UserInventory2);
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const tradeEmbed = new EmbedBuilder()
|
||||
.setTitle("Trade Accepted")
|
||||
.setDescription(`Trade initiated between ${receiveUser.username} and ${giveUser.username}`)
|
||||
.setDescription(`Trade initiated between ${user1User.username} and ${user2User.username}`)
|
||||
.setColor(EmbedColours.Success)
|
||||
.setImage("https://i.imgur.com/9w5f1ls.gif")
|
||||
.addFields([
|
||||
{
|
||||
name: "I receieve",
|
||||
value: `${receiveItem.id}: ${receiveItem.name}`,
|
||||
name: `${user1User.username} Receives`,
|
||||
value: `${user2Item.id}: ${user2Item.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: "You receieve",
|
||||
value: `${giveItem.id}: ${giveItem.name}`,
|
||||
name: `${user2User.username} Receives`,
|
||||
value: `${user1Item.id}: ${user1Item.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
|
@ -138,32 +138,32 @@ export default class Trade extends ButtonEvent {
|
|||
}
|
||||
|
||||
private async DeclineTrade(interaction: ButtonInteraction) {
|
||||
const giveUserId = interaction.customId.split(" ")[2];
|
||||
const receiveUserId = interaction.customId.split(" ")[3];
|
||||
const giveCardNumber = interaction.customId.split(" ")[4];
|
||||
const receiveCardNumber = interaction.customId.split(" ")[5];
|
||||
const user1UserId = interaction.customId.split(" ")[2];
|
||||
const user2UserId = interaction.customId.split(" ")[3];
|
||||
const user1CardNumber = interaction.customId.split(" ")[4];
|
||||
const user2CardNumber = interaction.customId.split(" ")[5];
|
||||
// No need to get expiry date
|
||||
const timeoutId = interaction.customId.split(" ")[7];
|
||||
|
||||
AppLogger.LogSilly("Button/Trade/DeclineTrade", `Parameters: giveUserId=${giveUserId}, receiveUserId=${receiveUserId}, giveCardNumber=${giveCardNumber}, receiveCardNumber=${receiveCardNumber}, timeoutId=${timeoutId}`);
|
||||
AppLogger.LogSilly("Button/Trade/DeclineTrade", `Parameters: user1UserId=${user1UserId}, user2UserId=${user2UserId}, user1CardNumber=${user1CardNumber}, user2CardNumber=${user2CardNumber}, timeoutId=${timeoutId}`);
|
||||
|
||||
if (interaction.user.id != receiveUserId && interaction.user.id !==giveUserId) {
|
||||
if (interaction.user.id != user1UserId && interaction.user.id !== user2UserId) {
|
||||
await interaction.reply("You are not the user who the trade is intended for");
|
||||
return;
|
||||
}
|
||||
|
||||
const giveUser = interaction.client.users.cache.get(giveUserId) || await interaction.client.users.fetch(giveUserId);
|
||||
const receiveUser = interaction.client.users.cache.get(receiveUserId) || await interaction.client.users.fetch(receiveUserId);
|
||||
const user1User = interaction.client.users.cache.get(user1UserId) || await interaction.client.users.fetch(user1UserId);
|
||||
const user2User = interaction.client.users.cache.get(user2UserId) || await interaction.client.users.fetch(user2UserId);
|
||||
|
||||
const giveItem = CoreClient.Cards
|
||||
const user1Item = CoreClient.Cards
|
||||
.flatMap(x => x.cards)
|
||||
.find(x => x.id === giveCardNumber);
|
||||
.find(x => x.id === user1CardNumber);
|
||||
|
||||
const receiveItem = CoreClient.Cards
|
||||
const user2Item = CoreClient.Cards
|
||||
.flatMap(x => x.cards)
|
||||
.find(x => x.id === receiveCardNumber);
|
||||
.find(x => x.id === user2CardNumber);
|
||||
|
||||
if (!giveItem || !receiveItem) {
|
||||
if (!user1Item || !user2Item) {
|
||||
await interaction.reply("One or more of the items you are trying to trade does not exist.");
|
||||
return;
|
||||
}
|
||||
|
@ -172,18 +172,18 @@ export default class Trade extends ButtonEvent {
|
|||
|
||||
const tradeEmbed = new EmbedBuilder()
|
||||
.setTitle("Trade Declined")
|
||||
.setDescription(`Trade initiated between ${receiveUser.username} and ${giveUser.username}`)
|
||||
.setDescription(`Trade initiated between ${user1User.username} and ${user2User.username}`)
|
||||
.setColor(EmbedColours.Error)
|
||||
.setImage("https://i.imgur.com/9w5f1ls.gif")
|
||||
.addFields([
|
||||
{
|
||||
name: "I Receive",
|
||||
value: `${receiveItem.id}: ${receiveItem.name}`,
|
||||
name: `${user1User.username} Receives`,
|
||||
value: `${user2Item.id}: ${user2Item.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: "You Receive",
|
||||
value: `${giveItem.id}: ${giveItem.name}`,
|
||||
name: `${user2User.username} Receives`,
|
||||
value: `${user1Item.id}: ${user1Item.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
|
|
|
@ -14,6 +14,8 @@ import Webhooks from "../webhooks";
|
|||
import CardMetadataFunction from "../Functions/CardMetadataFunction";
|
||||
import { SeriesMetadata } from "../contracts/SeriesMetadata";
|
||||
import AppLogger from "./appLogger";
|
||||
import TimerHelper from "../helpers/TimerHelper";
|
||||
import GiveCurrency from "../timers/GiveCurrency";
|
||||
|
||||
export class CoreClient extends Client {
|
||||
private static _commandItems: ICommandItem[];
|
||||
|
@ -23,6 +25,7 @@ export class CoreClient extends Client {
|
|||
private _events: Events;
|
||||
private _util: Util;
|
||||
private _webhooks: Webhooks;
|
||||
private _timerHelper: TimerHelper;
|
||||
|
||||
public static ClaimId: string;
|
||||
public static Environment: Environment;
|
||||
|
@ -59,6 +62,7 @@ export class CoreClient extends Client {
|
|||
this._events = new Events();
|
||||
this._util = new Util();
|
||||
this._webhooks = new Webhooks();
|
||||
this._timerHelper = new TimerHelper();
|
||||
|
||||
AppLogger.LogInfo("Client", `Environment: ${CoreClient.Environment}`);
|
||||
|
||||
|
@ -72,7 +76,12 @@ export class CoreClient extends Client {
|
|||
}
|
||||
|
||||
await AppDataSource.initialize()
|
||||
.then(() => AppLogger.LogInfo("Client", "App Data Source Initialised"))
|
||||
.then(() => {
|
||||
AppLogger.LogInfo("Client", "App Data Source Initialised");
|
||||
|
||||
const timerId = this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false);
|
||||
this._timerHelper.StartTimer(timerId);
|
||||
})
|
||||
.catch(err => {
|
||||
AppLogger.LogError("Client", "App Data Source Initialisation Failed");
|
||||
AppLogger.LogError("Client", err);
|
||||
|
|
|
@ -2,11 +2,14 @@ import { Interaction } from "discord.js";
|
|||
import ChatInputCommand from "./interactionCreate/ChatInputCommand";
|
||||
import Button from "./interactionCreate/Button";
|
||||
import AppLogger from "./appLogger";
|
||||
import NewUserDiscovery from "./interactionCreate/middleware/NewUserDiscovery";
|
||||
|
||||
export class Events {
|
||||
public async onInteractionCreate(interaction: Interaction) {
|
||||
if (!interaction.guildId) return;
|
||||
|
||||
await NewUserDiscovery(interaction);
|
||||
|
||||
if (interaction.isChatInputCommand()) {
|
||||
AppLogger.LogVerbose("Client", `ChatInputCommand: ${interaction.commandName}`);
|
||||
ChatInputCommand.onChatInput(interaction);
|
||||
|
|
15
src/client/interactionCreate/middleware/NewUserDiscovery.ts
Normal file
15
src/client/interactionCreate/middleware/NewUserDiscovery.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { Interaction } from "discord.js";
|
||||
import User from "../../../database/entities/app/User";
|
||||
import CardConstants from "../../../constants/CardConstants";
|
||||
import AppLogger from "../../appLogger";
|
||||
|
||||
export default async function NewUserDiscovery(interaction: Interaction) {
|
||||
const existingUser = await User.FetchOneById(User, interaction.user.id);
|
||||
|
||||
if (existingUser) return;
|
||||
|
||||
const newUser = new User(interaction.user.id, CardConstants.StartingCurrency);
|
||||
await newUser.Save(User, newUser);
|
||||
|
||||
AppLogger.LogInfo("NewUserDiscovery", `Discovered new user ${interaction.user.id}`);
|
||||
}
|
28
src/commands/balance.ts
Normal file
28
src/commands/balance.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { CommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../type/command";
|
||||
import User from "../database/entities/app/User";
|
||||
import EmbedColours from "../constants/EmbedColours";
|
||||
|
||||
export default class Balance extends Command {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.CommandBuilder = new SlashCommandBuilder()
|
||||
.setName("balance")
|
||||
.setDescription("Get your currency balance");
|
||||
}
|
||||
|
||||
public override async execute(interaction: CommandInteraction) {
|
||||
const user = await User.FetchOneById(User, interaction.user.id);
|
||||
|
||||
const userBalance = user != null ? user.Currency : 0;
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(EmbedColours.Ok)
|
||||
.setTitle("Balance")
|
||||
.setDescription(`You currently have **${userBalance} currency**!`)
|
||||
.setFooter({ text: interaction.user.username, iconURL: interaction.user.avatarURL() ?? undefined });
|
||||
|
||||
await interaction.reply({ embeds: [ embed ]});
|
||||
}
|
||||
}
|
43
src/commands/daily.ts
Normal file
43
src/commands/daily.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../type/command";
|
||||
import User from "../database/entities/app/User";
|
||||
import CardConstants from "../constants/CardConstants";
|
||||
import TimeLengthInput from "../helpers/TimeLengthInput";
|
||||
|
||||
export default class Daily extends Command {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.CommandBuilder = new SlashCommandBuilder()
|
||||
.setName("daily")
|
||||
.setDescription("Gain bonus currency, once a day");
|
||||
}
|
||||
|
||||
public override async execute(interaction: CommandInteraction) {
|
||||
const user = await User.FetchOneById(User, interaction.user.id) ?? new User(interaction.user.id, CardConstants.StartingCurrency);
|
||||
|
||||
const dayAgo = new Date(Date.now() - (1000 * 60 * 60 * 24));
|
||||
|
||||
if (user.LastUsedDaily && user.LastUsedDaily > dayAgo) {
|
||||
const timeNow = Date.now();
|
||||
const timeLength = 24 * 60 * 60 * 1000; // 1 day
|
||||
|
||||
const timeLeft = Math.ceil(((timeLength - (timeNow - user.LastUsedDaily.getTime()))) / 1000 / 60);
|
||||
|
||||
const timeLeftHours = Math.floor(timeLeft / 60);
|
||||
const timeLeftMinutes = timeLeft % 60;
|
||||
|
||||
const timeLeftString = new TimeLengthInput(`${timeLeftHours}h ${timeLeftMinutes}m`);
|
||||
|
||||
await interaction.reply(`You have already used the daily command! You can use it again in **${timeLeftString.GetLength()}**.`);
|
||||
return;
|
||||
}
|
||||
|
||||
user.AddCurrency(CardConstants.DailyCurrency);
|
||||
user.UpdateLastUsedDaily(new Date());
|
||||
|
||||
await user.Save(User, user);
|
||||
|
||||
await interaction.reply(`Congratulations, you have claimed your daily ${CardConstants.DailyCurrency} currency! You now have ${user.Currency} currency and can claim again in 24 hours!`);
|
||||
}
|
||||
}
|
|
@ -8,6 +8,8 @@ import Config from "../database/entities/app/Config";
|
|||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||
import path from "path";
|
||||
import AppLogger from "../client/appLogger";
|
||||
import User from "../database/entities/app/User";
|
||||
import CardConstants from "../constants/CardConstants";
|
||||
|
||||
export default class Drop extends Command {
|
||||
constructor() {
|
||||
|
@ -31,6 +33,20 @@ export default class Drop extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
let user = await User.FetchOneById(User, interaction.user.id);
|
||||
|
||||
if (!user) {
|
||||
user = new User(interaction.user.id, CardConstants.StartingCurrency);
|
||||
await user.Save(User, user);
|
||||
|
||||
AppLogger.LogInfo("Commands/Drop", `New user (${interaction.user.id}) saved to the database`);
|
||||
}
|
||||
|
||||
if (user.Currency < CardConstants.ClaimCost) {
|
||||
await interaction.reply(`Not enough currency! You need ${CardConstants.ClaimCost} currency, you have ${user.Currency}!`);
|
||||
return;
|
||||
}
|
||||
|
||||
const randomCard = CardDropHelperMetadata.GetRandomCard();
|
||||
|
||||
if (!randomCard) {
|
||||
|
|
|
@ -5,6 +5,7 @@ import Config from "../database/entities/app/Config";
|
|||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||
import Inventory from "../database/entities/app/Inventory";
|
||||
import AppLogger from "../client/appLogger";
|
||||
import User from "../database/entities/app/User";
|
||||
|
||||
export default class Give extends Command {
|
||||
constructor() {
|
||||
|
@ -14,19 +15,57 @@ export default class Give extends Command {
|
|||
.setName("give")
|
||||
.setDescription("Give a user a card manually, in case bot breaks")
|
||||
.setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator)
|
||||
.addStringOption(x =>
|
||||
.addSubcommand(x =>
|
||||
x
|
||||
.setName("cardnumber")
|
||||
.setDescription("G")
|
||||
.setRequired(true))
|
||||
.addUserOption(x =>
|
||||
.setName("card")
|
||||
.setDescription("Give a user a card manually")
|
||||
.addStringOption(x =>
|
||||
x
|
||||
.setName("cardnumber")
|
||||
.setDescription("The card to give")
|
||||
.setRequired(true))
|
||||
.addUserOption(x =>
|
||||
x
|
||||
.setName("user")
|
||||
.setDescription("The user to give the card to")
|
||||
.setRequired(true)))
|
||||
.addSubcommand(x =>
|
||||
x
|
||||
.setName("user")
|
||||
.setDescription("The user to give the card to")
|
||||
.setRequired(true));
|
||||
.setName("currency")
|
||||
.setDescription("Give a user currency manually")
|
||||
.addNumberOption(x =>
|
||||
x
|
||||
.setName("amount")
|
||||
.setDescription("The amount to give")
|
||||
.setRequired(true))
|
||||
.addUserOption(x =>
|
||||
x
|
||||
.setName("user")
|
||||
.setDescription("The user to give the currency to")
|
||||
.setRequired(true)));
|
||||
}
|
||||
|
||||
public override async execute(interaction: CommandInteraction<CacheType>) {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
const whitelistedUsers = process.env.BOT_ADMINS!.split(",");
|
||||
|
||||
if (!whitelistedUsers.find(x => x == interaction.user.id)) {
|
||||
await interaction.reply("Only whitelisted users can use this command.");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (interaction.options.getSubcommand()) {
|
||||
case "card":
|
||||
await this.GiveCard(interaction);
|
||||
break;
|
||||
case "currency":
|
||||
await this.GiveCurrency(interaction);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async GiveCard(interaction: CommandInteraction) {
|
||||
if (!CoreClient.AllowDrops) {
|
||||
await interaction.reply("Bot is currently syncing, please wait until its done.");
|
||||
return;
|
||||
|
@ -37,17 +76,10 @@ export default class Give extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
const whitelistedUsers = process.env.BOT_ADMINS!.split(",");
|
||||
|
||||
if (!whitelistedUsers.find(x => x == interaction.user.id)) {
|
||||
await interaction.reply("Only whitelisted users can use this command.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardNumber = interaction.options.get("cardnumber", true);
|
||||
const user = interaction.options.getUser("user", true);
|
||||
const user = interaction.options.get("user", true).user!;
|
||||
|
||||
AppLogger.LogSilly("Commands/Give", `Parameters: cardNumber=${cardNumber.value}, user=${user.id}`);
|
||||
AppLogger.LogSilly("Commands/Give/GiveCard", `Parameters: cardNumber=${cardNumber.value}, user=${user.id}`);
|
||||
|
||||
const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber.value!.toString());
|
||||
|
||||
|
@ -66,6 +98,21 @@ export default class Give extends Command {
|
|||
|
||||
await inventory.Save(Inventory, inventory);
|
||||
|
||||
await interaction.reply(`${card.card.name} given to ${user.username}, they now have ${inventory.Quantity}`);
|
||||
await interaction.reply(`Card ${card.card.name} given to ${user.username}, they now have ${inventory.Quantity}`);
|
||||
}
|
||||
|
||||
private async GiveCurrency(interaction: CommandInteraction) {
|
||||
const amount = interaction.options.get("amount", true);
|
||||
const user = interaction.options.get("user", true).user!;
|
||||
|
||||
AppLogger.LogSilly("Commands/Give/GiveCurrency", `Parameters: amount=${amount.value} user=${user.id}`);
|
||||
|
||||
const userEntity = await User.FetchOneById(User, user.id) || new User(user.id, 300);
|
||||
|
||||
userEntity.AddCurrency(amount.value! as number);
|
||||
|
||||
await userEntity.Save(User, userEntity);
|
||||
|
||||
await interaction.reply(`${amount.value} currency ${amount.value! as number >= 0 ? "given to" : "taken from"} ${user.username}, they now have ${userEntity.Currency}`);
|
||||
}
|
||||
}
|
|
@ -22,7 +22,9 @@ export default class Inventory extends Command {
|
|||
|
||||
public override async execute(interaction: CommandInteraction) {
|
||||
const page = interaction.options.get("page");
|
||||
const user = interaction.options.getUser("user") || interaction.user;
|
||||
const userOption = interaction.options.get("user");
|
||||
|
||||
const user = userOption ? userOption.user! : interaction.user;
|
||||
|
||||
AppLogger.LogSilly("Commands/Inventory", `Parameters: page=${page?.value}, user=${user.id}`);
|
||||
|
||||
|
|
73
src/commands/sacrifice.ts
Normal file
73
src/commands/sacrifice.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CacheType, CommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../type/command";
|
||||
import Inventory from "../database/entities/app/Inventory";
|
||||
import { CardRarityToString, GetSacrificeAmount } from "../constants/CardRarity";
|
||||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||
import EmbedColours from "../constants/EmbedColours";
|
||||
|
||||
export default class Sacrifice extends Command {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.CommandBuilder = new SlashCommandBuilder()
|
||||
.setName("sacrifice")
|
||||
.setDescription("Sacrifices a card for currency")
|
||||
.addStringOption(x =>
|
||||
x
|
||||
.setName("cardnumber")
|
||||
.setDescription("The card to sacrifice from your inventory")
|
||||
.setRequired(true));
|
||||
}
|
||||
|
||||
public override async execute(interaction: CommandInteraction<CacheType>): Promise<void> {
|
||||
const cardnumber = interaction.options.get("cardnumber", true);
|
||||
|
||||
const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, cardnumber.value! as string);
|
||||
|
||||
if (!cardInInventory || cardInInventory.Quantity == 0) {
|
||||
await interaction.reply("Unable to find card in your inventory.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardnumber.value! as string);
|
||||
|
||||
if (!cardData) {
|
||||
await interaction.reply("Unable to find card in the database.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cardValue = GetSacrificeAmount(cardData.card.type);
|
||||
const cardRarityString = CardRarityToString(cardData.card.type);
|
||||
|
||||
const description = [
|
||||
`Card: ${cardData.card.name}`,
|
||||
`Series: ${cardData.series.name}`,
|
||||
`Rarity: ${cardRarityString}`,
|
||||
`Quantity Owned: ${cardInInventory.Quantity}`,
|
||||
`Sacrifice Amount: ${cardValue}`,
|
||||
];
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("Sacrifice")
|
||||
.setDescription(description.join("\n"))
|
||||
.setColor(EmbedColours.Error)
|
||||
.setFooter({ text: `${interaction.user.username}` });
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents([
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`sacrifice confirm ${interaction.user.id} ${cardnumber.value!}`)
|
||||
.setLabel("Confirm")
|
||||
.setStyle(ButtonStyle.Success),
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`sacrifice cancel ${interaction.user.id} ${cardnumber.value!}`)
|
||||
.setLabel("Cancel")
|
||||
.setStyle(ButtonStyle.Secondary),
|
||||
]);
|
||||
|
||||
await interaction.reply({
|
||||
embeds: [ embed ],
|
||||
components: [ row ],
|
||||
});
|
||||
}
|
||||
}
|
|
@ -30,34 +30,39 @@ export default class Trade extends Command {
|
|||
}
|
||||
|
||||
public override async execute(interaction: CommandInteraction) {
|
||||
const user = interaction.options.getUser("user")!;
|
||||
const give = interaction.options.get("give")!;
|
||||
const receive = interaction.options.get("receive")!;
|
||||
const user = interaction.options.get("user", true).user!;
|
||||
const give = interaction.options.get("give", true);
|
||||
const receive = interaction.options.get("receive", true);
|
||||
|
||||
AppLogger.LogSilly("Commands/Trade", `Parameters: user=${user.id}, give=${give.value}, receive=${receive.value}`);
|
||||
|
||||
const giveItemEntity = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, give.value!.toString());
|
||||
const receiveItemEntity = await Inventory.FetchOneByCardNumberAndUserId(user.id, receive.value!.toString());
|
||||
if (interaction.user.id == user.id) {
|
||||
await interaction.reply("You can not create a trade with yourself.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!giveItemEntity) {
|
||||
const user1ItemEntity = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, give.value!.toString());
|
||||
const user2ItemEntity = await Inventory.FetchOneByCardNumberAndUserId(user.id, receive.value!.toString());
|
||||
|
||||
if (!user1ItemEntity) {
|
||||
await interaction.reply("You do not have the item you are trying to trade.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!receiveItemEntity) {
|
||||
if (!user2ItemEntity) {
|
||||
await interaction.reply("The user you are trying to trade with does not have the item you are trying to trade for.");
|
||||
return;
|
||||
}
|
||||
|
||||
const giveItem = CoreClient.Cards
|
||||
const user1Item = CoreClient.Cards
|
||||
.flatMap(x => x.cards)
|
||||
.find(x => x.id === give.value!.toString());
|
||||
|
||||
const receiveItem = CoreClient.Cards
|
||||
const user2Item = CoreClient.Cards
|
||||
.flatMap(x => x.cards)
|
||||
.find(x => x.id === receive.value!.toString());
|
||||
|
||||
if (!giveItem || !receiveItem) {
|
||||
if (!user1Item || !user2Item) {
|
||||
await interaction.reply("One or more of the items you are trying to trade does not exist.");
|
||||
return;
|
||||
}
|
||||
|
@ -72,13 +77,13 @@ export default class Trade extends Command {
|
|||
.setImage("https://media1.tenor.com/m/KkZwKl2AQ2QAAAAd/trade-offer.gif")
|
||||
.addFields([
|
||||
{
|
||||
name: "I Receive",
|
||||
value: `${receiveItem.id}: ${receiveItem.name}`,
|
||||
name: `${interaction.user.username} Receives`,
|
||||
value: `${user2Item.id}: ${user2Item.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: "You Receive",
|
||||
value: `${giveItem.id}: ${giveItem.name}`,
|
||||
name: `${user.username} Receives`,
|
||||
value: `${user1Item.id}: ${user1Item.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
|
@ -87,16 +92,16 @@ export default class Trade extends Command {
|
|||
}
|
||||
]);
|
||||
|
||||
const timeoutId = setTimeout(async () => this.autoDecline(interaction, interaction.user.username, user.username, giveItem.id, receiveItem.id, giveItem.name, receiveItem.name), 1000 * 60 * 15); // 15 minutes
|
||||
const timeoutId = setTimeout(async () => this.autoDecline(interaction, interaction.user.username, user.username, user1Item.id, user2Item.id, user1Item.name, user2Item.name), 1000 * 60 * 15); // 15 minutes
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents([
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`trade accept ${interaction.user.id} ${user.id} ${giveItem.id} ${receiveItem.id} ${expiry} ${timeoutId}`)
|
||||
.setCustomId(`trade accept ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId}`)
|
||||
.setLabel("Accept")
|
||||
.setStyle(ButtonStyle.Success),
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`trade decline ${interaction.user.id} ${user.id} ${giveItem.id} ${receiveItem.id} ${expiry} ${timeoutId}`)
|
||||
.setCustomId(`trade decline ${interaction.user.id} ${user.id} ${user1Item.id} ${user2Item.id} ${expiry} ${timeoutId}`)
|
||||
.setLabel("Decline")
|
||||
.setStyle(ButtonStyle.Danger),
|
||||
]);
|
||||
|
@ -104,23 +109,23 @@ export default class Trade extends Command {
|
|||
await interaction.reply({ content: `${user}`, embeds: [ tradeEmbed ], components: [ row ] });
|
||||
}
|
||||
|
||||
private async autoDecline(interaction: CommandInteraction, giveUsername: string, receiveUsername: string, giveCardNumber: string, receiveCardNumber: string, giveCardName: string, receiveCardName: string) {
|
||||
AppLogger.LogSilly("Commands/Trade/AutoDecline", `Auto declining trade between ${giveUsername} and ${receiveUsername}`);
|
||||
private async autoDecline(interaction: CommandInteraction, user1Username: string, user2Username: string, user1CardNumber: string, user2CardNumber: string, user1CardName: string, user2CardName: string) {
|
||||
AppLogger.LogSilly("Commands/Trade/AutoDecline", `Auto declining trade between ${user1Username} and ${user2Username}`);
|
||||
|
||||
const tradeEmbed = new EmbedBuilder()
|
||||
.setTitle("Trade Expired")
|
||||
.setDescription(`Trade initiated between ${receiveUsername} and ${giveUsername}`)
|
||||
.setDescription(`Trade initiated between ${user1Username} and ${user2Username}`)
|
||||
.setColor(EmbedColours.Error)
|
||||
.setImage("https://media1.tenor.com/m/KkZwKl2AQ2QAAAAd/trade-offer.gif")
|
||||
.addFields([
|
||||
{
|
||||
name: "I Receive",
|
||||
value: `${receiveCardNumber}: ${receiveCardName}`,
|
||||
name: `${user1Username} Receives`,
|
||||
value: `${user2CardNumber}: ${user2CardName}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: "You Receive",
|
||||
value: `${giveCardNumber}: ${giveCardName}`,
|
||||
name: `${user2Username} Receives`,
|
||||
value: `${user1CardNumber}: ${user1CardName}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
|
|
6
src/constants/CardConstants.ts
Normal file
6
src/constants/CardConstants.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
export default class CardConstants {
|
||||
public static readonly ClaimCost = 10;
|
||||
public static readonly TimerGiveAmount = 10;
|
||||
public static readonly DailyCurrency = 100;
|
||||
public static readonly StartingCurrency = 300;
|
||||
}
|
|
@ -58,4 +58,21 @@ export function CardRarityParse(rarity: string): CardRarity {
|
|||
default:
|
||||
return CardRarity.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
export function GetSacrificeAmount(rarity: CardRarity): number {
|
||||
switch (rarity) {
|
||||
case CardRarity.Bronze:
|
||||
return 5;
|
||||
case CardRarity.Silver:
|
||||
return 10;
|
||||
case CardRarity.Gold:
|
||||
return 30;
|
||||
case CardRarity.Manga:
|
||||
return 40;
|
||||
case CardRarity.Legendary:
|
||||
return 100;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,14 @@
|
|||
export default class EmbedColours {
|
||||
// General
|
||||
public static readonly Ok = 0x3050ba;
|
||||
public static readonly Success = 0x50c878;
|
||||
public static readonly Error = 0xff0000;
|
||||
|
||||
// Colours
|
||||
public static readonly Grey = 0xd3d3d3;
|
||||
public static readonly Green = 0x228B22;
|
||||
|
||||
// Card Types
|
||||
public static readonly BronzeCard = 0xcd7f32;
|
||||
public static readonly SilverCard = 0xc0c0c0;
|
||||
public static readonly GoldCard = 0xffd700;
|
||||
|
|
|
@ -27,6 +27,12 @@ export default class AppBaseEntity {
|
|||
await repository.save(entity);
|
||||
}
|
||||
|
||||
public static async SaveAll<T extends AppBaseEntity>(target: EntityTarget<T>, entities: DeepPartial<T>[]): Promise<void> {
|
||||
const repository = AppDataSource.getRepository<T>(target);
|
||||
|
||||
await repository.save(entities);
|
||||
}
|
||||
|
||||
public static async Remove<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> {
|
||||
const repository = AppDataSource.getRepository<T>(target);
|
||||
|
||||
|
|
|
@ -29,6 +29,12 @@ export default class Inventory extends AppBaseEntity {
|
|||
this.Quantity = quantity;
|
||||
}
|
||||
|
||||
public RemoveQuantity(amount: number) {
|
||||
if (this.Quantity < amount) return;
|
||||
|
||||
this.Quantity -= amount;
|
||||
}
|
||||
|
||||
public AddClaim(claim: Claim) {
|
||||
this.Claims.push(claim);
|
||||
}
|
||||
|
|
|
@ -13,7 +13,22 @@ export default class User extends AppBaseEntity {
|
|||
@Column()
|
||||
Currency: number;
|
||||
|
||||
public UpdateCurrency(currency: number) {
|
||||
this.Currency = currency;
|
||||
@Column()
|
||||
LastUsedDaily?: Date;
|
||||
|
||||
public AddCurrency(amount: number) {
|
||||
this.Currency += amount;
|
||||
}
|
||||
|
||||
public RemoveCurrency(amount: number): boolean {
|
||||
if (this.Currency < amount) return false;
|
||||
|
||||
this.Currency -= amount;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public UpdateLastUsedDaily(lastUsedDaily: Date) {
|
||||
this.LastUsedDaily = lastUsedDaily;
|
||||
}
|
||||
}
|
15
src/database/migrations/app/0.6/1715967355818-daily.ts
Normal file
15
src/database/migrations/app/0.6/1715967355818-daily.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
import MigrationHelper from "../../../../helpers/MigrationHelper";
|
||||
|
||||
export class Daily1715967355818 implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
MigrationHelper.Up("1715967355818-daily", "0.6", [
|
||||
"01-table/User",
|
||||
], queryRunner);
|
||||
}
|
||||
|
||||
public async down(): Promise<void> {
|
||||
}
|
||||
|
||||
}
|
|
@ -35,7 +35,8 @@ export default class InventoryHelper {
|
|||
.filter(x => {
|
||||
x.cards = x.cards
|
||||
.sort((a, b) => b.type - a.type)
|
||||
.filter(y => inventory.find(z => z.CardNumber == y.id));
|
||||
.filter(y => inventory.find(z => z.CardNumber == y.id))
|
||||
.filter(y => inventory.find(z => z.CardNumber == y.id)!.Quantity > 0);
|
||||
|
||||
return x;
|
||||
});
|
||||
|
|
81
src/helpers/TimerHelper.ts
Normal file
81
src/helpers/TimerHelper.ts
Normal file
|
@ -0,0 +1,81 @@
|
|||
import { CronJob } from "cron";
|
||||
import { v4 } from "uuid";
|
||||
import { Primitive } from "../type/primitive";
|
||||
|
||||
interface Timer {
|
||||
id: string;
|
||||
job: CronJob;
|
||||
context: Map<string, Primitive>;
|
||||
onTick: ((context: Map<string, Primitive>) => void) | ((context: Map<string, Primitive>) => Promise<void>);
|
||||
runOnStart: boolean;
|
||||
}
|
||||
|
||||
export default class TimerHelper {
|
||||
private _timers: Timer[];
|
||||
|
||||
constructor() {
|
||||
this._timers = [];
|
||||
}
|
||||
|
||||
public AddTimer(
|
||||
cronTime: string,
|
||||
timeZone: string,
|
||||
onTick: ((context: Map<string, Primitive>) => void) | ((context: Map<string, Primitive>) => Promise<void>),
|
||||
runOnStart: boolean = false): string {
|
||||
const context = new Map<string, Primitive>();
|
||||
|
||||
const job = new CronJob(
|
||||
cronTime,
|
||||
() => {
|
||||
onTick(context);
|
||||
},
|
||||
null,
|
||||
false,
|
||||
timeZone,
|
||||
);
|
||||
|
||||
const id = v4();
|
||||
|
||||
this._timers.push({
|
||||
id,
|
||||
job,
|
||||
context,
|
||||
onTick,
|
||||
runOnStart,
|
||||
});
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public StartAllTimers() {
|
||||
this._timers.forEach(timer => this.StartJob(timer));
|
||||
}
|
||||
|
||||
public StopAllTimers() {
|
||||
this._timers.forEach(timer => timer.job.stop());
|
||||
}
|
||||
|
||||
public StartTimer(id: string) {
|
||||
const timer = this._timers.find(x => x.id == id);
|
||||
|
||||
if (!timer) return;
|
||||
|
||||
this.StartJob(timer);
|
||||
}
|
||||
|
||||
public StopTimer(id: string) {
|
||||
const timer = this._timers.find(x => x.id == id);
|
||||
|
||||
if (!timer) return;
|
||||
|
||||
timer.job.stop();
|
||||
}
|
||||
|
||||
private StartJob(timer: Timer) {
|
||||
timer.job.start();
|
||||
|
||||
if (timer.runOnStart) {
|
||||
timer.onTick(timer.context);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,11 +3,14 @@ import { Environment } from "./constants/Environment";
|
|||
|
||||
// Global Command Imports
|
||||
import About from "./commands/about";
|
||||
import Balance from "./commands/balance";
|
||||
import Daily from "./commands/daily";
|
||||
import Drop from "./commands/drop";
|
||||
import Gdrivesync from "./commands/gdrivesync";
|
||||
import Give from "./commands/give";
|
||||
import Inventory from "./commands/inventory";
|
||||
import Resync from "./commands/resync";
|
||||
import Sacrifice from "./commands/sacrifice";
|
||||
import Series from "./commands/series";
|
||||
import Trade from "./commands/trade";
|
||||
import View from "./commands/view";
|
||||
|
@ -20,6 +23,7 @@ import Droprarity from "./commands/stage/droprarity";
|
|||
import Claim from "./buttonEvents/Claim";
|
||||
import InventoryButtonEvent from "./buttonEvents/Inventory";
|
||||
import Reroll from "./buttonEvents/Reroll";
|
||||
import SacrificeButtonEvent from "./buttonEvents/Sacrifice";
|
||||
import SeriesEvent from "./buttonEvents/Series";
|
||||
import TradeButtonEvent from "./buttonEvents/Trade";
|
||||
|
||||
|
@ -27,11 +31,14 @@ export default class Registry {
|
|||
public static RegisterCommands() {
|
||||
// Global Commands
|
||||
CoreClient.RegisterCommand("about", new About());
|
||||
CoreClient.RegisterCommand("balance", new Balance());
|
||||
CoreClient.RegisterCommand("daily", new Daily());
|
||||
CoreClient.RegisterCommand("drop", new Drop());
|
||||
CoreClient.RegisterCommand("gdrivesync", new Gdrivesync());
|
||||
CoreClient.RegisterCommand("give", new Give());
|
||||
CoreClient.RegisterCommand("inventory", new Inventory());
|
||||
CoreClient.RegisterCommand("resync", new Resync());
|
||||
CoreClient.RegisterCommand("sacrifice", new Sacrifice());
|
||||
CoreClient.RegisterCommand("series", new Series());
|
||||
CoreClient.RegisterCommand("trade", new Trade());
|
||||
CoreClient.RegisterCommand("view", new View());
|
||||
|
@ -41,14 +48,11 @@ export default class Registry {
|
|||
CoreClient.RegisterCommand("droprarity", new Droprarity(), Environment.Test);
|
||||
}
|
||||
|
||||
public static RegisterEvents() {
|
||||
|
||||
}
|
||||
|
||||
public static RegisterButtonEvents() {
|
||||
CoreClient.RegisterButtonEvent("claim", new Claim());
|
||||
CoreClient.RegisterButtonEvent("inventory", new InventoryButtonEvent());
|
||||
CoreClient.RegisterButtonEvent("reroll", new Reroll());
|
||||
CoreClient.RegisterButtonEvent("sacrifice", new SacrificeButtonEvent());
|
||||
CoreClient.RegisterButtonEvent("series", new SeriesEvent());
|
||||
CoreClient.RegisterButtonEvent("trade", new TradeButtonEvent());
|
||||
}
|
||||
|
|
19
src/timers/GiveCurrency.ts
Normal file
19
src/timers/GiveCurrency.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import AppLogger from "../client/appLogger";
|
||||
import CardConstants from "../constants/CardConstants";
|
||||
import User from "../database/entities/app/User";
|
||||
|
||||
export default async function GiveCurrency() {
|
||||
AppLogger.LogDebug("Timers/GiveCurrency", "Giving currency to every known user");
|
||||
|
||||
const users = await User.FetchAll(User);
|
||||
|
||||
const usersFiltered = users.filter(x => x.Currency < 1000);
|
||||
|
||||
for (const user of usersFiltered) {
|
||||
user.AddCurrency(CardConstants.TimerGiveAmount);
|
||||
}
|
||||
|
||||
User.SaveAll(User, users);
|
||||
|
||||
AppLogger.LogDebug("Timers/GiveCurrency", `Successfully gave +${CardConstants.TimerGiveAmount} currency to ${usersFiltered.length} users`);
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||
import { CommandInteraction } from "discord.js";
|
||||
|
||||
export abstract class Command {
|
||||
public CommandBuilder: Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- CommandBuilder type is dynamic depending on options and can't be strictly typed
|
||||
public CommandBuilder: any;
|
||||
|
||||
abstract execute(interaction: CommandInteraction): Promise<void>;
|
||||
}
|
||||
|
|
1
src/type/primitive.ts
Normal file
1
src/type/primitive.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export type Primitive = string | number | boolean;
|
Loading…
Reference in a new issue