-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
135 lines (117 loc) · 4.73 KB
/
main.js
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 { Client, GatewayIntentBits, Collection, Partials,Events} = require('discord.js');
const config = require('./config.json');
const path = require("path");
const fs = require("fs");
const cron = require('node-cron');
global.client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessageReactions
],
partials: [Partials.Channel],
});
module.exports.client=client;
/*関数読み込み*/
const system = require('./functions/logsystem.js');
const help = require('./functions/help.js');
const recordVC = require('./functions/recordVC.js');
const guildData = require('./functions/guildData.js');
const db = require('./functions/db.js');
/*スラッシュコマンド登録*/
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
client.commands = new Collection();
module.exports = client.commands;
/*Readyイベント*/
client.once("ready", async () => {
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
for (let i = 0; i < command.length; i++) {
client.commands.set(command[i].data.name, command[i]);
}
}
await system.log("Ready!");
});
/*スラッシュコマンド呼び出し*/
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) {
return;
}
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
let guild,channel;
if(!interaction.guildId) {
guild = {name:"ダイレクトメッセージ",id:"---"};
channel = {name:"---",id:"---"};
}
else{
guild = client.guilds.cache.get(interaction.guildId) ?? await client.guilds.fetch(interaction.guildId);
channel = client.channels.cache.get(interaction.channelId) ?? await client.channels.fetch(interaction.channelId);
}
await system.log(`コマンド名:${command.data.name}\`\`\`\nギルド :${guild.name}\n(ID:${guild.id})\n\nチャンネル:${channel.name}\n(ID:${channel.id})\n\nユーザ :${interaction.user.username}#${interaction.user.discriminator}\n(ID:${interaction.user.id})\`\`\``, "SlashCommand");
try {
await command.execute(interaction);
}
catch(error) {
await system.error(`スラッシュコマンド実行時エラー : ${command.data.name}\n\`\`\`\nギルド :${guild.name}\n(ID:${guild.id})\n\nチャンネル:${channel.name}\n(ID:${channel.id})\n\nユーザ :${interaction.user.username}#${interaction.user.discriminator}\n(ID:${interaction.user.id})\`\`\``, error);
try {
await interaction.reply({content: 'おっと、想定外の事態が起きちゃった。[Issue](https://github.com/starkoka/Tasclear/issues)に連絡してくれ。', ephemeral: true});
}
catch {
try{
await interaction.editReply({
content: 'おっと、想定外の事態が起きちゃった。[Issue](https://github.com/starkoka/Tasclear/issues)に連絡してくれ。',
ephemeral: true
});
}
catch{} //edit先が消えてる可能性を考えてtryに入れる
}
}
});
//StringSelectMenu受け取り
client.on(Events.InteractionCreate, async interaction => {
if(interaction.isStringSelectMenu()) {
if (interaction.customId === "adminHelp"){
await help.adminHelpDisplay(interaction);
}
else if (interaction.customId === "help"){
await help.helpDisplay(interaction);
}
}
});
client.on('voiceStateUpdate', (oldState, newState) => {
recordVC.vcStateUpdate(oldState, newState);
})
/*ギルド参加時*/
client.on('guildCreate', async interaction => {
await guildData.joinGuild(interaction);
})
/*ギルド退出時*/
client.on('guildDelete', async interaction => {
await guildData.deleteGuild(interaction);
})
/*ユーザー参加時*/
client.on('guildMemberAdd', async interaction => {
await guildData.joinMember(interaction);
})
/*ユーザー退出時*/
client.on('guildMemberRemove', async interaction => {
await guildData.removeMember(interaction);
})
/*ステータス更新*/
cron.schedule('* * * * *', async () => {
const joined = await db.find("main","user",{isJoined:true});
client.user.setPresence({
activities: [{
name: `${joined.length}人がタスク`
}],
});
});
if(require.main === module) {
client.login(config.token);
}