forked from mdshamoon/github-issues-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
82 lines (72 loc) · 2.29 KB
/
index.ts
File metadata and controls
82 lines (72 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import DiscordJS, { CommandInteraction } from "discord.js";
import dotenv from "dotenv";
import { Octokit } from "@octokit/rest";
import { getModal } from "./utils";
import report from "./report";
import express from "express";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.get("/", (req, res) => {
res.send("Github issues bot!");
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
const client = new DiscordJS.Client({
intents: ["Guilds", "GuildMessages"],
});
client.on("ready", () => {
console.log("issue bot ready");
const guildId = process.env.GUILD_ID || "";
const guild = client.guilds.cache.get(guildId);
guild?.commands.create(report.data);
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand()) {
await interaction
.showModal(
getModal(
interaction.options.data[0].value as string, // ["issueTitle"]
interaction.options.data[1].value as string // ["issueDescription"]
)
)
.catch((err) => {
console.log(err);
interaction.ephemeral = true;
interaction.reply(
`DiscordJS API responded with an error.\nError: ${err}}\nAction: While showing modal`
);
});
} else if (interaction.isModalSubmit()) {
const { fields } = interaction;
const issueTitle = fields.getTextInputValue("issueTitle");
const issueDescription = fields.getTextInputValue("issueDescription");
const octokit = new Octokit({
auth: process.env.GITHUB_ACCESS_TOKEN,
baseUrl: "https://api.github.com",
});
await octokit.rest.issues
.create({
owner: process.env.GITHUB_USERNAME || "",
repo: process.env.GITHUB_REPOSITORY || "",
title: issueTitle,
body: issueDescription,
})
.then((res) => {
interaction.ephemeral = true;
interaction.reply(`Issue created: ${res.data.html_url}`);
})
.catch((err) => {
console.log(err);
interaction.ephemeral = true;
interaction.reply(
`Octokit API responded with an error\nError: ${err}}\nAction: While creating issue in github`
);
});
}
});
client.login(process.env.BOT_TOKEN).catch((err) => {
console.log(err);
});