mirror of
https://github.com/nx-bat/pet-the-pond.git
synced 2026-09-22 10:29:11 -04:00
(events): postgres sucks.
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
NODE_ENV=production
|
||||
|
||||
DATABASE_URL=
|
||||
DEV_GUILD=
|
||||
DISCORD_TOKEN=
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import optin from "./optin";
|
||||
import optout from "./optout";
|
||||
|
||||
export default [optin, optout];
|
||||
@@ -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');
|
||||
@@ -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');
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
// ----------
|
||||
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
export type User = {
|
||||
user_id: string;
|
||||
|
||||
privacy: {
|
||||
optin: boolean;
|
||||
},
|
||||
|
||||
xp: {
|
||||
current: number;
|
||||
}
|
||||
};
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
export * from './database.d';
|
||||
+56
-1
@@ -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}'
|
||||
`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user