1
- import { Client , GatewayIntentBits , REST , Routes } from 'discord.js' ;
1
+ import { Client , GatewayIntentBits , REST , Routes , ActionRowBuilder , StringSelectMenuBuilder , StringSelectMenuOptionBuilder , CommandInteractionOptionResolver , ComponentType } from 'discord.js' ;
2
2
import dotenv from 'dotenv' ;
3
3
import fs from 'fs' ;
4
- import axios from 'axios' ;
5
4
dotenv . config ( ) ;
5
+ import { apikey , readUserIds , writeUserIds , readModels } from './utils' ;
6
6
const commands = JSON . parse ( fs . readFileSync ( 'src/commands.json' , 'utf-8' ) ) ;
7
7
8
8
const client = new Client ( {
@@ -32,35 +32,6 @@ async function registerCommands() {
32
32
console . error ( error ) ;
33
33
}
34
34
}
35
- function readUserIds ( ) {
36
- if ( fs . existsSync ( 'userids.json' ) ) {
37
- const data = fs . readFileSync ( 'db.json' , 'utf-8' ) ;
38
- return JSON . parse ( data ) ;
39
- } else {
40
- return { } ;
41
- }
42
- }
43
- async function apikey ( name : string ) {
44
- try {
45
- const response = await axios . post (
46
- 'https://gpt.anyvm.tech/v1/admin/create' ,
47
- { name } ,
48
- {
49
- headers : {
50
- Authorization : 'Bearer (key-noshow)'
51
- }
52
- }
53
- ) ;
54
- console . log ( response . data ) ;
55
- return response . data ;
56
- } catch ( error ) {
57
- console . error ( error ) ;
58
- }
59
- }
60
- function writeUserIds ( userIds : any ) {
61
- const data = JSON . stringify ( userIds ) ;
62
- fs . writeFileSync ( 'db.json' , data , 'utf-8' ) ;
63
- }
64
35
client . once ( 'ready' , async ( ) => {
65
36
console . log ( 'online' ) ;
66
37
await registerCommands ( ) ;
@@ -71,12 +42,96 @@ client.on('interactionCreate', async interaction => {
71
42
const userId = interaction . user . id ;
72
43
const userIds = readUserIds ( ) ;
73
44
74
- if ( interaction . commandName === 'userid ' ) {
45
+ if ( interaction . commandName === 'apikey ' ) {
75
46
await interaction . deferReply ( { ephemeral : true } ) ;
76
47
77
- userIds [ userId ] = apikey ( userId ) ;
78
- writeUserIds ( userIds ) ;
79
-
80
- await interaction . followUp ( { content : userId , ephemeral : true } ) ;
48
+ if ( Object . keys ( userIds ) . includes ( userId ) ) {
49
+ const existingApiKey = userIds [ userId ] . apiKey ;
50
+ await interaction . followUp ( { content : `Bearer ${ existingApiKey } ` , ephemeral : true } ) ;
51
+ } else {
52
+ const apiKey = await apikey ( userId ) ;
53
+ userIds [ userId ] = { apiKey : apiKey , model : '' } ;
54
+ writeUserIds ( userIds ) ;
55
+ await interaction . followUp ( { content : `Bearer ${ apiKey } ` , ephemeral : true } ) ;
56
+ }
57
+ } else if ( interaction . commandName === 'models' ) {
58
+ await interaction . deferReply ( { ephemeral : true } ) ;
59
+
60
+ if ( ! Object . keys ( userIds ) . includes ( userId ) ) {
61
+ const apiKey = await apikey ( userId ) ;
62
+ userIds [ userId ] = { apiKey : apiKey , model : '' } ;
63
+ await writeUserIds ( userIds ) ;
64
+ await interaction . followUp ( { content : `API key created: Bearer ${ apiKey } ` , ephemeral : true } ) ;
65
+ }
66
+
67
+ const options = interaction . options as CommandInteractionOptionResolver ;
68
+ const selectedModel = options . getString ( 'model' ) ;
69
+
70
+ if ( selectedModel ) {
71
+ userIds [ userId ] . model = selectedModel ;
72
+ await writeUserIds ( userIds ) ;
73
+ await interaction . followUp ( { content : `Model ${ selectedModel } selected.` , ephemeral : true } ) ;
74
+ } else {
75
+ const models = await readModels ( ) ;
76
+ const selectMenu = new StringSelectMenuBuilder ( )
77
+ . setCustomId ( interaction . id )
78
+ . setPlaceholder ( 'Select a model' )
79
+ . addOptions (
80
+ models . map ( ( model : string ) =>
81
+ new StringSelectMenuOptionBuilder ( )
82
+ . setLabel ( model )
83
+ . setValue ( model )
84
+ )
85
+ ) ;
86
+
87
+ const row = new ActionRowBuilder < StringSelectMenuBuilder > ( ) . addComponents ( selectMenu ) ;
88
+
89
+ await interaction . editReply ( {
90
+ content : 'Please select a model:' ,
91
+ components : [ row ] ,
92
+ } ) ;
93
+
94
+ const collector = interaction . channel ?. createMessageComponentCollector ( {
95
+ componentType : ComponentType . StringSelect ,
96
+ filter : ( i ) => i . user . id === userId && i . customId === interaction . id ,
97
+ time : 60000
98
+ } ) ;
99
+
100
+ if ( collector ) {
101
+ collector . on ( 'collect' , async i => {
102
+ const selectedModel = i . values [ 0 ] ;
103
+ userIds [ userId ] . model = selectedModel ;
104
+ await writeUserIds ( userIds ) ;
105
+ await i . update ( { content : `Model ${ selectedModel } selected.` , components : [ ] } ) ;
106
+ } ) ;
107
+ } else {
108
+ await interaction . followUp ( { content : 'No model selected.' , ephemeral : true } ) ;
109
+ }
110
+ }
81
111
}
82
- } ) ;
112
+ else if ( interaction . commandName === 'custom' ) {
113
+ await interaction . deferReply ( { ephemeral : true } ) ;
114
+
115
+ if ( ! Object . keys ( userIds ) . includes ( userId ) ) {
116
+ const apiKey = await apikey ( userId ) ;
117
+ userIds [ userId ] = { apiKey : apiKey , model : '' } ;
118
+ await writeUserIds ( userIds ) ;
119
+ await interaction . followUp ( { content : `API key created: Bearer ${ apiKey } ` , ephemeral : true } ) ;
120
+ }
121
+
122
+ const options = interaction . options as CommandInteractionOptionResolver ;
123
+ const customModel = options . getString ( 'model' ) ;
124
+
125
+ if ( customModel ) {
126
+ userIds [ userId ] . model = customModel ;
127
+ await writeUserIds ( userIds ) ;
128
+ await interaction . followUp ( { content : `Custom model ${ customModel } selected.` , ephemeral : true } ) ;
129
+ } else {
130
+ await interaction . followUp ( { content : 'Please provide a custom model name.' , ephemeral : true } ) ;
131
+ }
132
+ }
133
+ else if ( interaction . commandName === 'chat' ) {
134
+
135
+ }
136
+ } ) ;
137
+
0 commit comments