1
- const { Client, GatewayIntentBits, ActivityType } = require ( 'discord.js' ) ;
2
- const { fetchPrice } = require ( './utils' ) ;
1
+ const { Client, GatewayIntentBits, ActivityType, EmbedBuilder } = require ( 'discord.js' ) ;
2
+ const { fetchPairData } = require ( './utils' ) ;
3
3
4
- const client = new Client ( { intents : [ GatewayIntentBits . Guilds ] } ) ;
4
+ const client = new Client ( {
5
+ intents : [
6
+ GatewayIntentBits . Guilds ,
7
+ GatewayIntentBits . GuildMessages ,
8
+ GatewayIntentBits . MessageContent ,
9
+ ]
10
+ } ) ;
5
11
const dotenv = require ( 'dotenv' ) ;
6
12
7
13
dotenv . config ( )
@@ -25,18 +31,112 @@ const doTask = async () => {
25
31
}
26
32
27
33
const updateStatus = async ( ) => {
28
- const price = await fetchPrice ( )
29
- client . user . setActivity ( `$${ price } ` , { type : ActivityType . Watching } )
34
+ const data = await fetchPairData ( )
35
+ client . user . setActivity ( `$${ data . pair . priceUsd } ` , { type : ActivityType . Watching } )
30
36
}
31
37
32
38
const boardcast = async ( ) => {
33
- const price = await fetchPrice ( )
39
+ const data = await fetchPairData ( )
34
40
35
- // send message to all channels from specific channel ids
41
+ // Send message to all channels from specific channel ids
36
42
const channelIds = process . env . TARGET_CHANNEL_IDS . split ( ',' ) ;
37
43
44
+ if ( process . env . MESSAGE_TYPE === 'text' ) {
45
+ _boardcastText ( channelIds , data ) ;
46
+ } else {
47
+ _boardcastEmbed ( channelIds , data ) ;
48
+ }
49
+ }
50
+
51
+ const _boardcastEmbed = async ( channelIds , data ) => {
52
+ for ( const channelId of channelIds ) {
53
+ const channel = await client . channels . fetch ( channelId ) ;
54
+
55
+ // [Optional] Let the chaanel only keep latest message
56
+ const messages = await channel . messages . fetch ( { limit : 100 } ) ;
57
+
58
+ let targetMsg = null ;
59
+ // let ftPriceField = null;
60
+ let m5ChangeField = null ;
61
+
62
+ for ( const message of messages . values ( ) ) {
63
+ const embedMsg = message . embeds [ 0 ] ;
64
+ if ( embedMsg ) {
65
+ m5ChangeField = embedMsg . fields . find ( field => field . name === '5M' ) ;
66
+
67
+ if ( m5ChangeField ) {
68
+ targetMsg = message ;
69
+ break ;
70
+ }
71
+ }
72
+ }
73
+ // If you can't see any message, please check the bot's permission
74
+ // console.log(messages)
75
+
76
+ if ( m5ChangeField &&
77
+ m5ChangeField . value === `${ data . pair . priceChange . m5 } %` ) {
78
+ // same 5m change rate, no need to update
79
+ continue ;
80
+ } else if ( m5ChangeField && m5ChangeField . value !== `$${ data . pair . priceChange . m5 } ` ) {
81
+ // price changed, delete the old message
82
+ await targetMsg . delete ( ) ;
83
+ }
84
+
85
+ // no existed message or price changed, send a new message
86
+ const embedMsg = new EmbedBuilder ( )
87
+ . setTitle ( '丹 Price' )
88
+ . setURL ( `https://dexscreener.com/${ process . env . CHAIN } /${ process . env . PAIR_HASH } ` )
89
+ . setAuthor ( { name : 'mmq88x' , iconURL : 'https://i.imgur.com/8NJckwc.png' , url : 'https://twitter.com/mmq88x' } )
90
+ . setDescription ( `這是現在 PFPAsia 最大流動池的即時價格監控機器人,你可以點擊標頭查看更詳細的資訊。\n` +
91
+ `This is the realtime price monitoring bot of PFPAsia's largest pool now.` +
92
+ `You can click on the title to view more detailed information.` )
93
+ . addFields (
94
+ { name : '<:coin:1207027215414460416> FT Price' , value : `$${ data . pair . priceUsd } ` } ,
95
+ { name : '5M' , value : `${ data . pair . priceChange . m5 } %` , inline : true } ,
96
+ { name : '1H' , value : `${ data . pair . priceChange . h1 } %` , inline : true } ,
97
+ { name : '6H' , value : `${ data . pair . priceChange . h6 } %` , inline : true } ,
98
+ { name : '24H' , value : `${ data . pair . priceChange . h24 } %` , inline : false } ,
99
+ { name : '<:liquidity:1207026179639484506> Liquidity' , value : `$${ data . pair . liquidity . usd } ` , inline : true } ,
100
+ { name : '<:diamond:1207023588000010321> Market cap' , value : `$${ data . pair . fdv } ` , inline : true } ,
101
+ )
102
+ . setThumbnail ( 'https://i.imgur.com/ys8mjvO.png' )
103
+ . setFooter ( { text : 'Update time' , iconURL : 'https://i.imgur.com/ys8mjvO.png' } )
104
+ . setColor ( [ 227 , 23 , 13 ] )
105
+ . setTimestamp ( ) ;
106
+
107
+ await channel . send ( { embeds : [ embedMsg ] } ) ;
108
+
109
+ }
110
+ }
111
+
112
+ const _boardcastText = async ( channelIds , data ) => {
38
113
for ( const channelId of channelIds ) {
39
114
const channel = await client . channels . fetch ( channelId ) ;
115
+
116
+ // [Optional] Let the chaanel only keep latest message
117
+ const messages = await channel . messages . fetch ( { limit : 100 } ) ;
118
+
119
+ // If you can't see any message, please check the bot's permission
120
+ // console.log(messages)
121
+ const price = data . pair . priceUsd ;
122
+ const priceSameMsg = messages . find (
123
+ ( msg ) => msg . content === `丹 DAN Price: $${ price } `
124
+ ) ;
125
+
126
+ if ( priceSameMsg ) {
127
+ continue ;
128
+ }
129
+
130
+ const priceDifferentMsg = messages . find (
131
+ ( msg ) => msg . content . startsWith ( '丹 DAN Price:' ) && msg . content !== `丹 DAN Price: $${ price } `
132
+ ) ;
133
+
134
+ if ( priceDifferentMsg ) {
135
+ await priceDifferentMsg . edit ( `丹 DAN Price: ${ price } ` ) ;
136
+ continue ;
137
+ }
138
+
139
+ // No existed message from the bot, send a new message
40
140
await channel . send ( `丹 DAN Price: $${ price } ` ) ;
41
141
}
42
142
}
0 commit comments