-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
100 lines (94 loc) · 3 KB
/
index.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Client, Intents } from "discord.js";
import fetch from "node-fetch";
import { config } from "dotenv";
config();
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
client.once("ready", () => {
console.log("Ready!");
});
const atodemiru = "808566487983587338";
const test = "800186124613451806";
client.on("messageReactionAdd", async (reaction, user) => {
console.log("test");
if (reaction.message.partial) {
try {
await reaction.message.fetch();
} catch (error) {
console.error("Something went wrong when fetching the message: ", error);
}
}
if (reaction.message.channel.id === atodemiru && reaction.count === 1) {
reaction.message.pin();
} else if (reaction.message.channel.id === test && reaction.count === 1) {
reaction.message.pin();
}
// console.log(reaction.count);
// console.log(`${user.username} reacted with "${reaction.emoji.name}".`);
});
client.on("messageReactionRemove", async (reaction, user) => {
if (reaction.message.partial) {
try {
await reaction.message.fetch();
} catch (error) {
console.error("Something went wrong when fetching the message: ", error);
}
}
console.log(
`${user.username} removed their "${reaction.emoji.name}" reaction.`
);
});
client.on("messageCreate", async (message) => {
if (message.author.bot) {
return;
}
if (message.content.startsWith("https://github.com/")) {
const url = message.content;
const newurl = url.replace(
/^(.*\/\/github.com\/.+\/.+\/)blob(\/.+)$/i,
"$1raw$2"
);
const paragraph = url.split("#");
if (paragraph[1] != null) {
console.log("Where you want to display is existing.");
(async () => {
try {
const response = await fetch(newurl);
let sendtext = "";
const body = await response.text();
const lines = body.split("\n");
// 拡張子を取得
const fileType = paragraph[0]
.split("/")
.slice(-1)[0]
.split(".")
.slice(-1)[0];
const LineNumber = paragraph[1].split("-");
// 行始めと行終わりを取得
if (LineNumber.length === 2) {
const begin = Number(LineNumber[0].substring(1)) - 1;
const end = Number(LineNumber[1].substring(1));
console.log({ begin, end });
sendtext = lines.slice(begin, end).join("\n");
} else if (LineNumber.length === 1) {
const begin = Number(LineNumber[0].substring(1)) - 1;
sendtext = lines.slice(begin, begin + 1).join("\n");
} else {
console.log("error");
}
// テンプレートリテラル
message.channel.send(` \`\`\`${fileType}\n${sendtext} \`\`\``);
} catch (error) {
console.log(error);
}
})();
}
}
});
client.login(process.env.DISCORD_TOKEN);