Skip to content

Commit 75224f2

Browse files
peak typescript
1 parent 0a62517 commit 75224f2

File tree

7 files changed

+325
-55
lines changed

7 files changed

+325
-55
lines changed

db.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
{"698221698838691920":{}}
1+
{
2+
"1103862127455895552": {
3+
"apiKey": "any-bpr0h85pM9Rzl0Ga23lmrsqBfLPzr077",
4+
"model": ""
5+
},
6+
"698221698838691920": {
7+
"apiKey": "any-0BjWZzvd3CBLOup9Me6iGmCPbKM1l7QS",
8+
"model": "gpt3"
9+
},
10+
"838717500266381312": {
11+
"apiKey": "any-u4ODkCD1lNyptDwi5vJFrXdNZ1aUGSFt",
12+
"model": ""
13+
}
14+
}

src/commands.json

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
[
22
{
3-
"name": "userid",
4-
"description": "Replies with Pong!"
3+
"name": "apikey",
4+
"description": "get or see your api key"
5+
},
6+
{
7+
"name": "models",
8+
"description": "change the model"
9+
},
10+
{
11+
"name": "custom",
12+
"description": "set a unlisted model",
13+
"options": [
14+
{
15+
"name": "model",
16+
"description": "The custom model to use",
17+
"type": 3,
18+
"required": true
19+
}
20+
]
21+
},
22+
{
23+
"name": "chat",
24+
"description": "chat with ANYGPT"
525
}
626
]

src/history/main.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

src/models.json

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{
2+
"object": "list",
3+
"data": [
4+
{
5+
"id": "gpt-3.5-turbo",
6+
"type": "chat.completions",
7+
"owned_by": "openai",
8+
"endpoint": "/v1/chat/completions",
9+
"premium": false
10+
},
11+
{
12+
"id": "gpt-4",
13+
"type": "chat.completions",
14+
"owned_by": "openai",
15+
"endpoint": "/v1/chat/completions",
16+
"premium": false
17+
},
18+
{
19+
"id": "gpt-4-turbo",
20+
"type": "chat.completions",
21+
"owned_by": "openai",
22+
"endpoint": "/v1/chat/completions",
23+
"premium": true
24+
},
25+
{
26+
"id": "gpt-4o",
27+
"type": "chat.completions",
28+
"owned_by": "openai",
29+
"endpoint": "/v1/chat/completions",
30+
"premium": true
31+
},
32+
{
33+
"id": "gpt-4o-mini",
34+
"type": "chat.completions",
35+
"owned_by": "openai",
36+
"endpoint": "/v1/chat/completions",
37+
"premium": true
38+
},
39+
{
40+
"id": "claude-3-5-sonnet",
41+
"type": "chat.completions",
42+
"owned_by": "anthropic",
43+
"endpoint": "/v1/chat/completions",
44+
"premium": true
45+
},
46+
{
47+
"id": "claude-3-opus",
48+
"type": "chat.completions",
49+
"owned_by": "anthropic",
50+
"endpoint": "/v1/chat/completions",
51+
"premium": true
52+
},
53+
{
54+
"id": "claude-3-sonnet",
55+
"type": "chat.completions",
56+
"owned_by": "anthropic",
57+
"endpoint": "/v1/chat/completions",
58+
"premium": true
59+
},
60+
{
61+
"id": "claude-3-haiku",
62+
"type": "chat.completions",
63+
"owned_by": "anthropic",
64+
"endpoint": "/v1/chat/completions",
65+
"premium": false
66+
},
67+
{
68+
"id": "command-r-plus",
69+
"type": "chat.completions",
70+
"owned_by": "cohere",
71+
"endpoint": "/v1/chat/completions",
72+
"premium": true
73+
},
74+
{
75+
"id": "gemini-1.5-flash",
76+
"type": "chat.completions",
77+
"owned_by": "google",
78+
"endpoint": "/v1/chat/completions",
79+
"premium": false
80+
},
81+
{
82+
"id": "gemini-1.5-pro",
83+
"type": "chat.completions",
84+
"owned_by": "google",
85+
"endpoint": "/v1/chat/completions",
86+
"premium": false
87+
},
88+
{
89+
"id": "llama-3.1-70b",
90+
"type": "chat.completions",
91+
"owned_by": "meta",
92+
"endpoint": "/v1/chat/completions",
93+
"premium": false
94+
},
95+
{
96+
"id": "llama-3.1-405b",
97+
"type": "chat.completions",
98+
"owned_by": "meta",
99+
"endpoint": "/v1/chat/completions",
100+
"premium": false
101+
},
102+
{
103+
"id": "mistral-large-latest",
104+
"type": "chat.completions",
105+
"owned_by": "mistral",
106+
"endpoint": "/v1/chat/completions",
107+
"premium": true
108+
},
109+
{
110+
"id": "llama-3-sonar-large-32k-online",
111+
"type": "chat.completions",
112+
"owned_by": "perplexity",
113+
"endpoint": "/v1/chat/completions",
114+
"premium": false
115+
}
116+
]
117+
}

