Skip to content

Commit

Permalink
Add middleware so that the bot saves a user to the database on discov…
Browse files Browse the repository at this point in the history
…ery of them
  • Loading branch information
Vylpes committed Jun 13, 2024
1 parent e41cede commit e8ffe26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/client/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/client/interactionCreate/middleware/NewUserDiscovery.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}

0 comments on commit e8ffe26

Please sign in to comment.