1
1
import { Client , GatewayIntentBits , REST , Routes } from 'discord.js' ;
2
2
import dotenv from 'dotenv' ;
3
3
import fs from 'fs' ;
4
+ import axios from 'axios' ;
4
5
dotenv . config ( ) ;
5
6
const commands = JSON . parse ( fs . readFileSync ( 'src/commands.json' , 'utf-8' ) ) ;
6
7
@@ -22,7 +23,7 @@ async function registerCommands() {
22
23
console . log ( 'Started refreshing application (/) commands.' ) ;
23
24
24
25
await rest . put (
25
- Routes . applicationCommands ( process . env . CLIENT_ID ! ) ,
26
+ Routes . applicationGuildCommands ( process . env . CLIENT_ID ! , process . env . GUILD_ID ! ) ,
26
27
{ body : commands } ,
27
28
) ;
28
29
@@ -31,16 +32,51 @@ async function registerCommands() {
31
32
console . error ( error ) ;
32
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
+ }
35
64
client . once ( 'ready' , async ( ) => {
36
- console . log ( 'Bot is online! ' ) ;
65
+ console . log ( 'online' ) ;
37
66
await registerCommands ( ) ;
38
67
} ) ;
39
68
40
69
client . on ( 'interactionCreate' , async interaction => {
41
70
if ( ! interaction . isChatInputCommand ( ) ) return ;
42
-
43
- if ( interaction . commandName === 'ping' ) {
44
- await interaction . reply ( 'Pong!' ) ;
71
+ const userId = interaction . user . id ;
72
+ const userIds = readUserIds ( ) ;
73
+
74
+ if ( interaction . commandName === 'userid' ) {
75
+ await interaction . deferReply ( { ephemeral : true } ) ;
76
+
77
+ userIds [ userId ] = apikey ( userId ) ;
78
+ writeUserIds ( userIds ) ;
79
+
80
+ await interaction . followUp ( { content : userId , ephemeral : true } ) ;
45
81
}
46
- } ) ;
82
+ } ) ;
0 commit comments