(events): postgres sucks.

This commit is contained in:
2026-08-26 12:03:25 +08:00
parent 00c64f449a
commit eaf05aebc3
10 changed files with 132 additions and 7 deletions
+4
View File
@@ -0,0 +1,4 @@
import optin from "./optin";
import optout from "./optout";
export default [optin, optout];
+23
View File
@@ -0,0 +1,23 @@
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from "athena-prime";
import { setUserPrivacy } from "../utils";
@SlashCommand(
new CommandBuilder('optin', 'Opt into the Pet the Pond event.')
.setIntegrationTypes(Constants.ApplicationIntegrationType.GuildInstall)
.setContexts(Constants.InteractionContextType.Guild)
.setCommandType(Constants.ApplicationCommandType.ChatInput)
)
class OptInCommand extends Command<CommandClient> {
cooldown = 5;
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
await setUserPrivacy(interaction.member.id, { optin: true });
await interaction.createMessage({
content: "Successfully opted in. Interactions will be acknowledged.",
flags: Constants.MessageFlags.Ephemeral
});
}
}
export default new OptInCommand('optin');
+23
View File
@@ -0,0 +1,23 @@
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from "athena-prime";
import { setUserPrivacy } from "../utils";
@SlashCommand(
new CommandBuilder('optout', 'Opt out of the Pet the Pond event.')
.setIntegrationTypes(Constants.ApplicationIntegrationType.GuildInstall)
.setContexts(Constants.InteractionContextType.Guild)
.setCommandType(Constants.ApplicationCommandType.ChatInput)
)
class OptOutCommand extends Command<CommandClient> {
cooldown = 5;
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
await setUserPrivacy(interaction.member.id, { optin: false });
await interaction.createMessage({
content: "Successfully opted out. Interactions will not be acknowledged.",
flags: Constants.MessageFlags.Ephemeral
});
}
}
export default new OptOutCommand('optout');
+6 -2
View File
@@ -1,7 +1,8 @@
import { CommandClient, Constants, Event, Member, Message } from "athena-prime";
import config from '../../config';
import { getUser, setUserXp } from "../../utils";
// ----------
/// ----------
class MessageReactionAddEvent extends Event<CommandClient> {
event: string = 'messageReactionAdd' as const;
@@ -10,7 +11,10 @@ class MessageReactionAddEvent extends Event<CommandClient> {
const msg = await context.getMessage(message.channel.id, message.id);
if (msg.author.id !== config.identifiers.targetId || emoji.id !== config.identifiers.emojiId) return;
console.log(`[MESSAGE_REACTION_ADD] - ID: ${member.id}`);
const _user = await getUser(member.id);
if (!_user.privacy.optin) return;
await setUserXp(member.id, { current: _user.xp.current + 1 });
}
}
+6 -2
View File
@@ -1,16 +1,20 @@
import { CommandClient, Constants, Event, Member, Message } from "athena-prime";
import config from '../../config';
import { getUser, setUserXp } from "../../utils";
// ----------
class MessageReactionRemoveEvent extends Event<CommandClient> {
event: string = 'messageReactionRemove' as const;
async handle(context: CommandClient<any, any>, message: { id: string, channel: { id: string }}, emoji: Constants.APIEmoji, user: string) {
async handle(context: CommandClient<any, any>, message: { id: string, channel: { id: string }}, emoji: Constants.APIEmoji, userId: string) {
const msg = await context.getMessage(message.channel.id, message.id);
if (msg.author.id !== config.identifiers.targetId || emoji.id !== config.identifiers.emojiId) return;
console.log(`[MESSAGE_REACTION_REMOVE] - ID: ${user}`);
const _user = await getUser(userId);
if (!_user.privacy.optin) return;
await setUserXp(userId, { current: _user.xp.current - 1 });
}
}
+2
View File
@@ -1,6 +1,7 @@
import 'dotenv/config';
import { CommandClient, Constants, Guild, Member, Message, NullCollection, User } from 'athena-prime';
import commands from './commands';
import events from './events';
// ----------
@@ -27,6 +28,7 @@ const client: CommandClient = new CommandClient({
},
});
commands.forEach(command => client.registerCommand(command));
events.forEach(event => client.registerEvent(event));
// ----------
+11
View File
@@ -0,0 +1,11 @@
export type User = {
user_id: string;
privacy: {
optin: boolean;
},
xp: {
current: number;
}
};
+1
View File
@@ -0,0 +1 @@
export * from './database.d';
+56 -1
View File
@@ -1,7 +1,62 @@
import postgres from "postgres";
import { User } from "../types";
// ----------
const client = postgres(process.env.DATABASE_URL);
export default {};
const getDefaultUser = (userId: string): User => ({
user_id: userId,
privacy: {
optin: true,
},
xp: {
current: 0
},
});
export async function getUser(userId: string): Promise<User> {
const [user] = await client<{ user_id: string; data: User }[]>`
INSERT INTO users (user_id, data)
VALUES (${userId}, ${JSON.stringify(getDefaultUser(userId))}::jsonb)
ON CONFLICT (user_id)
DO UPDATE SET user_id = EXCLUDED.user_id
RETURNING user_id, data
`;
return user.data;
}
export async function setUser(user: User): Promise<void> {
await client`
INSERT INTO users (user_id, data)
VALUES (${user.user_id}, ${JSON.stringify(user)}::jsonb)
ON CONFLICT (user_id)
DO UPDATE SET data = EXCLUDED.data
`;
}
export async function deleteUser(userId: string): Promise<void> {
await client`
DELETE FROM users
WHERE user_id = '${userId}'
`;
}
export async function setUserPrivacy(userId: string, privacy: User["privacy"]): Promise<void> {
await client`
UPDATE users
SET data = jsonb_set(data, '{privacy}', ${JSON.stringify(privacy)}::jsonb)
WHERE user_id = '${userId}'
`;
}
export async function setUserXp(userId: string, xp: User["xp"]): Promise<void> {
await client`
UPDATE users
SET data = jsonb_set(data, '{xp}', ${JSON.stringify(xp)}::jsonb)
WHERE user_id = '${userId}'
`;
}