add chat syncing with discord
This commit is contained in:
parent
4edf560785
commit
9d2fb05c34
@ -39,8 +39,12 @@ export default class ChatController {
|
||||
}
|
||||
|
||||
try {
|
||||
const ch = await global.client.channels.fetch(config.chatChannelId)
|
||||
|
||||
const t = global.config.messageTime + 0.001
|
||||
const time = (t / 3600 / 24).toFixed(100)
|
||||
const { data, status } = await axios.get<MessageI[]>(
|
||||
`${process.env.ECO_API_BASE || "https://eco.kamgames.xyz"}/api/v1/chat?startDay=${global.config.messages.length > 0 ? (global.config.messages[global.config.messages.length - 1].Timestamp / 3600 - 60).toFixed(10) : 0}`, {
|
||||
`${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
|
||||
@ -59,28 +63,20 @@ export default class ChatController {
|
||||
|
||||
const messages = data
|
||||
|
||||
const ch = await global.client.channels.fetch(config.chatChannelId)
|
||||
if (!ch || !ch.isTextBased()) {
|
||||
isSyncing = false
|
||||
return false
|
||||
}
|
||||
|
||||
messages.forEach(async m => {
|
||||
if (global.config.messages.filter(v =>
|
||||
m.Timestamp === v.Timestamp &&
|
||||
m.Text === v.Text &&
|
||||
m.Sender === v.Sender &&
|
||||
m.Receiver === v.Receiver
|
||||
).length > 0) return
|
||||
else {
|
||||
// const me = new EmbedBuilder()
|
||||
// me.setDescription(m.Text)
|
||||
// me.setAuthor({ name: m.Sender })
|
||||
// ch.send({ embeds: [me] })
|
||||
await ch.send(`**${m.Sender}**: ${m.Text}`)
|
||||
|
||||
global.config.messages.push(m)
|
||||
}
|
||||
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] })
|
||||
console.log(`${m.Sender}: ${m.Text}`)
|
||||
await ch.send(`**${m.Sender}**: ${m.Text}`)
|
||||
})
|
||||
|
||||
isSyncing = false
|
||||
|
@ -10,6 +10,7 @@ interface ConfigManagerI {
|
||||
authCodes: Date[];
|
||||
};
|
||||
messages?: MessageI[];
|
||||
messageTime: number;
|
||||
serverInfo?: ServerInfoI;
|
||||
chatChannelId?: string;
|
||||
}
|
||||
@ -23,9 +24,8 @@ export default class ConfigManager {
|
||||
users: [],
|
||||
authCodes: [],
|
||||
}
|
||||
public messages: MessageI[] = []
|
||||
// public serverInfo?: ServerInfoI = undefined
|
||||
public chatChannelId: string = ""
|
||||
public messageTime: number = 0
|
||||
|
||||
constructor() {
|
||||
this.load()
|
||||
@ -34,17 +34,14 @@ export default class ConfigManager {
|
||||
save() {
|
||||
const r: ConfigManagerI = {
|
||||
userManager: this.userManager,
|
||||
messages: this.messages,
|
||||
// serverInfo: this.serverInfo,
|
||||
chatChannelId: this.chatChannelId,
|
||||
messageTime: this.messageTime,
|
||||
}
|
||||
fs.writeFileSync(this.configPath, JSON.stringify(r))
|
||||
}
|
||||
load() {
|
||||
const r = {
|
||||
userManager: this.userManager,
|
||||
// serverInfo: this.serverInfo,
|
||||
messages: this.messages,
|
||||
chatChannelId: this.chatChannelId,
|
||||
}
|
||||
if (!fs.existsSync(this.configPath)) {
|
||||
@ -54,10 +51,8 @@ export default class ConfigManager {
|
||||
const t: ConfigManagerI = JSON.parse(String(fs.readFileSync(this.configPath)))
|
||||
if (!!t?.userManager)
|
||||
this.userManager = { ...this.userManager, ...t.userManager }
|
||||
if (!!t?.messages)
|
||||
this.messages = t.messages
|
||||
// if (!!t?.serverInfo)
|
||||
// this.serverInfo = t.serverInfo
|
||||
if (!!t?.messageTime)
|
||||
this.messageTime = t.messageTime
|
||||
if (!!t?.chatChannelId)
|
||||
this.chatChannelId = t.chatChannelId
|
||||
} catch (error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user