Compare commits

..

No commits in common. "main" and "v0.6.3" have entirely different histories.
main ... v0.6.3

32 changed files with 181 additions and 1760 deletions

View file

@ -7,17 +7,13 @@
# any secret values.
BOT_TOKEN=
BOT_VER=0.8.1
BOT_VER=0.6.3
BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816
BOT_CLIENTID=682942374040961060
BOT_ENV=4
BOT_ADMINS=147392775707426816,887272961504071690
BOT_LOGLEVEL=info
BOT_LOG_DISCORD_ENABLE=false
BOT_LOG_DISCORD_LEVEL=warn
BOT_LOG_DISCORD_WEBHOOK=
BOT_LOG_DISCORD_SERVICE=
ABOUT_FUNDING=
ABOUT_REPO=
@ -37,4 +33,4 @@ DB_CARD_FILE=:memory:
EXPRESS_PORT=3302
GDRIVESYNC_AUTO=true
GDRIVESYNC_AUTO=true

View file

@ -30,7 +30,7 @@ jobs:
needs: build
runs-on: node
steps:
- uses: https://github.com/appleboy/ssh-action@v1.0.3
- uses: https://github.com/appleboy/ssh-action@v1.0.0
env:
DB_NAME: ${{ secrets.PROD_DB_NAME }}
DB_AUTH_USER: ${{ secrets.PROD_DB_AUTH_USER }}
@ -55,16 +55,12 @@ jobs:
GDRIVESYNC_AUTO: ${{ vars.PROD_GDRIVESYNC_AUTO }}
EXPRESS_PORT: ${{ secrets.PROD_EXPRESS_PORT }}
BOT_LOGLEVEL: ${{ vars.PROD_BOT_LOGLEVEL }}
BOT_LOG_DISCORD_ENABLE: ${{ vars.PROD_BOT_LOG_DISCORD_ENABLE }}
BOT_LOG_DISCORD_LEVEL: ${{ vars.PROD_BOT_LOG_DISCORD_LEVEL }}
BOT_LOG_DISCORD_WEBHOOK: ${{ secrets.PROD_BOT_LOG_DISCORD_WEBHOOK }}
BOT_LOG_DISCORD_SERVICE: ${{ vars.PROD_BOT_LOG_DISCORD_SERVICE }}
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,BOT_LOGLEVEL,BOT_LOG_DISCORD_ENABLE,BOT_LOG_DISCORD_LEVEL,BOT_LOG_DISCORD_WEBHOOK,BOT_LOG_DISCORD_SERVICE
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 \

View file

@ -30,7 +30,7 @@ jobs:
needs: build
runs-on: node
steps:
- uses: https://github.com/appleboy/ssh-action@v1.0.3
- uses: https://github.com/appleboy/ssh-action@v1.0.0
env:
DB_NAME: ${{ secrets.STAGE_DB_NAME }}
DB_AUTH_USER: ${{ secrets.STAGE_DB_AUTH_USER }}
@ -55,16 +55,12 @@ jobs:
GDRIVESYNC_AUTO: ${{ vars.STAGE_GDRIVESYNC_AUTO }}
EXPRESS_PORT: ${{ secrets.STAGE_EXPRESS_PORT }}
BOT_LOGLEVEL: ${{ vars.STAGE_BOT_LOGLEVEL }}
BOT_LOG_DISCORD_ENABLE: ${{ vars.STAGE_BOT_LOG_DISCORD_ENABLE }}
BOT_LOG_DISCORD_LEVEL: ${{ vars.STAGE_BOT_LOG_DISCORD_LEVEL }}
BOT_LOG_DISCORD_WEBHOOK: ${{ secrets.STAGE_BOT_LOG_DISCORD_WEBHOOK }}
BOT_LOG_DISCORD_SERVICE: ${{ vars.STAGE_BOT_LOG_DISCORD_SERVICE }}
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,BOT_LOGLEVEL,BOT_LOG_DISCORD_ENABLE,BOT_LOG_DISCORD_LEVEL,BOT_LOG_DISCORD_WEBHOOK,BOT_LOG_DISCORD_SERVICE
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 \

View file

