eco-discord-bot/src/controller/ChatController.ts
2023-07-14 04:43:22 +12:00

93 lines
2.2 KiB
TypeScript

import axios from "axios";
import MessageI from "../types/MessageI";
var isSyncing = false
export default class ChatController {
public constructor() { }
public async getMessages() {
try {
const { data, status } = await axios.get<MessageI[]>(`${process.env.ECO_API_BASE || "https://eco.kamgames.xyz"}/api/v1/chat`, {
timeout: 5000,
headers: {
"X-API-Key": process.env.ECO_API_KEY
}
});
if (status !== 200) return false
console.log(data)
return true
} catch (error) {
console.log(error)
}
return false
}
public async syncMessageWithDiscord() {
if (isSyncing) return true
isSyncing = true
if (!global.config.chatChannelId) {
isSyncing = false
return false
}
try {
const ch = await global.client.channels.fetch(config.chatChannelId)
const t = global.config.messageTime
const time = (t / 3600 / 24).toString()
const { data, status } = await axios.get<MessageI[]>(
`${process.env.ECO_API_BASE || "https://eco.kamgames.xyz"}/api/v1/chat?startDay=${time}`, {
timeout: 5000,
headers: {
"X-API-Key": process.env.ECO_API_KEY
}
});
if (status !== 200) {
isSyncing = false
return false
}
if (data.length === 0) {
isSyncing = false
return true
}
const messages = data
if (!ch || !ch.isTextBased()) {
isSyncing = false
return false
}
messages.forEach(async m => {
if (["Общий", "General"].indexOf(m.Receiver) > -1 && m.Receiver !== global.config.inGameChat)
global.config.inGameChat = m.Receiver
if (global.config.messageTime !== m.Timestamp) {
if (global.config.messageTime < m.Timestamp)
global.config.messageTime = m.Timestamp
// const me = new EmbedBuilder()
// me.setDescription(m.Text)
// me.setAuthor({ name: m.Sender })
// ch.send({ embeds: [me] })
await ch.send(`**${m.Sender}**: ${m.Text}`)
}
})
isSyncing = false
return true
} catch (error) {
console.log(error)
}
isSyncing = false
return false
}
}