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