mirror of
https://github.com/nx-bat/pet-the-pond.git
synced 2026-09-22 10:29:11 -04:00
(core): fix commands, events & database.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import init from "./init";
|
||||
import optin from "./optin";
|
||||
import optout from "./optout";
|
||||
|
||||
export default [optin, optout];
|
||||
export default [init, optin, optout];
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from "athena-prime";
|
||||
import config from "../config";
|
||||
|
||||
@SlashCommand(
|
||||
new CommandBuilder('init', 'Event setup command.')
|
||||
.setIntegrationTypes(Constants.ApplicationIntegrationType.GuildInstall)
|
||||
.setContexts(Constants.InteractionContextType.Guild)
|
||||
.setCommandType(Constants.ApplicationCommandType.ChatInput)
|
||||
)
|
||||
class InitCommand extends Command<CommandClient> {
|
||||
users = ['1363132678022631428'];
|
||||
|
||||
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
|
||||
await context.createMessage(interaction.channel.id, {
|
||||
embed: {
|
||||
color: 0xe77ed1,
|
||||
|
||||
title: 'Pet the Pond!',
|
||||
description: `
|
||||
There's a Pond that needs petting, and a leaderboard to show off your level of appreciation.
|
||||
|
||||
**How does it work?**
|
||||
On any message sent by <@${config.identifiers.targetId}>, react with the <:pet:448912932340891670> emoji to receive a point.
|
||||
|
||||
**What do you get?**
|
||||
You get appreciation and proof you appreciate Pond the most.
|
||||
|
||||
**What if I don't want to be involved?**
|
||||
You can opt in & out using the \`/optin\` & \`/optout\` commands.
|
||||
`,
|
||||
|
||||
footer: {
|
||||
text: `Made by nxbat @ Archwing`,
|
||||
icon_url: 'https://femboytrain.ing/nxbat-archwing'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
await interaction.createMessage({ content: 'Done.', flags: Constants.MessageFlags.Ephemeral });
|
||||
}
|
||||
}
|
||||
|
||||
export default new InitCommand('init');
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from "athena-prime";
|
||||
import { setUserPrivacy } from "../utils";
|
||||
import { getUser, setUserPrivacy } from "../utils";
|
||||
|
||||
@SlashCommand(
|
||||
new CommandBuilder('optin', 'Opt into the Pet the Pond event.')
|
||||
@@ -11,11 +11,13 @@ class OptInCommand extends Command<CommandClient> {
|
||||
cooldown = 5;
|
||||
|
||||
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
|
||||
await setUserPrivacy(interaction.member.id, { optin: true });
|
||||
await interaction.defer(true);
|
||||
|
||||
const _user = await getUser(interaction.user.id);
|
||||
await setUserPrivacy(_user.user_id, { optin: true });
|
||||
|
||||
await interaction.createMessage({
|
||||
content: "Successfully opted in. Interactions will be acknowledged.",
|
||||
flags: Constants.MessageFlags.Ephemeral
|
||||
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 { setUserPrivacy } from "../utils";
|
||||
import { getUser, setUserPrivacy } from "../utils";
|
||||
|
||||
@SlashCommand(
|
||||
new CommandBuilder('optout', 'Opt out of the Pet the Pond event.')
|
||||
@@ -11,11 +11,13 @@ class OptOutCommand extends Command<CommandClient> {
|
||||
cooldown = 5;
|
||||
|
||||
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction) {
|
||||
await setUserPrivacy(interaction.member.id, { optin: false });
|
||||
await interaction.defer(true);
|
||||
|
||||
const _user = await getUser(interaction.user.id);
|
||||
await setUserPrivacy(_user.user_id, { optin: false });
|
||||
|
||||
await interaction.createMessage({
|
||||
content: "Successfully opted out. Interactions will not be acknowledged.",
|
||||
flags: Constants.MessageFlags.Ephemeral
|
||||
content: "Successfully opted out. You won't receive any points for petting :<"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ class MessageReactionAddEvent extends Event<CommandClient> {
|
||||
const _user = await getUser(member.id);
|
||||
if (!_user.privacy.optin) return;
|
||||
|
||||
await setUserXp(member.id, { current: _user.xp.current + 1 });
|
||||
await setUserXp(_user.user_id, { current: _user.xp.current + 1 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ class MessageReactionRemoveEvent extends Event<CommandClient> {
|
||||
if (msg.author.id !== config.identifiers.targetId || emoji.id !== config.identifiers.emojiId) return;
|
||||
|
||||
const _user = await getUser(userId);
|
||||
if (!_user.privacy.optin) return;
|
||||
if (!_user.privacy.optin || _user.xp.current <= 0) return;
|
||||
|
||||
await setUserXp(userId, { current: _user.xp.current - 1 });
|
||||
await setUserXp(_user.user_id, { current: _user.xp.current - 1 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -28,8 +28,8 @@ const client: CommandClient = new CommandClient({
|
||||
},
|
||||
});
|
||||
|
||||
commands.forEach(command => client.registerCommand(command));
|
||||
events.forEach(event => client.registerEvent(event));
|
||||
commands.forEach(command => client.registerCommand(command, true));
|
||||
events.forEach(event => client.registerEvent(event, true));
|
||||
|
||||
// ----------
|
||||
|
||||
|
||||
+28
-16
@@ -1,38 +1,50 @@
|
||||
import postgres from "postgres";
|
||||
import { User } from "../types";
|
||||
|
||||
// ----------
|
||||
|
||||
const client = postgres(process.env.DATABASE_URL);
|
||||
|
||||
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
|
||||
let [user] = await client<{ user_id: string; data: User }[]>`
|
||||
SELECT user_id, data
|
||||
FROM users
|
||||
WHERE user_id = ${userId}
|
||||
`;
|
||||
|
||||
if (!user) {
|
||||
const defaultUser = getDefaultUser(userId);
|
||||
|
||||
[user] = await client<{ user_id: string; data: User }[]>`
|
||||
INSERT INTO users (user_id, data)
|
||||
VALUES (${userId}, ${client.json(defaultUser)})
|
||||
ON CONFLICT (user_id)
|
||||
DO NOTHING
|
||||
RETURNING user_id, data
|
||||
`;
|
||||
|
||||
if (!user) {
|
||||
[user] = await client<{ user_id: string; data: User }[]>`
|
||||
SELECT user_id, data FROM users WHERE user_id = ${userId}
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
VALUES (${user.user_id}, ${client.json(user)})
|
||||
ON CONFLICT (user_id)
|
||||
DO UPDATE SET data = EXCLUDED.data
|
||||
`;
|
||||
@@ -41,22 +53,22 @@ export async function setUser(user: User): Promise<void> {
|
||||
export async function deleteUser(userId: string): Promise<void> {
|
||||
await client`
|
||||
DELETE FROM users
|
||||
WHERE user_id = '${userId}'
|
||||
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}'
|
||||
SET data = jsonb_set(data, '{privacy}', ${client.json(privacy)})
|
||||
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}'
|
||||
SET data = jsonb_set(data, '{xp}', ${client.json(xp)})
|
||||
WHERE user_id = ${userId}
|
||||
`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user