-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
165 lines (155 loc) · 4.59 KB
/
server.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const Discord = require("discord.js");
const axios = require("axios");
require("dotenv").config();
const wsClinet = new (require("websocket").client)();
const moment = require("moment");
const Sequelize = require("sequelize");
const sequelize = new Sequelize({
dialect: "sqlite",
storage: "stonks.sqlite",
});
const Tickers = sequelize.define("tickers", {
symbol: {
type: Sequelize.STRING,
unique: true,
},
lastPrice: {
type: Sequelize.FLOAT,
},
});
const Channels = sequelize.define("channels", {
channel: {
type: Sequelize.STRING,
unique: true,
},
});
const client = new Discord.Client();
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
const rPrice = {};
const lPrice = {};
const directions = {};
const green = "🟩";
const red = "🟥";
const yellow = "🟨";
const PREFIX = "^^";
client.on("ready", () => {
Tickers.sync();
Channels.sync();
console.log(`Logged in as ${client.user.tag}!`);
wsClinet.on("connect", (connection) => {
console.log("WebSocket Client Connected");
Tickers.findAll().then((tickers) => {
tickers.forEach((ticker) => {
rPrice[ticker.symbol] = 0;
lPrice[ticker.symbol] = 0;
directions[ticker.symbol] = yellow;
console.log(`subscribing to ${ticker.symbol}`);
connection.send(
JSON.stringify({ type: "subscribe", symbol: ticker.symbol })
);
});
});
connection.on("message", (e) => {
const { data } = JSON.parse(e.utf8Data);
if (data) {
data
.sort((a, b) => a.t - b.t)
.forEach((dataPoint) => {
rPrice[dataPoint.s] = dataPoint.p;
Tickers.update(
{
lastPrice: dataPoint.p,
},
{
where: {
symbol: dataPoint.s,
},
}
);
});
}
});
client.on("message", async (message) => {
if (
message.content.startsWith(PREFIX) &&
message.member.hasPermission(["ADMINISTRATOR"])
) {
const input = message.content.slice(PREFIX.length).trim().split(" ");
const command = input.shift();
const commandArgs = input.join(" ");
if (command == "add") {
const toAdd = commandArgs.trim();
Tickers.create({
symbol: toAdd,
}).then((ticker) => {
connection.send(
JSON.stringify({ type: "subscribe", symbol: ticker.symbol })
);
message.reply(`Symbol ${ticker.symbol} added.`);
});
} else if (command == "remove") {
const toDelete = commandArgs.trim();
Tickers.destroy({
where: {
symbol: toDelete,
},
}).then((ticker) => {
connection.send(
JSON.stringify({ type: "unsubscribe", symbol: toDelete })
);
message.reply(`Symbol ${toDelete} removed.`);
});
} else if (command == "send") {
Channels.create({
channel: message.channel.id,
}).then(() => {
message.reply("Subscribed to updates");
});
} else if (command == "unsend") {
Channels.destroy({
where: {
channel: message.channel.id,
},
}).then(() => {
message.reply("Unsubscribed to updates");
});
}
}
});
setInterval(() => {
const now = moment();
let toSend = "";
Tickers.findAll({
order: [["symbol", "ASC"]],
}).then((tickers) => {
tickers.forEach((ticker, i) => {
if (rPrice[ticker.symbol] > lPrice[ticker.symbol]) {
directions[ticker.symbol] = green;
} else if (rPrice[ticker.symbol] < lPrice[ticker.symbol]) {
directions[ticker.symbol] = red;
} else {
directions[ticker.symbol] = yellow;
}
toSend += `\`${directions[ticker.symbol]} ${
ticker.symbol
} ${formatter.format(ticker.lastPrice)}\``;
if (i < tickers.length - 1) {
toSend += " ";
}
lPrice[ticker.symbol] = rPrice[ticker.symbol];
});
Channels.findAll().then((channels) => {
channels.forEach((channel) => {
client.channels.cache.get(channel.channel).send(toSend);
});
});
});
}, 30 * 1000);
});
wsClinet.connect(`wss://ws.finnhub.io?token=${process.env.FINNHUB_TOKEN}`);
});
client.on("rateLimit", (e) => console.log(e));
client.login(process.env.DISCORD_TOKEN);