src/server.ts

+92-37
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Client, GatewayIntentBits, REST, Routes } from 'discord.js';
1+
import { Client, GatewayIntentBits, REST, Routes, ActionRowBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, CommandInteractionOptionResolver, ComponentType } from 'discord.js';
22
import dotenv from 'dotenv';
33
import fs from 'fs';
4-
import axios from 'axios';
54
dotenv.config();
5+
import { apikey, readUserIds, writeUserIds, readModels } from './utils';
66
const commands = JSON.parse(fs.readFileSync('src/commands.json', 'utf-8'));
77

88
const client = new Client({
@@ -32,35 +32,6 @@ async function registerCommands() {
3232
console.error(error);
3333
}
3434
}
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-
}
6435
client.once('ready', async () => {
6536
console.log('online');
6637
await registerCommands();
@@ -71,12 +42,96 @@ client.on('interactionCreate', async interaction => {
7142
const userId = interaction.user.id;
7243
const userIds = readUserIds();
7344

74-
if (interaction.commandName === 'userid') {
45+
if (interaction.commandName === 'apikey') {
7546
await interaction.deferReply({ ephemeral: true });
7647

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+
}
81111
}
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+

src/utils.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import axios from 'axios';
2+
import fs from 'fs';
3+
4+
export async function apikey(name: string) {
5+
try {
6+
const response = await axios.post(
7+
`${process.env.URL}`,
8+
{ name },
9+
{
10+
headers: {
11+
Authorization: `Bearer ${process.env.ADMIN_KEY}`
12+
}
13+
}
14+
);
15+
const key = response.data.result.key;
16+
console.log(key);
17+
return key;
18+
} catch (error) {
19+
console.error(error);
20+
}
21+
}
22+
23+
export function readUserIds() {
24+
if (fs.existsSync('db.json')) {
25+
const data = fs.readFileSync('db.json', 'utf-8');
26+
return JSON.parse(data);
27+
} else {
28+
return {};
29+
}
30+
}
31+
32+
export function writeUserIds(userIds: any) {
33+
const data = JSON.stringify(userIds, null, 2);
34+
fs.writeFileSync('db.json', data, 'utf-8');
35+
}
36+
37+
export async function readModels() {
38+
if (fs.existsSync('src/models.json')) {
39+
const data = fs.readFileSync('src/models.json', 'utf-8');
40+
const models = JSON.parse(data);
41+
return models.data.map((model: any) => model.id);
42+
} else {
43+
return [];
44+
}
45+
}
46+
47+
export function saveUserData(userId: string, apiKey: string, selectedModel: string) {
48+
const userIds = readUserIds();
49+
userIds[userId] = {
50+
apiKey: apiKey,
51+
model: selectedModel
52+
};
53+
writeUserIds(userIds);
54+
}

0 commit comments

Comments
 (0)