mirror of
https://github.com/nx-bat/pet-the-pond.git
synced 2026-09-22 10:29:11 -04:00
code 👍
This commit is contained in:
@@ -3,3 +3,18 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
build: .
|
build: .
|
||||||
env_file: .env
|
env_file: .env
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:17
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
POSTGRES_DB: postgres
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
|
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) {
|
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
|
||||||
await interaction.defer();
|
await interaction.defer();
|
||||||
|
|
||||||
const _data = await getHighestPets();
|
const _data = await database.getHighestPets();
|
||||||
const description = _data.length
|
const description = _data.length
|
||||||
? _data
|
? _data
|
||||||
.map((entry, index) => `**${index + 1}.** <@${entry.userId}> — **${entry.pets.toLocaleString()}** pets!`)
|
.map((entry, index) => `**${index + 1}.** <@${entry.userId}> — **${entry.pets.toLocaleString()}** pets!`)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
|
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) {
|
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
|
||||||
await interaction.defer(true);
|
await interaction.defer(true);
|
||||||
|
|
||||||
await updateUserSettings(interaction.user.id, { participating: true });
|
await database.updateUserSettings(interaction.user.id, { participating: true });
|
||||||
|
|
||||||
await interaction.createMessage({
|
await interaction.createMessage({
|
||||||
content: "Successfully opted in. You'll receive points for your pets!",
|
content: "Successfully opted in. You'll receive points for your pets!",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
|
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) {
|
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
|
||||||
await interaction.defer(true);
|
await interaction.defer(true);
|
||||||
|
|
||||||
await updateUserSettings(interaction.user.id, { participating: false });
|
await database.updateUserSettings(interaction.user.id, { participating: false });
|
||||||
|
|
||||||
await interaction.createMessage({
|
await interaction.createMessage({
|
||||||
content: "Successfully opted out. You won't receive any points for petting :<",
|
content: "Successfully opted out. You won't receive any points for petting :<",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime';
|
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) {
|
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
|
||||||
await interaction.defer();
|
await interaction.defer();
|
||||||
|
|
||||||
const _user = await getUser(interaction.user.id);
|
const _user = await database.getUser(interaction.user.id);
|
||||||
const position = await getPetPosition(interaction.user.id);
|
const position = await database.getPetPosition(interaction.user.id);
|
||||||
|
|
||||||
await interaction.createMessage({
|
await interaction.createMessage({
|
||||||
embeds: [
|
embeds: [
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { CommandClient, Constants, Event, Member, Message } from 'athena-prime';
|
import { CommandClient, Constants, Event, Member, Message } from 'athena-prime';
|
||||||
import config from '../../config';
|
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);
|
const msg = await context.getMessage(message.channel.id, message.id);
|
||||||
if (msg.author.id !== config.identifiers.targetId || emoji.id !== config.identifiers.emojiId) return;
|
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;
|
if (!_user.settings.participating) return;
|
||||||
|
|
||||||
await updateUserStatistics(member.id, { pets: _user.statistics.pets + 1 });
|
await database.updateUserStatistics(member.id, { pets: _user.statistics.pets + 1 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { CommandClient, Constants, Event } from 'athena-prime';
|
import { CommandClient, Constants, Event } from 'athena-prime';
|
||||||
import config from '../../config';
|
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);
|
const msg = await context.getMessage(message.channel.id, message.id);
|
||||||
if (msg.author.id !== config.identifiers.targetId || emoji.id !== config.identifiers.emojiId) return;
|
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.settings.participating) return;
|
||||||
|
|
||||||
if (_user.statistics.pets >= 0) 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
@@ -3,7 +3,7 @@ import { User } from '../types';
|
|||||||
|
|
||||||
const client = postgres(process.env.DATABASE_URL);
|
const client = postgres(process.env.DATABASE_URL);
|
||||||
|
|
||||||
const getDefaultUser = (userId: string): User => ({
|
const getDefaultUser = (): User => ({
|
||||||
settings: {
|
settings: {
|
||||||
participating: true,
|
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> {
|
export async function getUser(userId: string): Promise<User> {
|
||||||
let [user] = await client<{ user_id: string; data: User }[]>`
|
let [user] = await client<{ user_id: string; data: User }[]>`
|
||||||
SELECT user_id, data FROM users
|
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 }[]>`
|
[user] = await client<{ user_id: string; data: User }[]>`
|
||||||
INSERT INTO users (user_id, data)
|
INSERT INTO users (user_id, data)
|
||||||
VALUES (${userId}, ${client.json(getDefaultUser(userId))})
|
VALUES (${userId}, ${client.json(getDefaultUser())})
|
||||||
ON CONFLICT (user_id)
|
ON CONFLICT (user_id)
|
||||||
DO NOTHING
|
DO NOTHING
|
||||||
RETURNING user_id, data
|
RETURNING user_id, data
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
return user?.data ?? getDefaultUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setUser(userId: string, data: User): Promise<void> {
|
export async function setUser(userId: string, data: User): Promise<void> {
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
export * from './database';
|
export * as database from './database';
|
||||||
|
|||||||
Reference in New Issue
Block a user