質問編集履歴

1

コードを追加

2022/07/15 21:49

投稿

takotthi
takotthi

スコア1

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,141 @@
5
5
 
6
6
  ### 実現したいこと
7
7
  - [ ] ▲▲機能を動作するようにする
8
+
9
+ ###コード
10
+ ```
11
+ const { Client, Intents } = require('discord.js')
12
+
13
+ const Keyv = require('keyv')
14
+
15
+ const http = require('http');
16
+ http.createServer(function (req, res) {
17
+ res.write("online");
18
+ res.end();
19
+ }).listen(8080);
20
+
21
+ const db = new Keyv('sqlite://registerchannel.sqlite', {
22
+ table: 'registerchannel'
23
+ })
24
+
25
+ //intentsのoption
26
+ const option = {
27
+ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES,Intents.FLAGS.GUILD_VOICE_STATES,Intents.FLAGS.GUILD_MEMBERS,Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
28
+ partials: ['MESSAGE', 'CHANNEL', 'REACTION']
29
+
30
+ }
31
+ //client
32
+ const client = new Client(option)
33
+
34
+ client.login(process.env.token)
35
+
36
+ client.on('ready',() => {
37
+ console.log('Successfully login!')
38
+ })
39
+
40
+ setInterval(_ => client.user.setActivity({ name: `日本の災害を監視中|ver:2.1.1|ping:${client.ws.ping}ms` }), 30000);
41
+
42
+ const { WebSocket } = require('ws')
43
+ // WebSocket に接続して JSONをもらおう
44
+ const ws = new WebSocket('wss://api.p2pquake.net/v2/ws')
45
+
46
+ ws.onmessage = async (data) => {
47
+ // JSON化しちゃおう
48
+ const o = JSON.parse(data.data)
49
+
50
+ const scalesjp = {
51
+ "10": "1",
52
+ "20": "2",
53
+ "30": "3",
54
+ "40": "4",
55
+ "45": "5弱",
56
+ "50": "5強",
57
+ "55": "6弱",
58
+ "60": "6強",
59
+ "70": "7"
60
+ };
61
+ const scalesen = {
62
+ "10": "1",
63
+ "20": "2",
64
+ "30": "3",
65
+ "40": "4",
66
+ "45": "5-",
67
+ "50": "5+",
68
+ "55": "6-",
69
+ "60": "6+",
70
+ "70": "7"
71
+ };
72
+ const eewjp = {
73
+ "10": "地震速報",
74
+ "20": "地震速報",
75
+ "30": "地震速報",
76
+ "40": "緊急地震速報",
77
+ "45": "緊急地震速報",
78
+ "50": "緊急地震速報",
79
+ "55": "緊急地震速報",
80
+ "60": "緊急地震速報",
81
+ "70": "緊急地震速報"
82
+ };
83
+ const eewen = {
84
+ "10": "EarthquakeBulletin",
85
+ "20": "EarthquakeBulletin",
86
+ "30": "EarthquakeBulletin",
87
+ "40": "EarthquakeEarlyWarning",
88
+ "45": "EarthquakeEarlyWarning",
89
+ "50": "EarthquakeEarlyWarning",
90
+ "55": "EarthquakeEarlyWarning",
91
+ "60": "EarthquakeEarlyWarning",
92
+ "70": "EarthquakeEarlyWarning"
93
+ };
94
+ if(data.data)o = JSON.parse(data.data)
95
+ ws.on('message', async data => {
96
+ const o = JSON.parse(data.data)
97
+ if(o.code == 551){
98
+ let msindo = o.earthquake.maxScale
99
+ let atime = o.earthquake.time
100
+ let domesticTsunami = o.earthquake.domesticTsunami
101
+ let depth = o.earthquake.hypocenter.depth
102
+ let magunitude = o.earthquake.hypocenter.magnitude
103
+ let hyposentername = o.earthquake.hypocenter.name
104
+ let pointskazu = o.points.length
105
+ let register = await db.get('registerchannel')
106
+ register.forEach(ch => {
107
+ const embed = new MessageEmbed().setColor("YELLOW").setDescription(`${eewjp[`${o.earthquake.maxScale}`]}\n・地震が発生しました。落ち着いて行動してください。倒れやすい家具などからはなれてください。火の始末は、後にしてください。\n・最大震度${scalesjp[`${o.earthquake.maxScale}`]} | 深さ${o.earthquake.hypocenter.depth}km | M${o.earthquake.hypocenter.magnitude}| 震源地:${o.earthquake.hypocenter.name}\n\n${eewen[`${o.earthquake.maxScale}`]}\n・An earthquake has occurred. Please act calmly. Please get away from furniture that is easy to fall down. Please take care of the fire later.\n・MaximumSeismic${scalesen[`${o.earthquake.maxScale}`]} | Depth${o.earthquake.hypocenter.depth}km | M${o.earthquake.hypocenter.magnitude} | Epicenter:${o.earthquake.hypocenter.name} `);
108
+ client.channels.cache.get(ch).send({ embeds: [embed].catch(c => console.log(`${c.id}に送信できませんでした`))})
109
+
110
+ client.on('messageCreate',async (message) => {
111
+ const prefix = "!"
112
+ if (message.author.bot || message.channel.type === "dm") return;
113
+ if (message.content.indexOf(prefix) !== 0) return
114
+ const args = message.content.slice(prefix.length).trim().split(/ +/g);
115
+ const command = args.shift().toLowerCase();
116
+ if(command === "register"){
117
+ message.channel.send(`<#${message.channel.id}>を地震情報通知チャンネルに設定しています...`)
118
+ const channels = await db.get('registerchannel')
119
+ let set
120
+ let save
121
+ if(channels){
122
+ let roles = channels.join(',');
123
+ set =`${roles},${message.channel.id}`
124
+ }
125
+ else set = `${message.channel.id}`
126
+ save = set.split(',');
127
+ await db.set('registerchannel',save)
128
+ await message.channel.send('正常に完了しました。\n:warning: 一度設定してしまったチャンネルは登録を解除することができません。')
129
+ }
130
+
131
+ if(command === "send"){
132
+ let register = await db.get('registerchannel')
133
+ register.forEach(ch => {
134
+ client.channels.cache.get(ch).send('一斉送信テスト!')
135
+ })
136
+ }
137
+ })
138
+ })
139
+ }
140
+ })
141
+ }
142
+ ```
8
143
 
9
144
  ### 発生している問題・エラーメッセージ
10
145