@ -1,60 +1,2 @@
# Card Drop
Card Drop is a Discord Bot designed to allow users to "drop" random cards into
a channel and have the ability to claim them for themselves or let others if
they so choose.
The cards are randomly chosen based on weights of their card type (i.e. Bronze
is more common than Gold). The user who ran the drop command has 5 minutes to
choose if they want the card to themselves before its claimable by anyone, or
until the drop command is ran again.
## Installation
Downloads of the latest version can be found from the [GitHub Releases](https://github.com/vylpes/card-drop/releases)
or [Forgejo Releases](https://git.vylpes.xyz/external/card-drop/releases) page.
Copy the config template file and fill in the strings.
## Requirements
- NodeJS
- Yarn
- Docker
## Usage
Install the dependencies and build the app:
```bash
yarn Install
yarn build
```
Setup the database (Recommended to use the docker-compose file
```bash
docker compose up -d
```
Copy and edit the settings file
```bash
cp .env.template .env
```
> **NOTE:** Make sure you do *not* check in these files! These contain
sensitive information and should be treated as private.
If you're not using `DB_SYNC=true` in `.env`, make sure to migrate the database
```bash
yarn db:up
```
Start the bot
```bash
yarn start
```
# card-drop

View file

@ -1,120 +0,0 @@
# Cards
This document will describe how to add cards to the bot. This is from the
perspective of the development side and doesn't go into details of syncing
from an external place such as with the Google Drive Sync function.
The cards will be put into the `$DATA_DIR/cards` folder. `$DATA_DIR` is
configured in the `.env` file.
## Folder Structure
The general structure of the cards folder is as follows:
```
cards # The main cards folder
| Series 1 # Series folder
| | BRONZE # Type folder
| | | 1000.jpg # Card image
| | | 1001.jpg
| | 1.json # Card metadata file
| Series 2
| | SILVER
| | | 2000.jpg
| | 2.json
```
- The root of the cards folder will have a folder foor each series
- Each series will contain folders for each of the card types containing the
card images.
- The series folder will also contain a metadata JSON folder containing the
metadata of the cards within that series.
The bot when loading will search the cards folder recursively for each json,
and then read them to determine what cards should be used for the bot.
## Series Metadata
An example of what the metadata files could look like are as follows:
```json
[
{
"id": 1,
"name": "Series 1",
"cards": [
{
"id": "1000",
"name": "Card 1000 of Series 1",
"type": 1,
"path": "Series 1/BRONZE/1000.jpg"
},
{
"id": "1001",
"name": "Card 1001 of Series 1",
"type": 1,
"path": "Series 2/BRONZE?1001.jpg",
"subseries": "Custom Series Name"
}
]
}
]
```
This file will load a series called "Series 1" with the id of 1, containing 2
cards:
- Card 1000, with type 1 (Bronze), with its image located at (from root)
"Series 1/BRONZE/1000.jpg"
- Card 1001 is the same, except has a custom "subseries" name which will
override the main series name if shown, helpful for an "other" category.
### Card Type
<table>
<thead>
<tr>
<th>Number</th>
<th>Name</th>
<th>Chance</th>
<th>Sacrifice Cost (Coins)</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>Unknown</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>1</td>
<td>Bronze</td>
<td>62%</td>
<td>5</td>
</tr>
<tr>
<td>2</td>
<td>Silver</td>
<td>31%</td>
<td>10</td>
</tr>
<tr>
<td>3</td>
<td>Gold</td>
<td>4.4%</td>
<td>30</td>
</tr>
<tr>
<td>4</td>
<td>Manga</td>
<td>2%</td>
<td>40</td>
</tr>
<tr>
<td>5</td>
<td>Legendary</td>
<td>0.6%</td>
<td>100</td>
</tr>
</tbody>
</table>

View file

@ -1,13 +1,13 @@
{
"name": "card-drop",
"version": "0.8.1",
"version": "0.6.3",
"main": "./dist/bot.js",
"typings": "./dist",
"scripts": {
"clean": "rm -rf node_modules/ dist/",
"build": "tsc",
"start": "node ./dist/bot.js",
"test": "echo true",
"test": "jest --passWithNoTests",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js",
@ -15,11 +15,11 @@
"db:create": "typeorm migration:create ./src/database/migrations/app/new",
"release": "np --no-publish"
},
"repository": "https://git.vylpes.xyz/External/card-drop.git",
"repository": "https://gitea.vylpes.xyz/External/card-drop.git",
"author": "Ethan Lane <ethan@vylpes.com>",
"license": "MIT",
"bugs": {
"url": "https//git.vylpes.xyz/External/card-drop/issues",
"url": "https//gitea.vylpes.xyz/External/card-drop/issues",
"email": "helpdesk@vylpes.com"
},
"homepage": "https://gitea.vylpes.xyz/External/card-drop",
@ -31,30 +31,26 @@
"@types/jest": "^29.0.0",
"@types/uuid": "^9.0.0",
"body-parser": "^1.20.2",
"canvas": "^2.11.2",
"clone-deep": "^4.0.1",
"cron": "^3.1.7",
"discord.js": "^14.15.3",
"discord.js": "^14.3.0",
"dotenv": "^16.0.0",
"express": "^4.18.2",
"glob": "^10.3.10",
"jest": "^29.0.0",
"jest-mock-extended": "^3.0.0",
"jimp": "^0.22.12",
"minimatch": "9.0.5",
"minimatch": "9.0.4",
"mysql": "^2.18.1",
"ts-jest": "^29.0.0",
"typeorm": "0.3.20",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^5.0.0",
"winston-discord-transport": "^1.3.0"
"winston": "^3.11.0"
},
"resolutions": {
"**/ws": "^8.17.1"
"overrides": {
"undici": "^5.28.3"
},
"devDependencies": {
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"eslint": "^8.56.0",
"np": "^9.0.0",

View file

@ -5,7 +5,6 @@ import { glob } from "glob";
import { SeriesMetadata } from "../contracts/SeriesMetadata";
import { CoreClient } from "../client/client";
import AppLogger from "../client/appLogger";
import {CardRarity} from "../constants/CardRarity";
export interface CardMetadataResult {
IsSuccess: boolean;
@ -38,22 +37,7 @@ export default class CardMetadataFunction {
if (cardResult.IsSuccess) {
CoreClient.Cards = cardResult.Result!;
const allCards = CoreClient.Cards.flatMap(x => x.cards);
const totalCards = allCards.length;
const bronzeCards = allCards.filter(x => x.type == CardRarity.Bronze)
.length;
const silverCards = allCards.filter(x => x.type == CardRarity.Silver)
.length;
const goldCards = allCards.filter(x => x.type == CardRarity.Gold)
.length;
const mangaCards = allCards.filter(x => x.type == CardRarity.Manga)
.length;
const legendaryCards = allCards.filter(x => x.type == CardRarity.Legendary)
.length;
AppLogger.LogInfo("Functions/CardMetadataFunction", `Loaded ${totalCards} cards to database (${bronzeCards} bronze, ${silverCards} silver, ${goldCards} gold, ${mangaCards} manga, ${legendaryCards} legendary)`);
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);

View file

@ -20,14 +20,6 @@ export default class Claim extends ButtonEvent {
const droppedBy = interaction.customId.split(" ")[3];
const userId = interaction.user.id;
const whenDropped = interaction.message.createdAt;
const lastClaimableDate = new Date(Date.now() - (1000 * 60 * 5)); // 5 minutes ago
if (whenDropped < lastClaimableDate) {
await interaction.channel.send(`${interaction.user}, Cards can only be claimed within 5 minutes of it being dropped!`);
return;
}
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);
@ -39,6 +31,8 @@ export default class Claim extends ButtonEvent {
return;
}
await user.Save(User, user);
const claimed = await eClaim.FetchOneByClaimId(claimId);
if (claimed) {
@ -51,8 +45,6 @@ export default class Claim extends ButtonEvent {
return;
}
await user.Save(User, user);
let inventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
if (!inventory) {
@ -76,7 +68,7 @@ export default class Claim extends ButtonEvent {
const imageFileName = card.card.path.split("/").pop()!;
const embed = CardDropHelperMetadata.GenerateDropEmbed(card, inventory.Quantity, imageFileName, interaction.user.username, user.Currency);
const embed = CardDropHelperMetadata.GenerateDropEmbed(card, inventory.Quantity, imageFileName, interaction.user.username);
const row = CardDropHelperMetadata.GenerateDropButtons(card, claimId, interaction.user.id, true);
await interaction.editReply({
@ -84,4 +76,4 @@ export default class Claim extends ButtonEvent {
components: [ row ],
});
}
}
}

View file

@ -11,8 +11,6 @@ export default class Inventory extends ButtonEvent {
const page = interaction.customId.split(" ")[2];
AppLogger.LogSilly("Button/Inventory", `Parameters: userid=${userid}, page=${page}`);
await interaction.deferUpdate();
const member = interaction.guild.members.cache.find(x => x.id == userid) || await interaction.guild.members.fetch(userid);
@ -26,20 +24,14 @@ export default class Inventory extends ButtonEvent {
const embed = await InventoryHelper.GenerateInventoryPage(member.user.username, member.user.id, Number(page));
if (!embed) {
await interaction.followUp("No page for user found.");
return;
}
await interaction.editReply({
files: [ embed.image ],
await interaction.update({
embeds: [ embed.embed ],
components: [ embed.row ],
});
} catch (e) {
AppLogger.LogError("Button/Inventory", `Error generating inventory page for ${member.user.username} with id ${member.user.id}: ${e}`);
await interaction.followUp("An error has occurred running this command.");
await interaction.reply("No page for user found.");
}
}
}
}

View file

@ -59,7 +59,7 @@ export default class Reroll extends ButtonEvent {
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName, undefined, user.Currency);
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
const claimId = v4();
@ -78,4 +78,4 @@ export default class Reroll extends ButtonEvent {
await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`);
}
}
}
}

View file

@ -24,14 +24,11 @@ export default class Series extends ButtonEvent {
const seriesid = interaction.customId.split(" ")[2];
const page = interaction.customId.split(" ")[3];
await interaction.deferUpdate();
const embed = SeriesHelper.GenerateSeriesViewPage(Number(seriesid), Number(page));
const embed = await SeriesHelper.GenerateSeriesViewPage(Number(seriesid), Number(page), interaction.user.id);
await interaction.editReply({
await interaction.update({
embeds: [ embed!.embed ],
components: [ embed!.row ],
files: [ embed!.image ],
});
}
@ -45,4 +42,4 @@ export default class Series extends ButtonEvent {
components: [ embed!.row ],
});
}
}
}

View file

@ -1,7 +1,4 @@
import path from "path";
import { Logger, createLogger, format, transports } from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
import DiscordTransport from "winston-discord-transport";
export default class AppLogger {
public static Logger: Logger;
@ -22,21 +19,12 @@ export default class AppLogger {
customFormat,
),
defaultMeta: { service: "bot" },
transports: [],
transports: [
new transports.File({ filename: "error.log", level: "error" }),
new transports.File({ filename: "combined.log" }),
],
});
if (process.env.DATA_DIR) {
const logDir = path.join(process.env.DATA_DIR, "logs");
logger.add(new DailyRotateFile({
filename: "bot-%DATE%.log",
dirname: logDir,
datePattern: "YYYY-MM-DD-HH",
maxSize: "20m",
maxFiles: "14d",
}));
}
if (outputToConsole) {
logger.add(new transports.Console({
format: format.combine(
@ -46,18 +34,6 @@ export default class AppLogger {
)}));
}
if (process.env.BOT_LOG_DISCORD_ENABLE == "true") {
if (process.env.BOT_LOG_DISCORD_WEBHOOK) {
logger.add(new DiscordTransport({
webhook: process.env.BOT_LOG_DISCORD_WEBHOOK.toString(),
defaultMeta: { service: process.env.BOT_LOG_DISCORD_SERVICE },
level: process.env.BOT_LOG_DISCORD_LEVEL,
}));
} else {
throw "BOT_LOG_DISCORD_WEBHOOK is required to enable discord logger support.";
}
}
AppLogger.Logger = logger;
AppLogger.LogInfo("AppLogger", `Log Level: ${logLevel}`);
@ -86,4 +62,4 @@ export default class AppLogger {
public static LogSilly(label: string, message: string) {
AppLogger.Logger.silly({ label, message });
}
}
}

View file

@ -16,7 +16,6 @@ import { SeriesMetadata } from "../contracts/SeriesMetadata";
import AppLogger from "./appLogger";
import TimerHelper from "../helpers/TimerHelper";
import GiveCurrency from "../timers/GiveCurrency";
import PurgeClaims from "../timers/PurgeClaims";
export class CoreClient extends Client {
private static _commandItems: ICommandItem[];
@ -80,10 +79,8 @@ export class CoreClient extends Client {
.then(() => {
AppLogger.LogInfo("Client", "App Data Source Initialised");
this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false);
this._timerHelper.AddTimer("0 0 * * *", "Europe/London", PurgeClaims, false);
this._timerHelper.StartAllTimers();
const timerId = this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false);
this._timerHelper.StartTimer(timerId);
})
.catch(err => {
AppLogger.LogError("Client", "App Data Source Initialisation Failed");

View file

@ -2,14 +2,11 @@ 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);

View file

@ -1,15 +0,0 @@
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}`);
}

View file

@ -1,29 +0,0 @@
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import EmbedColours from "../constants/EmbedColours";
import { Command } from "../type/command";
import User from "../database/entities/app/User";
export default class AllBalance extends Command {
constructor() {
super();
this.CommandBuilder = new SlashCommandBuilder()
.setName("allbalance")
.setDescription("Get everyone's currency balance")
.setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator);
}
public override async execute(interaction: CommandInteraction) {
const users = await User.FetchAll(User);
const filteredUsers = users.filter(x => x.Currency > 0)
.sort((a, b) => b.Currency - a.Currency);
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle("All Balances")
.setDescription(filteredUsers.map(x => `<@${x.Id}> ${x.Currency}`).join("\n"));
await interaction.reply({ embeds: [ embed ], ephemeral: true });
}
}

View file

@ -67,7 +67,7 @@ export default class Drop extends Command {
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName, undefined, user.Currency);
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
const claimId = v4();
@ -88,4 +88,4 @@ export default class Drop extends Command {
}
}
}
}

View file

@ -26,8 +26,6 @@ export default class Inventory extends Command {
const user = userOption ? userOption.user! : interaction.user;
await interaction.deferReply();
AppLogger.LogSilly("Commands/Inventory", `Parameters: page=${page?.value}, user=${user.id}`);
try {
@ -39,20 +37,14 @@ export default class Inventory extends Command {
const embed = await InventoryHelper.GenerateInventoryPage(user.username, user.id, pageNumber);
if (!embed) {
await interaction.followUp("No page for user found.");
return;
}
await interaction.followUp({
files: [ embed.image ],
await interaction.reply({
embeds: [ embed.embed ],
components: [ embed.row ],
});
} catch (e) {
AppLogger.LogError("Commands/Inventory", e as string);
await interaction.followUp("An error has occurred running this command.");
await interaction.reply("No page for user found.");
}
}
}
}

View file

@ -47,8 +47,6 @@ export default class Series extends Command {
AppLogger.LogSilly("Commands/Series/View", `Parameters: id=${id?.value}`);
await interaction.deferReply();
if (!id) return;
const series = CoreClient.Cards.find(x => x.id == id.value);
@ -56,17 +54,13 @@ export default class Series extends Command {
if (!series) {
AppLogger.LogVerbose("Commands/Series/View", "Series not found.");
await interaction.followUp("Series not found.");
await interaction.reply("Series not found.");
return;
}
const embed = await SeriesHelper.GenerateSeriesViewPage(series.id, 0, interaction.user.id);
const embed = SeriesHelper.GenerateSeriesViewPage(series.id, 0);
await interaction.followUp({
embeds: [ embed!.embed ],
components: [ embed!.row ],
files: [ embed!.image ],
});
await interaction.reply({ embeds: [ embed!.embed ], components: [ embed!.row ]});
}
private async ListSeries(interaction: CommandInteraction) {
@ -74,4 +68,4 @@ export default class Series extends Command {
await interaction.reply({ embeds: [ embed!.embed ], components: [ embed!.row ]});
}
}
}

View file

@ -1,51 +0,0 @@
import {CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder} from "discord.js";
import {Command} from "../type/command";
import {CoreClient} from "../client/client";
import {CardRarity} from "../constants/CardRarity";
import EmbedColours from "../constants/EmbedColours";
export default class Stats extends Command {
constructor() {
super();
this.CommandBuilder = new SlashCommandBuilder()
.setName("stats")
.setDescription("Get bot stats such as card info")
.setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator);
}
public override async execute(interaction: CommandInteraction) {
const allCards = CoreClient.Cards.flatMap(x => x.cards);
const totalCards = allCards.length;
const bronzeCards = allCards.filter(x => x.type == CardRarity.Bronze)
.length;
const silverCards = allCards.filter(x => x.type == CardRarity.Silver)
.length;
const goldCards = allCards.filter(x => x.type == CardRarity.Gold)
.length;
const mangaCards = allCards.filter(x => x.type == CardRarity.Manga)
.length;
const legendaryCards = allCards.filter(x => x.type == CardRarity.Legendary)
.length;
const description = [
`${totalCards} Total`,
`${bronzeCards} Bronze`,
`${silverCards} Silver`,
`${goldCards} Gold`,
`${mangaCards} Manga`,
`${legendaryCards} Legendary`,
].join("\n");
const embed = new EmbedBuilder()
.setTitle("Stats")
.setDescription(description)
.setColor(EmbedColours.Ok);
await interaction.reply({
embeds: [ embed ],
ephemeral: true,
});
}
}

View file

@ -39,12 +39,6 @@ export default class AppBaseEntity {
await repository.remove(entity);
}
public static async RemoveMany<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T[]): Promise<void> {
const repository = AppDataSource.getRepository<T>(target);
await repository.remove(entity);
}
public static async FetchAll<T extends AppBaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
const repository = AppDataSource.getRepository<T>(target);

View file

@ -11,8 +11,6 @@ export interface CardMetadata {
name: string,
type: CardRarity,
path: string,
subseries?: string,
colour?: string,
}
export interface DropResult {

View file

@ -54,4 +54,4 @@ export default class Inventory extends AppBaseEntity {
return all;
}
}
}

View file

@ -4,8 +4,6 @@ import CardRarityChances from "../constants/CardRarityChances";
import { DropResult } from "../contracts/SeriesMetadata";
import { CoreClient } from "../client/client";
import AppLogger from "../client/appLogger";
import CardConstants from "../constants/CardConstants";
import StringTools from "./StringTools";
export default class CardDropHelperMetadata {
public static GetRandomCard(): DropResult | undefined {
@ -79,59 +77,25 @@ export default class CardDropHelperMetadata {
return { card, series };
}
public static GenerateDropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string, claimedBy?: string, currency?: number): EmbedBuilder {
public static GenerateDropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string, claimedBy?: string): EmbedBuilder {
AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropEmbed", `Parameters: drop=${drop.card.id}, quantityClaimed=${quantityClaimed}, imageFileName=${imageFileName}`);
const description = drop.card.subseries ?? drop.series.name;
let colour = CardRarityToColour(drop.card.type);
let description = "";
description += `Series: ${drop.series.name}\n`;
description += `Claimed: ${quantityClaimed}\n`;
if (drop.card.colour && StringTools.IsHexCode(drop.card.colour)) {
const hexCode = Number("0x" + drop.card.colour);
if (hexCode) {
colour = hexCode;
} else {
AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`);
}
} else if (drop.card.colour) {
AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`);
if (claimedBy != null) {
description += `Claimed by: ${claimedBy}\n`;
} else {
description += "Claimed by: (UNCLAIMED)\n";
}
const embed = new EmbedBuilder()
return new EmbedBuilder()
.setTitle(drop.card.name)
.setDescription(description)
.setFooter({ text: `${CardRarityToString(drop.card.type)} · ${drop.card.id}` })
.setColor(colour)
.setImage(`attachment://${imageFileName}`)
.addFields([
{
name: "Claimed",
value: `${quantityClaimed}`,
inline: true,
}
]);
if (claimedBy != null) {
embed.addFields([
{
name: "Claimed by",
value: claimedBy,
inline: true,
}
]);
}
if (currency != null) {
embed.addFields([
{
name: "Currency",
value: `${currency}`,
inline: true,
}
]);
}
return embed;
.setColor(CardRarityToColour(drop.card.type))
.setImage(`attachment://${imageFileName}`);
}
public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string, disabled: boolean = false): ActionRowBuilder<ButtonBuilder> {
@ -141,7 +105,7 @@ export default class CardDropHelperMetadata {
.addComponents(
new ButtonBuilder()
.setCustomId(`claim ${drop.card.id} ${claimId} ${userId}`)
.setLabel(`Claim (${CardConstants.ClaimCost} 🪙)`)
.setLabel("Claim")
.setStyle(ButtonStyle.Primary)
.setDisabled(disabled),
new ButtonBuilder()
@ -149,4 +113,4 @@ export default class CardDropHelperMetadata {
.setLabel("Reroll")
.setStyle(ButtonStyle.Secondary));
}
}
}

View file

@ -1,62 +0,0 @@
import {createCanvas, loadImage} from "canvas";
import path from "path";
import AppLogger from "../client/appLogger";
import {existsSync} from "fs";
import Inventory from "../database/entities/app/Inventory";
import Jimp from "jimp";
interface CardInput {
id: string;
path: string;
}
export default class ImageHelper {
public static async GenerateCardImageGrid(cards: CardInput[], userId?: string): Promise<Buffer> {
const gridWidth = 3;
const gridHeight = Math.ceil(cards.length / gridWidth);
const imageWidth = 526;
const imageHeight = 712;
const canvasWidth = imageWidth * gridWidth;
const canvasHeight = imageHeight * gridHeight;
const canvas = createCanvas(canvasWidth, canvasHeight);
const ctx = canvas.getContext("2d");
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
const filePath = path.join(process.env.DATA_DIR!, "cards", card.path);
const exists = existsSync(filePath);
if (!exists) {
AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${card.path}`);
continue;
}
const imageData = await Jimp.read(filePath);
if (userId != null) {
const claimed = await Inventory.FetchOneByCardNumberAndUserId(userId, card.id);
if (!claimed || claimed.Quantity == 0) {
imageData.greyscale();
}
}
const image = await loadImage(await imageData.getBufferAsync("image/png"));
const x = i % gridWidth;
const y = Math.floor(i / gridWidth);
const imageX = imageWidth * x;
const imageY = imageHeight * y;
ctx.drawImage(image, imageX, imageY);
}
return canvas.toBuffer();
}
}

View file

@ -1,11 +1,10 @@
import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import Inventory from "../database/entities/app/Inventory";
import { CoreClient } from "../client/client";
import EmbedColours from "../constants/EmbedColours";
import { CardRarity, CardRarityToString } from "../constants/CardRarity";
import cloneDeep from "clone-deep";
import AppLogger from "../client/appLogger";
import ImageHelper from "./ImageHelper";
interface InventoryPage {
id: number,
@ -19,26 +18,16 @@ interface InventoryPageCards {
name: string,
type: CardRarity,
quantity: number,
path: string,
}
interface ReturnedInventoryPage {
embed: EmbedBuilder,
row: ActionRowBuilder<ButtonBuilder>,
image: AttachmentBuilder,
}
export default class InventoryHelper {
public static async GenerateInventoryPage(username: string, userid: string, page: number): Promise<ReturnedInventoryPage | undefined> {
public static async GenerateInventoryPage(username: string, userid: string, page: number): Promise<{ embed: EmbedBuilder, row: ActionRowBuilder<ButtonBuilder> }> {
AppLogger.LogSilly("Helpers/InventoryHelper", `Parameters: username=${username}, userid=${userid}, page=${page}`);
const cardsPerPage = 9;
const cardsPerPage = 15;
const inventory = await Inventory.FetchAllByUserId(userid);
if (!inventory || inventory.length == 0) return undefined;
const clientCards = cloneDeep(CoreClient.Cards);
const allSeriesClaimed = clientCards
@ -73,7 +62,6 @@ export default class InventoryHelper {
name: card.name,
type: card.type,
quantity: item.Quantity,
path: card.path,
});
}
@ -89,15 +77,15 @@ export default class InventoryHelper {
const currentPage = pages[page];
if (!currentPage) {
return undefined;
AppLogger.LogError("Helpers/InventoryHelper", "Unable to find page");
return Promise.reject("Unable to find page");
}
const embed = new EmbedBuilder()
.setTitle(username)
.setDescription(`**${currentPage.name} (${currentPage.seriesSubpage + 1})**\n${currentPage.cards.map(x => `[${x.id}] ${x.name} (${CardRarityToString(x.type)}) x${x.quantity}`).join("\n")}`)
.setFooter({ text: `Page ${page + 1} of ${pages.length}` })
.setColor(EmbedColours.Ok)
.setImage("attachment://page.png");
.setColor(EmbedColours.Ok);
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
@ -112,9 +100,6 @@ export default class InventoryHelper {
.setStyle(ButtonStyle.Primary)
.setDisabled(page + 1 == pages.length));
const buffer = await ImageHelper.GenerateCardImageGrid(currentPage.cards.map(x => ({ id: x.id, path: x.path })));
const image = new AttachmentBuilder(buffer, { name: "page.png" });
return { embed, row, image };
return { embed, row };
}
}
}

View file

@ -1,16 +1,15 @@
import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import AppLogger from "../client/appLogger";
import cloneDeep from "clone-deep";
import { CoreClient } from "../client/client";
import EmbedColours from "../constants/EmbedColours";
import { CardRarityToString } from "../constants/CardRarity";
import ImageHelper from "./ImageHelper";
export default class SeriesHelper {
public static async GenerateSeriesViewPage(seriesId: number, page: number, userId: string): Promise<{ embed: EmbedBuilder, row: ActionRowBuilder<ButtonBuilder>, image: AttachmentBuilder } | null> {
public static GenerateSeriesViewPage(seriesId: number, page: number): { embed: EmbedBuilder, row: ActionRowBuilder<ButtonBuilder> } | null {
AppLogger.LogSilly("Helpers/SeriesHelper", `Parameters: seriesId=${seriesId}, page=${page}`);
const itemsPerPage = 9;
const itemsPerPage = 15;
const series = cloneDeep(CoreClient.Cards)
.find(x => x.id == seriesId);
@ -21,7 +20,6 @@ export default class SeriesHelper {
}
const totalPages = Math.ceil(series.cards.length / itemsPerPage);
const totalCards = series.cards.length;
if (page > totalPages) {
AppLogger.LogVerbose("Helpers/SeriesHelper", `Trying to find page greater than what exists for this series. Page: ${page} but there are only ${totalPages} pages`);
@ -31,15 +29,14 @@ export default class SeriesHelper {
const cardsOnPage = series.cards.splice(page * itemsPerPage, itemsPerPage);
const description = cardsOnPage
.map(x => `[${x.id}] ${x.name} (${CardRarityToString(x.type)})`)
.map(x => `[${x.id}] ${x.name} ${CardRarityToString(x.type).toUpperCase()}`)
.join("\n");
const embed = new EmbedBuilder()
.setTitle(series.name)
.setColor(EmbedColours.Ok)
.setDescription(description)
.setFooter({ text: `${series.id} · ${totalCards} cards · Page ${page + 1} of ${totalPages}` })
.setImage("attachment://page.png");
.setFooter({ text: `${series.id} · ${series.cards.length} cards · Page ${page + 1} of ${totalPages}` });
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
@ -52,12 +49,9 @@ export default class SeriesHelper {
.setCustomId(`series view ${seriesId} ${page + 1}`)
.setLabel("Next")
.setStyle(ButtonStyle.Primary)
.setDisabled(page + 1 == totalPages));
.setDisabled(page + 1 > totalPages));
const buffer = await ImageHelper.GenerateCardImageGrid(cardsOnPage.map(x => ({id: x.id, path: x.path})), userId);
const image = new AttachmentBuilder(buffer, { name: "page.png" });
return { embed, row, image };
return { embed, row };
}
public static GenerateSeriesListPage(page: number): { embed: EmbedBuilder, row: ActionRowBuilder<ButtonBuilder> } | null {
@ -78,7 +72,7 @@ export default class SeriesHelper {
const seriesOnPage = series.splice(page * itemsPerPage, itemsPerPage);
const description = seriesOnPage
.map(x => `[${x.id}] ${x.name} (${x.cards.length} cards)`)
.map(x => `[${x.id}] ${x.name}`)
.join("\n");
const embed = new EmbedBuilder()
@ -98,8 +92,8 @@ export default class SeriesHelper {
.setCustomId(`series list ${page + 1}`)
.setLabel("Next")
.setStyle(ButtonStyle.Primary)
.setDisabled(page + 1 == totalPages));
.setDisabled(page + 1 > totalPages));
return { embed, row };
}
}
}

View file

@ -39,18 +39,4 @@ export default class StringTools {
public static ReplaceAll(str: string, find: string, replace: string) {
return str.replace(new RegExp(find, "g"), replace);
}
public static IsHexCode(str: string): boolean {
if (str.length != 6) return false;
const characters = "0123456789abcdefABCDEF";
for (let i = 0; i < 6; i++) {
const char = str[i];
if (!characters.includes(char)) return false;
}
return true;
}
}

View file

@ -3,7 +3,6 @@ import { Environment } from "./constants/Environment";
// Global Command Imports
import About from "./commands/about";
import AllBalance from "./commands/allbalance";
import Balance from "./commands/balance";
import Daily from "./commands/daily";
import Drop from "./commands/drop";
@ -13,7 +12,6 @@ import Inventory from "./commands/inventory";
import Resync from "./commands/resync";
import Sacrifice from "./commands/sacrifice";
import Series from "./commands/series";
import Stats from "./commands/stats";
import Trade from "./commands/trade";
import View from "./commands/view";
@ -33,7 +31,6 @@ export default class Registry {
public static RegisterCommands() {
// Global Commands
CoreClient.RegisterCommand("about", new About());
CoreClient.RegisterCommand("allbalance", new AllBalance());
CoreClient.RegisterCommand("balance", new Balance());
CoreClient.RegisterCommand("daily", new Daily());
CoreClient.RegisterCommand("drop", new Drop());
@ -43,7 +40,6 @@ export default class Registry {
CoreClient.RegisterCommand("resync", new Resync());
CoreClient.RegisterCommand("sacrifice", new Sacrifice());
CoreClient.RegisterCommand("series", new Series());
CoreClient.RegisterCommand("stats", new Stats());
CoreClient.RegisterCommand("trade", new Trade());
CoreClient.RegisterCommand("view", new View());

View file

@ -1,14 +0,0 @@
import AppLogger from "../client/appLogger";
import Claim from "../database/entities/app/Claim";
export default async function PurgeClaims() {
const claims = await Claim.FetchAll(Claim);
const whenLastClaimable = new Date(Date.now() - (1000 * 60 * 5)); // 5 minutes ago
const expiredClaims = claims.filter(x => x.WhenCreated < whenLastClaimable);
await Claim.RemoveMany(Claim, expiredClaims);
AppLogger.LogInfo("Timers/PurgeClaims", `Purged ${expiredClaims.length} claims from the database`);
}

View file

@ -1,66 +0,0 @@
import {CoreClient} from "../src/client/client";
import Registry from "../src/registry";
import fs from "fs";
import path from "path";
describe("RegisterCommands", () => {
test("EXPECT every command in the commands folder to be registered", () => {
const registeredCommands: string[] = [];
CoreClient.RegisterCommand = jest.fn().mockImplementation((name: string) => {
registeredCommands.push(name);
});
Registry.RegisterCommands();
const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands"))
.filter(x => x.endsWith(".ts"));
for (const file of commandFiles) {
expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]);
}
expect(commandFiles.length).toBe(registeredCommands.length);
});
});
describe("RegisterButtonEvents", () => {
test("EXEPCT every button event in the button events folder to be registered", () => {
const registeredButtonEvents: string[] = [];
CoreClient.RegisterButtonEvent = jest.fn().mockImplementation((name: string) => {
registeredButtonEvents.push(name);
});
Registry.RegisterButtonEvents();
const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents"))
.filter(x => x.endsWith(".ts"));
for (const file of eventFiles) {
expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase());
}
expect(eventFiles.length).toBe(registeredButtonEvents.length);
});
});
function getFilesInDirectory(dir: string): string[] {
let results: string[] = [];
const list = fs.readdirSync(dir);
list.forEach(file => {
file = path.join(dir, file);
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* recurse into a subdirectory */
results = results.concat(getFilesInDirectory(file));
} else {
/* is a file */
results.push(file);
}
});
return results;
}

1192
yarn.lock

File diff suppressed because it is too large Load diff