Skip to content

Commit

Permalink
Add code command (4nt0n10M4#9)
Browse files Browse the repository at this point in the history
* `code` command to get code suggestions using codex api.

+ added requirement `axios`
  • Loading branch information
4nt0n10M4 authored Nov 18, 2021
2 parents abd96d4 + ed86aa2 commit f2fcc52
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ OWNER_ID=
DISCORD_DEV_GUILD=
# Bot prefix
#PREFIX=jo!

# Token needed in order to use Codex api (https://openai.com/join)
CODEX_TOKEN=

# Debugging config
#LOG_LEVEL=debug
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@discordjs/builders": "0.8.2",
"@discordjs/rest": "^0.1.0-canary.0",
"axios": "^0.24.0",
"discord-api-types": "^0.24.0",
"discord-cmd-parser": "^2.2.0",
"discord.js": "^13.2.0",
Expand Down
47 changes: 47 additions & 0 deletions src/commands/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Command from "../types/Command";
import CommandArg from "../types/CommandArg";
import CommandCall from "../types/CommandCall";
import axios, { AxiosRequestConfig } from "axios";

class CodeCommand extends Command {
constructor() {
super({
aliases: ["code", "codex"],
description: "Make codex code something",
args: [new CommandArg({ name: "instructions", description: "Instructions to code", required: true, type: "content", })]
});
}

async run(call: CommandCall) {
if (!process.env.CODEX_TOKEN) return call.reply({ content: "**Error**: Codex token not found" });

let headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.CODEX_TOKEN}`,
};

let data = {
prompt: `\"\"\"\n${call.args.instructions}\"\"\"`,
temperature: 0,
max_tokens: 64,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
};

let options: AxiosRequestConfig = {
url: "https://api.openai.com/v1/engines/davinci-codex/completions",
method: "POST",
headers: headers,
data: JSON.stringify(data),
};

let res, json: any;
res = await axios(options);
json = await res.data;

return call.reply({ content: "```py\n" + json.choices[0].text + "\n```" });
}
}

export default CodeCommand;
22 changes: 17 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ atomic-sleep@^1.0.0:
resolved "https://registry.yarnpkg.com/atomic-sleep/-/atomic-sleep-1.0.0.tgz#eb85b77a601fc932cfe432c5acd364a9e2c9075b"
integrity sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==

axios@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.24.0.tgz#804e6fa1e4b9c5288501dd9dff56a7a0940d20d6"
integrity sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==
dependencies:
follow-redirects "^1.14.4"

callsites@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73"
Expand Down Expand Up @@ -171,11 +178,6 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

discord-api-types@0.24.0, discord-api-types@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.24.0.tgz#9e429b8a1ddb4147134dfb3109093422de7ec549"
integrity sha512-X0uA2a92cRjowUEXpLZIHWl4jiX1NsUpDhcEOpa1/hpO1vkaokgZ8kkPtPih9hHth5UVQ3mHBu/PpB4qjyfJ4A==

discord-api-types@^0.18.1:
version "0.18.1"
resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.18.1.tgz#5d08ed1263236be9c21a22065d0e6b51f790f492"
Expand All @@ -191,6 +193,11 @@ discord-api-types@^0.23.1:
resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.23.1.tgz#832d0ee2b3c8e2eae02947c1dbf38121d6d357d5"
integrity sha512-igWmn+45mzXRWNEPU25I/pr8MwxHb767wAr51oy3VRLRcTlp5ADBbrBR0lq3SA1Rfw3MtM4TQu1xo3kxscfVdQ==

discord-api-types@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.24.0.tgz#9e429b8a1ddb4147134dfb3109093422de7ec549"
integrity sha512-X0uA2a92cRjowUEXpLZIHWl4jiX1NsUpDhcEOpa1/hpO1vkaokgZ8kkPtPih9hHth5UVQ3mHBu/PpB4qjyfJ4A==

discord-cmd-parser@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/discord-cmd-parser/-/discord-cmd-parser-2.2.0.tgz#4a51d849aafae828912c42fd8d2ac1265a95f466"
Expand Down Expand Up @@ -264,6 +271,11 @@ fastify-warning@^0.2.0:
resolved "https://registry.yarnpkg.com/fastify-warning/-/fastify-warning-0.2.0.tgz#e717776026a4493dc9a2befa44db6d17f618008f"
integrity sha512-s1EQguBw/9qtc1p/WTY4eq9WMRIACkj+HTcOIK1in4MV5aFaQC9ZCIt0dJ7pr5bIf4lPpHvAtP2ywpTNgs7hqw==

follow-redirects@^1.14.4:
version "1.14.5"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.5.tgz#f09a5848981d3c772b5392309778523f8d85c381"
integrity sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
Expand Down

0 comments on commit f2fcc52

Please sign in to comment.