spacecommands 3.5.5 → 3.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
+ const message_1 = __importDefault(require("./models/message"));
39
40
  const languages_1 = __importDefault(require("./models/languages"));
40
41
  const user_languages_1 = __importDefault(require("./models/user-languages"));
41
42
  const defualtMessages = require('../messages.json');
@@ -49,6 +50,22 @@ class MessageHandler {
49
50
  this._instance = instance;
50
51
  (async () => {
51
52
  this._messages = messagePath ? await Promise.resolve(`${messagePath}`).then(s => __importStar(require(s))) : defualtMessages;
53
+ if (instance.isDBConnected()) {
54
+ const dbMessages = await message_1.default.find();
55
+ for (const msg of dbMessages) {
56
+ this._messages[msg._id] = msg.text;
57
+ }
58
+ const results = await languages_1.default.find();
59
+ // @ts-ignore
60
+ for (const { _id: guildId, language } of results) {
61
+ this._guildLanguages.set(guildId, language);
62
+ }
63
+ const userResults = await user_languages_1.default.find();
64
+ // @ts-ignore
65
+ for (const { _id: userId, language } of userResults) {
66
+ this._userLanguages.set(userId, language);
67
+ }
68
+ }
52
69
  const languages = this._messages['LANGUAGE_NOT_SUPPORTED'] || this._messages['NEW_LANGUAGE'];
53
70
  if (languages) {
54
71
  for (const language of Object.keys(languages)) {
@@ -70,18 +87,6 @@ class MessageHandler {
70
87
  if (!this._languages.includes(instance.defaultLanguage)) {
71
88
  throw new Error(`The current default language defined is not supported.`);
72
89
  }
73
- if (instance.isDBConnected()) {
74
- const results = await languages_1.default.find();
75
- // @ts-ignore
76
- for (const { _id: guildId, language } of results) {
77
- this._guildLanguages.set(guildId, language);
78
- }
79
- const userResults = await user_languages_1.default.find();
80
- // @ts-ignore
81
- for (const { _id: userId, language } of userResults) {
82
- this._userLanguages.set(userId, language);
83
- }
84
- }
85
90
  })();
86
91
  }
87
92
  languages() {
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // @ts-nocheck
4
+ const supabase_1 = require("../supabase");
5
+ const TABLE_NAME = 'spacecommands_messages';
6
+ exports.default = {
7
+ async find(filter = {}) {
8
+ const client = (0, supabase_1.getSupabaseClient)();
9
+ if (!client)
10
+ return [];
11
+ let query = client.from(TABLE_NAME).select('*');
12
+ if (filter._id) {
13
+ query = query.eq('id', filter._id);
14
+ }
15
+ const { data, error } = await query;
16
+ if (error) {
17
+ console.error('SpaceCommands > Error fetching messages:', error);
18
+ return [];
19
+ }
20
+ return (data || []).map((row) => ({
21
+ _id: row.id,
22
+ text: row.text,
23
+ }));
24
+ },
25
+ async findOne(filter) {
26
+ const client = (0, supabase_1.getSupabaseClient)();
27
+ if (!client)
28
+ return null;
29
+ const { data, error } = await client
30
+ .from(TABLE_NAME)
31
+ .select('*')
32
+ .eq('id', filter._id)
33
+ .single();
34
+ if (error) {
35
+ if (error.code === 'PGRST116')
36
+ return null;
37
+ console.error('SpaceCommands > Error finding message:', error);
38
+ return null;
39
+ }
40
+ return {
41
+ _id: data.id,
42
+ text: data.text,
43
+ };
44
+ },
45
+ async findOneAndUpdate(filter, update, options = {}) {
46
+ const client = (0, supabase_1.getSupabaseClient)();
47
+ if (!client)
48
+ return null;
49
+ const id = filter._id;
50
+ const text = update.text || update.$set?.text;
51
+ const { data, error } = await client
52
+ .from(TABLE_NAME)
53
+ .upsert({ id: id, text }, { onConflict: 'id' })
54
+ .select()
55
+ .single();
56
+ if (error) {
57
+ console.error('SpaceCommands > Error upserting message:', error);
58
+ return null;
59
+ }
60
+ return {
61
+ _id: data.id,
62
+ text: data.text,
63
+ };
64
+ },
65
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spacecommands",
3
- "version": "3.5.5",
3
+ "version": "3.5.6",
4
4
  "main": "dist/index.js",
5
5
  "types": "./typings.d.ts",
6
6
  "typings": "./typings.d.ts",