1
+ const { Client, GatewayIntentBits, ActivityType } = require ( 'discord.js' ) ;
2
+ const { fetchPrice } = require ( './utils' ) ;
3
+
4
+ const client = new Client ( { intents : [ GatewayIntentBits . Guilds ] } ) ;
5
+ const dotenv = require ( 'dotenv' ) ;
6
+
7
+ dotenv . config ( )
8
+
9
+ TASK_QUEUE = [ ]
10
+
11
+ const registTask = async ( ) => {
12
+ if ( process . env . UPDATE_STATUS == 'on' ) {
13
+ TASK_QUEUE . push ( updateStatus ) ;
14
+ }
15
+
16
+ if ( process . env . BOARDCAST == 'on' ) {
17
+ TASK_QUEUE . push ( boardcast ) ;
18
+ }
19
+ }
20
+
21
+ const doTask = async ( ) => {
22
+ await TASK_QUEUE . forEach ( async ( task ) => {
23
+ await task ( ) ;
24
+ } ) ;
25
+ }
26
+
27
+ const updateStatus = async ( ) => {
28
+ const price = await fetchPrice ( )
29
+ client . user . setActivity ( `$${ price } ` , { type : ActivityType . Watching } )
30
+ }
31
+
32
+ const boardcast = async ( ) => {
33
+ const price = await fetchPrice ( )
34
+
35
+ // send message to all channels from specific channel ids
36
+ const channelIds = process . env . TARGET_CHANNEL_IDS . split ( ',' ) ;
37
+
38
+ for ( const channelId of channelIds ) {
39
+ const channel = await client . channels . fetch ( channelId ) ;
40
+ await channel . send ( `丹 DAN Price: $${ price } ` ) ;
41
+ }
42
+ }
43
+
44
+ client . on ( 'ready' , ( ) => {
45
+ client . user . setActivity ( '丹 DAN Price' , { type : ActivityType . Watching } )
46
+ console . log ( `Logged in as ${ client . user . tag } !` ) ;
47
+ registTask ( ) ;
48
+
49
+ setInterval ( async ( ) => {
50
+ doTask ( ) ;
51
+ } , process . env . UPDATE_FREQUENCY ) ;
52
+ } ) ;
53
+
54
+ client . login ( process . env . DISCORD_TOKEN ) ;
0 commit comments