Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ops: renames guilds-plugins table to use _, fixes cronjob refresh /as… #54

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/controllers/bot/guilds.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Hans } from './../../index'
import { insertGuildPlugin, resolveGuildPlugins } from './plugins.controller'

export type GuildSettings = Database['public']['Tables']['guilds']['Row']
export type GuildPlugin = Database['public']['Tables']['guilds-plugins']['Row']
export type GuildPlugin = Database['public']['Tables']['guilds_plugins']['Row']

/** Fetches guild in the DB, if found, sets it as a cache for CACHE_TTL and returns it.
* @param guildId string with the Guild ID
Expand Down
23 changes: 13 additions & 10 deletions src/controllers/bot/plugins.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const insertGuildPlugin = async (guild_id: string) => {

// check if a row with the same plugin name and guild_id exists
const { data: existingGuildPlugins } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.select('*')
.in(
'name',
Expand All @@ -34,7 +34,7 @@ export const insertGuildPlugin = async (guild_id: string) => {
})

if (newGuildPlugins.length > 0) {
await supabase.from('guilds-plugins').upsert(newGuildPlugins)
await supabase.from('guilds_plugins').upsert(newGuildPlugins)
}
} catch (error) {
console.error('❌ ERROR: insertGuildPlugin', error)
Expand All @@ -45,7 +45,7 @@ export const findGuildPlugins = async (guild_id: string) => {
try {
const { data, error } = await supabase
.from('guilds')
.select('*, guilds-plugins(*, plugins(enabled, description, premium))')
.select('*, guilds_plugins(*, plugins(enabled, description, premium))')
.eq('guild_id', guild_id)

if (error) throw error
Expand All @@ -69,11 +69,14 @@ export const resolveGuildPlugins = async (
try {
const { data: guildPlugin } = await supabase
.from('guilds')
.select('*, guilds-plugins(*, plugins(enabled, description, premium))')
.select('*, guilds_plugins(*, plugins(enabled, description, premium))')
.eq('guild_id', guild_id)
.single()

const matchingPlugin = guildPlugin?.['guilds-plugins'].find(
// INFO: Not sure why this fails below, `guilds_plugins` is an array but the array methods wont show.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const matchingPlugin = guildPlugin.guilds_plugins.find(
(ele: GuildPlugin) => ele.name === pluginName,
)

Expand Down Expand Up @@ -102,7 +105,7 @@ export const toggleGuildPlugin = async (
) => {
try {
const { data, error } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.update({ enabled: toggle })
.eq('name', name)
.eq('owner', interaction.guildId)
Expand Down Expand Up @@ -137,7 +140,7 @@ export const pluginChatGPTSettings = async (
) => {
try {
const { data: currentSettings } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.select('*')
.eq('name', 'chatGtp')
.eq('owner', interaction.guildId)
Expand All @@ -146,7 +149,7 @@ export const pluginChatGPTSettings = async (
const _metadata = JSON.parse(JSON.stringify(currentSettings?.metadata)) || {}

const { error } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.update({
metadata: {
..._metadata,
Expand Down Expand Up @@ -193,7 +196,7 @@ export const pluginThreadsSettings = async ({ interaction, metadata }: PluginsTh
})

const { data: guildsPlugins, error } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.select('metadata')
.eq('name', 'threads')
.eq('owner', interaction.guildId)
Expand Down Expand Up @@ -230,7 +233,7 @@ export const pluginThreadsSettings = async ({ interaction, metadata }: PluginsTh

// Update the metadata in the database
const { error: updateError } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.update({ metadata: updatedMetadata })
.eq('name', 'threads')
.eq('owner', interaction.guildId)
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/plugins/chat-gpt3.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const chatGptCommandHandler = async (
footer: {
text: `Tokens: ${answer?.token} | Price: $${((answer?.token / 1000) * 0.002).toFixed(
6,
)} ${!guild.premium ?? `| ${usage} usages left for today`}`,
)} ${!guild.premium ? `| ${usage} usages left for today` : ''}`,
},
color: DEFAULT_COLOR,
},
Expand All @@ -172,7 +172,7 @@ export const chatGptUsage = async (
): Promise<GuildPlugin | any> => {
try {
const { data: currentSettings } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.select('*')
.eq('name', 'chatGtp')
.eq('owner', guild_id)
Expand All @@ -182,7 +182,7 @@ export const chatGptUsage = async (

if (guildPlugin === null) {
const { data } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.update({ metadata: { ..._metadata, usage: CHATGPT_COMMANDS_USAGE_DAILY - 1 } })
.eq('owner', guild_id)
.eq('name', 'chatGtp')
Expand All @@ -191,7 +191,7 @@ export const chatGptUsage = async (
return data
} else {
const { data } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.update({ metadata: { ..._metadata, usage: guildPlugin.usage - 1 } })
.eq('owner', guild_id)
.eq('name', 'chatGtp')
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/plugins/guild_activity.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import supabase from '../../libs/supabase'
export const guildActivitySetChannel = async (interaction: CommandInteraction, channel: string) => {
try {
const { data, error } = await supabase
.from('guilds-plugins')
.from('guilds_plugins')
.update({ metadata: { channelId: channel } })
.eq('name', `serverMembersActivity`)
.eq('owner', interaction.guildId)
Expand Down
2 changes: 1 addition & 1 deletion src/models/plugins.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Database } from '../types/database.types'

export type Plugins = Database['public']['Tables']['plugins']['Row']
export type GuildPlugins = Database['public']['Tables']['guilds-plugins']['Row']
export type GuildPlugins = Database['public']['Tables']['guilds_plugins']['Row']

export type GenericPluginParts = Omit<Plugins, 'id' | 'name'>
const genericStructure: GenericPluginParts = {
Expand Down
5 changes: 4 additions & 1 deletion src/types/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface Database {
premium?: boolean | null
}
}
'guilds-plugins': {
guilds_plugins: {
Row: {
created_at: string | null
enabled: boolean | null
Expand Down Expand Up @@ -141,5 +141,8 @@ export interface Database {
Enums: {
[_ in never]: never
}
CompositeTypes: {
[_ in never]: never
}
}
}
89 changes: 0 additions & 89 deletions supabase/migrations/20230509212317_public.sql

This file was deleted.

5 changes: 0 additions & 5 deletions supabase/migrations/20230510095338_public.sql

This file was deleted.

33 changes: 16 additions & 17 deletions supabase/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
create extension if not exists "pg_cron" with schema "public" version '1.4-1';

create table "public"."configs" (
"id" integer not null,
"bot_id" text not null,
Expand All @@ -11,7 +13,7 @@ create table "public"."configs" (
"notify_channel_id" text,
"perma_invite" text,
"website" text,
"debug_channel" text
"monitoring_channel_id" text
);


Expand All @@ -29,7 +31,7 @@ create table "public"."guilds" (

alter table "public"."guilds" enable row level security;

create table "public"."guilds-plugins" (
create table "public"."guilds_plugins" (
"id" integer generated by default as identity not null,
"name" text,
"owner" text,
Expand All @@ -39,7 +41,7 @@ create table "public"."guilds-plugins" (
);


alter table "public"."guilds-plugins" enable row level security;
alter table "public"."guilds_plugins" enable row level security;

create table "public"."plugins" (
"id" integer generated by default as identity not null,
Expand All @@ -60,7 +62,7 @@ CREATE UNIQUE INDEX guilds_guild_id_key ON public.guilds USING btree (guild_id);

CREATE UNIQUE INDEX guilds_pkey ON public.guilds USING btree (id);

CREATE UNIQUE INDEX guilds_plugins_pkey ON public."guilds-plugins" USING btree (id);
CREATE UNIQUE INDEX guilds_plugins_pkey ON public.guilds_plugins USING btree (id);

CREATE UNIQUE INDEX plugins_name_key ON public.plugins USING btree (name);

Expand All @@ -70,37 +72,34 @@ alter table "public"."configs" add constraint "configs_pkey" PRIMARY KEY using i

alter table "public"."guilds" add constraint "guilds_pkey" PRIMARY KEY using index "guilds_pkey";

alter table "public"."guilds-plugins" add constraint "guilds_plugins_pkey" PRIMARY KEY using index "guilds_plugins_pkey";
alter table "public"."guilds_plugins" add constraint "guilds_plugins_pkey" PRIMARY KEY using index "guilds_plugins_pkey";

alter table "public"."plugins" add constraint "plugins_pkey" PRIMARY KEY using index "plugins_pkey";

alter table "public"."guilds" add constraint "guilds_guild_id_key" UNIQUE using index "guilds_guild_id_key";

alter table "public"."guilds-plugins" add constraint "guilds-plugins_name_fkey" FOREIGN KEY (name) REFERENCES plugins(name) not valid;
alter table "public"."guilds_plugins" add constraint "guilds_plugins_name_fkey" FOREIGN KEY (name) REFERENCES plugins(name) not valid;

alter table "public"."guilds-plugins" validate constraint "guilds-plugins_name_fkey";
alter table "public"."guilds_plugins" validate constraint "guilds_plugins_name_fkey";

alter table "public"."guilds-plugins" add constraint "guilds-plugins_owner_fkey" FOREIGN KEY (owner) REFERENCES guilds(guild_id) ON DELETE CASCADE not valid;
alter table "public"."guilds_plugins" add constraint "guilds_plugins_owner_fkey" FOREIGN KEY (owner) REFERENCES guilds(guild_id) ON DELETE CASCADE not valid;

alter table "public"."guilds-plugins" validate constraint "guilds-plugins_owner_fkey";
alter table "public"."guilds_plugins" validate constraint "guilds_plugins_owner_fkey";

alter table "public"."plugins" add constraint "plugins_name_key" UNIQUE using index "plugins_name_key";


-- Functions

-- Resets the usage of all plugins to 100
CREATE OR REPLACE FUNCTION resetPluginUsage()
CREATE OR REPLACE FUNCTION reset_chat_gpt_plugin()
RETURNS void AS
$$
BEGIN
UPDATE public."guilds_plugins"
UPDATE guilds_plugins
SET metadata = jsonb_set(metadata, '{usage}', '100'::jsonb)
WHERE TRUE;
WHERE name = 'chatGtp';
END;
$$
LANGUAGE plpgsql;

-- Triggers
CREATE EXTENSION IF NOT EXISTS pg_cron;
SELECT cron.schedule('0 0 * * *', 'SELECT resetPluginUsage()');
-- Crons
SELECT cron.schedule('0 0 * * *', 'SELECT reset_chat_gpt_plugin()');