-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscordSys.js
More file actions
135 lines (125 loc) · 3.8 KB
/
discordSys.js
File metadata and controls
135 lines (125 loc) · 3.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const Discord = require("discord.js");
const fs = require("fs");
const api = require("./api");
class DiscordSys {
running = false;
/**@param {Express.Application} server */
constructor(server, serverEvents) {
this.server = server;
this.serverEvents = serverEvents;
/**@type {Array} */
this.commands = [];
/**@type {Discord.Client} */
this.client = new Discord.Client({
intents: [Discord.GatewayIntentBits.MessageContent],
partials: [
Discord.Partials.Message,
Discord.Partials.Channel,
Discord.Partials.Reaction,
],
});
this.client.once("ready", async () => {
await this.onBotReady();
});
this.client.on("interactionCreate", async (interaction) => {
await this.onCommand(interaction);
});
}
async tick() {
const elements = await api.GetElements();
this.client.user.setActivity({
name: `${elements.length}要素数のMEネットワーク`,
type: Discord.ActivityType.Playing,
});
}
start() {
console.log("Starting Discord Bot");
this.client.login();
}
end() {
this.running = false;
this.client.destroy();
}
async onBotReady() {
setInterval(() => {
this.tick();
}, 5000);
await this.registCommands();
console.log("Discord bot is running on " + this.client.user?.username);
this.running = true;
}
async registCommands() {
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"))
.filter((file) => !file.startsWith("_"));
let command = null;
const data = [];
for (const file in commandFiles) {
command = require(`./commands/${commandFiles[file]}`);
this.commands[command.data.name] = command;
}
for (const commandName in this.commands) {
data.push(this.commands[commandName].data);
}
//@ts-ignore
await this.client.application.commands.set(data);
}
/**
* @param {Discord.Interaction} interaction
*/
async onCommand(interaction) {
const commandvalues = Object.values(this.commands);
if (interaction.isCommand()) {
const command = this.commands[interaction.commandName];
try {
//実行
await command.execute(interaction);
} catch (error) {
console.error(error);
if (error.message.match("2000")) {
ErrorRep(interaction, "2000文字を超えました。");
} else {
ErrorRep(
interaction,
"コマンド実行中にエラーが発生しました。\r\n:x:エラー:実行したコマンドと共に管理者に伝えてください。"
);
}
}
} else if (interaction.isButton()) {
const command = commandvalues.find(
(c) => c.id === interaction.customId.substring(0, 4)
);
await command.Button(interaction, interaction.customId);
} else if (interaction.isAnySelectMenu()) {
const command = commandvalues.find(
(c) => c.id === interaction.customId.substring(0, 4)
);
await command.SelMenu(interaction, interaction.customId);
} else if (interaction.isModalSubmit()) {
const command = commandvalues.find(
(c) => c.id === interaction.customId.substring(0, 4)
);
await command.Modal(interaction, interaction.customId);
} else if (interaction.isUserContextMenuCommand()) {
//@ts-ignore
const command = this.commands[interaction.id];
await command.UserContent(interaction);
}
}
}
/**
* @param {Discord.ChatInputCommandInteraction} interaction
* @param {string} message
*/
function ErrorRep(interaction, message) {
if (interaction.deferred)
interaction.editReply({
content: ":x:エラー:" + message,
});
else
interaction.reply({
content: ":x:エラー:" + message,
});
}
module.exports = DiscordSys;