-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.ts
More file actions
74 lines (61 loc) · 2 KB
/
index.ts
File metadata and controls
74 lines (61 loc) · 2 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
import DiscordJS from "discord.js";
import dotenv from "dotenv";
import { Octokit } from "@octokit/rest";
import { getModal } from "./utils";
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);
let commands;
if (guild) {
commands = guild.commands;
} else {
commands = client.application?.commands;
}
commands?.create({
name: "Open github issue",
type: 3,
});
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isMessageContextMenuCommand()) {
const { commandName, targetMessage } = interaction;
if (commandName === "Open github issue") {
const modal = getModal(targetMessage.content);
interaction.showModal(modal);
}
} else if (interaction.isModalSubmit()) {
const { fields } = interaction;
const issueTitle = fields.getField("issueTitle").value;
const issueDescription = fields.getField("issueDescription").value;
const octokit = new Octokit({
auth: process.env.GITHUB_ACCESS_TOKEN,
baseUrl: "https://api.github.com",
});
octokit.rest.issues
.create({
owner: process.env.GITHUB_USERNAME || "",
repo: process.env.GITHUB_REPOSITORY || "",
title: issueTitle,
body: issueDescription,
})
.then((res) => {
interaction.reply(`Issue created: ${res.data.html_url}`);
});
}
});
client.login(process.env.BOT_TOKEN);