/
recoverContract.js
154 lines (143 loc) · 6.38 KB
/
recoverContract.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
/* This file is not run by default.
It is a tool for recovering contracts from the database.
This tool datamines old messages for contracts and inserts them into the DB.
It should be used when the database is wiped, major changes are made, etc. */
import { readdirSync } from 'node:fs';
import { join } from 'node:path';
import { Client, GatewayIntentBits, Collection, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js';
import config from './config.json' assert { type: 'json' };;
const { token } = config;
import { openContract } from './databaseManager';
import { v4 as uuidv4 } from 'uuid';
import Contract from "./objects/Contract.js";
// Manually enter the guild ID, channel name, and message IDs here
const guildID = '941946604309594142'; // SHADE ID
const channelName = ""
const message_ids = [];
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const eventsPath = join(__dirname, 'events');
const eventFiles = readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
// Set the commands in the client
client.commands = new Collection();
const commandsPath = join(__dirname, 'commands');
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
// Bot is ready, login and start the collectors
client.login(token);
client.on('ready', async () => {
const guild = client.guilds.cache.get(guildID);
var textChannels = guild.channels.cache;
textChannels.sweep(channel => channel.name != channelName);
textChannels = textChannels.values();
for (const channel of textChannels) {
for (const message_id of message_ids) {
var message = await channel.messages.fetch(message_id);
const lines = message.content.split(/\r?\n/);
const status = lines[1].substring(8);
const endOfCrafterLine = lines[2].indexOf(">");
const crafter_id = lines[2].substring(11, endOfCrafterLine);
var miner_id = ""
var resource = ""
var quantity = ""
var cpu = ""
if (lines[3].substring(0, 1) === "M") {
const endOfMinerLine = lines[3].indexOf(">");
var miner_id = lines[3].substring(9, endOfMinerLine);
const beginningOfResourceLine = lines[4].indexOf("=");
resource = lines[4].substring(beginningOfResourceLine + 1);
quantity = lines[5].substring(10);
cpu = lines[6].substring(5);
}
else {
const beginningOfResourceLine = lines[3].indexOf("=");
resource = lines[3].substring(beginningOfResourceLine + 1);
quantity = lines[4].substring(10);
cpu = lines[5].substring(5);
}
const url = message.url;
const channel_id = message.channel_id;
var contractObject = new Contract({
"crafter_id": crafter_id,
"miner_id": miner_id,
"status": status,
"url": url,
"resource": resource,
"quantity": quantity,
"cpu": cpu,
"message_id": message_id,
"channel_id": channel_id,
"accept_id": uuidv4(),
"cancel_id": uuidv4(),
"unaccept_id": uuidv4(),
"vendors_id": uuidv4(),
"complete_id": uuidv4(),
"uncomplete_id": uuidv4(),
"confirm_id": uuidv4(),
})
var contractButtons = null;
if (status === "OPEN") {
contractButtons = new ActionRowBuilder()
.addComponents([
new ButtonBuilder()
.setCustomId(contractObject.accept_id)
.setLabel('Accept')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId(contractObject.cancel_id)
.setLabel('Cancel')
.setStyle(ButtonStyle.Danger)
]);
} else if (status === "IN PROGRESS") {
contractButtons = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId(contractObject.vendors_id)
.setLabel("Vendors")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId(contractObject.complete_id)
.setLabel('Complete')
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId(contractObject.unaccept_id)
.setLabel('Unaccept')
.setStyle(ButtonStyle.Danger),
);
} else if (status === "COMPLETE") {
contractButtons = new ActionRowBuilder()
.addComponents([
new ButtonBuilder()
.setCustomId(contractObject.confirm_id)
.setLabel('Confirm')
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId(contractObject.uncomplete_id)
.setLabel('Uncomplete')
.setStyle(ButtonStyle.Danger),
]);
}
await message.edit({ content: contractObject.toString(), components: [contractButtons] });
openContract(contractObject);
}
}
console.log("Success");
return true;
})