code 👍

This commit is contained in:
2026-08-31 10:23:12 +00:00
parent bba53c0bc0
commit d81ddb021d
9 changed files with 44 additions and 18 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
import { getHighestPets } from '../utils';
import { database } from '../utils';
// ----------
@@ -15,7 +15,7 @@ class LeaderboardCommand extends Command<CommandClient> {
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
await interaction.defer();
const _data = await getHighestPets();
const _data = await database.getHighestPets();
const description = _data.length
? _data
.map((entry, index) => `**${index + 1}.** <@${entry.userId}> — **${entry.pets.toLocaleString()}** pets!`)
+2 -2
View File
@@ -1,5 +1,5 @@
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
import { updateUserSettings } from '../../utils';
import { database } from '../../utils';
// ----------
@@ -15,7 +15,7 @@ class OptInCommand extends Command<CommandClient> {
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
await interaction.defer(true);
await updateUserSettings(interaction.user.id, { participating: true });
await database.updateUserSettings(interaction.user.id, { participating: true });
await interaction.createMessage({
content: "Successfully opted in. You'll receive points for your pets!",
+2 -2
View File
@@ -1,5 +1,5 @@
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
import { updateUserSettings } from '../../utils';
import { database } from '../../utils';
// ----------
@@ -15,7 +15,7 @@ class OptOutCommand extends Command<CommandClient> {
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
await interaction.defer(true);
await updateUserSettings(interaction.user.id, { participating: false });
await database.updateUserSettings(interaction.user.id, { participating: false });
await interaction.createMessage({
content: "Successfully opted out. You won't receive any points for petting :<",
+3 -3
View File
@@ -1,5 +1,5 @@
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
import { getPetPosition, getUser } from '../utils';
import { database } from '../utils';
// ----------
@@ -15,8 +15,8 @@ class ProfileCommand extends Command<CommandClient> {
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
await interaction.defer();
const _user = await getUser(interaction.user.id);
const position = await getPetPosition(interaction.user.id);
const _user = await database.getUser(interaction.user.id);
const position = await database.getPetPosition(interaction.user.id);
await interaction.createMessage({
embeds: [
+3 -3
View File
@@ -1,6 +1,6 @@
import { CommandClient, Constants, Event, Member, Message } from 'athena-prime';
import config from '../../config';
import { getUser, updateUserStatistics } from '../../utils';
import { database } from '../../utils';
// ----------
@@ -11,10 +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;
const _user = await getUser(member.id);
const _user = await database.getUser(member.id);
if (!_user.settings.participating) return;
await updateUserStatistics(member.id, { pets: _user.statistics.pets + 1 });
await database.updateUserStatistics(member.id, { pets: _user.statistics.pets + 1 });
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
import { CommandClient, Constants, Event } from 'athena-prime';
import config from '../../config';
import { getUser, updateUserStatistics } from '../../utils';
import { database } from '../../utils';
// ----------
@@ -16,11 +16,11 @@ class MessageReactionRemoveEvent 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;
const _user = await getUser(userId);
const _user = await database.getUser(userId);
if (!_user.settings.participating) return;
if (_user.statistics.pets >= 0) return;
await updateUserStatistics(userId, { pets: _user.statistics.pets - 1 });
await database.updateUserStatistics(userId, { pets: _user.statistics.pets - 1 });
}
}
+13 -2
View File
@@ -3,7 +3,7 @@ import { User } from '../types';
const client = postgres(process.env.DATABASE_URL);
const getDefaultUser = (userId: string): User => ({
const getDefaultUser = (): User => ({
settings: {
participating: true,
},
@@ -13,6 +13,15 @@ const getDefaultUser = (userId: string): User => ({
},
});
export async function init(): Promise<void> {
await client`
CREATE TABLE IF NOT EXISTS users (
user_id TEXT PRIMARY KEY,
data JSONB NOT NULL
)
`;
}
export async function getUser(userId: string): Promise<User> {
let [user] = await client<{ user_id: string; data: User }[]>`
SELECT user_id, data FROM users
@@ -23,11 +32,13 @@ export async function getUser(userId: string): Promise<User> {
[user] = await client<{ user_id: string; data: User }[]>`
INSERT INTO users (user_id, data)
VALUES (${userId}, ${client.json(getDefaultUser(userId))})
VALUES (${userId}, ${client.json(getDefaultUser())})
ON CONFLICT (user_id)
DO NOTHING
RETURNING user_id, data
`;
return user?.data ?? getDefaultUser();
}
export async function setUser(userId: string, data: User): Promise<void> {
+1 -1
View File
@@ -1 +1 @@
export * from './database';
export * as database from './database';