Add owner IDs to db, clean up some logging

This commit is contained in:
Skylar Grant 2023-06-16 20:01:29 -04:00
parent 9e5a89eabb
commit a1386dd688
8 changed files with 96 additions and 25 deletions

View File

@ -12,7 +12,9 @@ module.exports = {
const serverIds = commandData.args.split(" "); const serverIds = commandData.args.split(" ");
for (let i = 0; i < serverIds.length; i++) { for (let i = 0; i < serverIds.length; i++) {
const id = serverIds[i]; const id = serverIds[i];
const guild = await message.client.guilds.fetch(id); const guild = await message.client.guilds.fetch(id).catch(e => {
if (!(e.status === 404)) throw e;
});
await guild.leave(); await guild.leave();
await message.channel.send("Left Guild: " + id); await message.channel.send("Left Guild: " + id);
} }

13
main.js
View File

@ -25,6 +25,7 @@ const client = new Client({
const fn = require('./modules/functions.js'); const fn = require('./modules/functions.js');
const strings = require('./data/strings.json'); const strings = require('./data/strings.json');
const dbfn = require('./modules/dbfn.js'); const dbfn = require('./modules/dbfn.js');
const { GuildInfo } = require('./modules/CustomClasses.js');
const isDev = process.env.DEBUG; const isDev = process.env.DEBUG;
let statusChannel; let statusChannel;
@ -71,7 +72,7 @@ client.on('interactionCreate', async interaction => {
case 'deleteping': case 'deleteping':
if (interaction.message.deletable) { if (interaction.message.deletable) {
await interaction.message.delete().catch(err => { await interaction.message.delete().catch(err => {
console.error(err); // console.error(err);
}); });
} }
break; break;
@ -128,12 +129,22 @@ client.on('guildCreate', async guild => {
const serverCount = client.guilds.cache.size; const serverCount = client.guilds.cache.size;
client.user.setActivity({ name: `${serverCount} trees grow.`, type: ActivityType.Watching }); client.user.setActivity({ name: `${serverCount} trees grow.`, type: ActivityType.Watching });
await statusChannel.send(`I've been added to a new guild: ${guild.name} (${guild.id})`); await statusChannel.send(`I've been added to a new guild: ${guild.name} (${guild.id})`);
const guildInfo = new GuildInfo()
.setIds(guild.id, guild.ownerId);
const setBasicQuery = guildInfo.queryBuilder("setBasic");
await dbfn.setGuildInfo(setBasicQuery).catch(e => console.error(e));
}); });
client.on('guildDelete', async guild => { client.on('guildDelete', async guild => {
const serverCount = client.guilds.cache.size; const serverCount = client.guilds.cache.size;
client.user.setActivity({ name: `${serverCount} trees grow.`, type: ActivityType.Watching }); client.user.setActivity({ name: `${serverCount} trees grow.`, type: ActivityType.Watching });
await statusChannel.send(`I've been removed from a guild: ${guild.name} (${guild.id})`); await statusChannel.send(`I've been removed from a guild: ${guild.name} (${guild.id})`);
if (client.guildInfos.has(guild.id)) {
let guildInfo = client.guildInfos.get(guild.id);
guildInfo.setReminders(undefined, undefined, undefined, undefined, false);
const setRemindersQuery = guildInfo.queryBuilder("setReminders");
await dbfn.setGuildInfo(setRemindersQuery);
}
}); });
async function checkRateLimits(hi) { async function checkRateLimits(hi) {

View File

@ -11,6 +11,7 @@ module.exports = {
GuildInfo: class { GuildInfo: class {
constructor() { constructor() {
this.guildId = ""; this.guildId = "";
this.ownerId = ""; // TODO Is ownerId fully implemented?
this.treeName = ""; this.treeName = "";
this.treeHeight = 0; this.treeHeight = 0;
this.treeMessageId = ""; this.treeMessageId = "";
@ -28,8 +29,9 @@ module.exports = {
this.compareMessageId = ""; this.compareMessageId = "";
} }
setId(id) { setIds(guildId, ownerId) {
this.guildId = id; this.guildId = guildId;
this.ownerId = ownerId === undefined ? this.ownerId : ownerId
return this; return this;
} }
setName(name) { setName(name) {
@ -198,6 +200,23 @@ module.exports = {
`) ON DUPLICATE KEY UPDATE compare_channel_id = ${db.escape(this.compareChannelId)}, compare_message_id = ${db.escape(this.compareMessageId)}`, `) ON DUPLICATE KEY UPDATE compare_channel_id = ${db.escape(this.compareChannelId)}, compare_message_id = ${db.escape(this.compareMessageId)}`,
]; ];
return queryParts.join(''); return queryParts.join('');
// TODO This is hacked in and needs to be implemented throughout the code
case "setIds":
queryParts = [
`UPDATE guild_info SET `,
`owner_id=${db.escape(this.ownerId)} `,
`WHERE guild_id=${db.escape(this.guildId)}`
];
return queryParts.join('');
case "setBasic":
queryParts = [
`INSERT INTO guild_info (`,
`guild_id, owner_id`,
`) VALUES (`,
`${db.escape(this.guildId)}, ${db.escape(this.ownerId)}`,
`) ON DUPLICATE KEY UPDATE owner_id=${db.escape(this.ownerId)}`
];
return queryParts.join('');
default: default:
break; break;
} }

View File

@ -63,7 +63,7 @@ module.exports = {
} }
row = res[0]; row = res[0];
const guildInfo = new GuildInfo() const guildInfo = new GuildInfo()
.setId(row.guild_id) .setIds(row.guild_id, row.owner_id)
.setName(row.tree_name) .setName(row.tree_name)
.setHeight(row.tree_height) .setHeight(row.tree_height)
.setTreeMessage(row.tree_message_id, row.tree_channel_id) .setTreeMessage(row.tree_message_id, row.tree_channel_id)
@ -107,7 +107,7 @@ module.exports = {
for (let i = 0; i < res.length; i++) { for (let i = 0; i < res.length; i++) {
let row = res[i]; let row = res[i];
guildInfos.push(new GuildInfo() guildInfos.push(new GuildInfo()
.setId(row.guild_id) .setIds(row.guild_id, row.owner_id)
.setName(row.tree_name) .setName(row.tree_name)
.setHeight(row.tree_height) .setHeight(row.tree_height)
.setTreeMessage(row.tree_message_id, row.tree_channel_id) .setTreeMessage(row.tree_message_id, row.tree_channel_id)
@ -117,6 +117,7 @@ module.exports = {
.setCompareMessage(row.compare_channel_id, row.compare_message_id) .setCompareMessage(row.compare_channel_id, row.compare_message_id)
); );
} }
console.log(res.length + " // " + guildInfos.length);
db.end(); db.end();
resolve(guildInfos); resolve(guildInfos);

View File

@ -463,7 +463,7 @@ const functions = {
}); });
}).catch(err => { }).catch(err => {
reject(strings.status.missingTreeChannel); reject(strings.status.missingTreeChannel);
console.error(err); // console.error(err);
return; return;
}); });
} else { } else {
@ -717,7 +717,7 @@ const functions = {
doDbUpdate = true; doDbUpdate = true;
} }
} else { } else {
guildInfo = new GuildInfo().setId(message.guildId) guildInfo = new GuildInfo().setIds(message.guildId, message.guild.ownerId)
.setLeaderboardMessage(message.id, message.channel.id); .setLeaderboardMessage(message.id, message.channel.id);
doDbUpdate = true; doDbUpdate = true;
} }
@ -773,7 +773,7 @@ const functions = {
doDbUpdate = true; doDbUpdate = true;
} }
} else { } else {
guildInfo = new GuildInfo().setId(message.guildId) guildInfo = new GuildInfo().setIds(message.guildId, message.guild.ownerId)
.setTreeInfo(isTree.treeName, isTree.treeHeight, message.channel.id, message.id); .setTreeInfo(isTree.treeName, isTree.treeHeight, message.channel.id, message.id);
doDbUpdate = true; doDbUpdate = true;
} }

View File

@ -4,6 +4,7 @@
const dotenv = require('dotenv'); const dotenv = require('dotenv');
dotenv.config(); dotenv.config();
const token = process.env.TOKEN; const token = process.env.TOKEN;
const dbfn = require('./dbfn.js');
// Discord.JS // Discord.JS
const { Client, GatewayIntentBits, Partials } = require('discord.js'); const { Client, GatewayIntentBits, Partials } = require('discord.js');
@ -25,10 +26,13 @@ const fn = require('../modules/functions.js');
client.once('ready', async () => { client.once('ready', async () => {
// watchRequestRates(); // watchRequestRates();
await fn.collectionBuilders.guildInfos(client);
const guilds = client.guilds.cache; const guilds = client.guilds.cache;
guilds.each(g => { console.log("I'm in " + guilds.size + " guilds with " + client.guildInfos.size + " guildInfos");
console.log(g.name + "," + g.id + "," + g.ownerId); // guilds.each(g => {
}); // console.log(g.name + "," + g.id + "," + g.ownerId);
// });
await setAllGuildOwners();
process.exit(); process.exit();
}); });
@ -54,7 +58,41 @@ async function watchRequestRates() {
}).catch(error => { }).catch(error => {
console.error(error); console.error(error);
}); });
await fn.sleep(500).then(async () =>{ await fn.sleep(500).then(async () => {
await watchRequestRates(); await watchRequestRates();
}); });
} }
async function setAllGuildOwners() {
try {
let guildInfosArray = new Array();
let guildUpdateCount = 0;
let guildMissingCount = 0;
client.guildInfos.forEach((guildInfo) => {
guildInfosArray.push(guildInfo);
});
// console.log(guildInfosArray);
for (let i = 0; i < guildInfosArray.length; i++) {
const guildInfo = guildInfosArray[i];
let eFlag = 0;
const guild = await client.guilds.fetch(guildInfo.guildId).catch(e => {
eFlag = 1;
if (e.status === 404) {
console.log("Missing guild: " + guildInfo.guildId);
guildMissingCount++;
} else {
throw e;
}
});
if (eFlag === 1) continue;
guildInfo.setIds(guildInfo.guildId, guild.ownerId);
const query = guildInfo.queryBuilder("setIds");
console.log(query);
await dbfn.setGuildInfo(query);
guildUpdateCount++;
}
console.log(`Updated ${guildUpdateCount} guilds with ${guildMissingCount} missing guilds.`);
} catch(err) {
console.error(err);
}
}

View File

@ -110,7 +110,7 @@ module.exports = {
const reminderChannel = interaction.options.getChannel('pingchannel'); const reminderChannel = interaction.options.getChannel('pingchannel');
// Create a new GuildInfo object // Create a new GuildInfo object
let guildInfo = new GuildInfo() let guildInfo = new GuildInfo()
.setId(interaction.guildId) .setIds(interaction.guildId, interaction.guild.ownerId)
// Set the reminder configuration // Set the reminder configuration
.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id, true); .setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id, true);
// Update the guildInfos Collection // Update the guildInfos Collection

View File

@ -50,7 +50,7 @@ module.exports = {
await interaction.editReply(fn.builders.embeds.treeRoleMenu(guildInfo)).catch(e => console.error(e)); await interaction.editReply(fn.builders.embeds.treeRoleMenu(guildInfo)).catch(e => console.error(e));
} else { } else {
let guildInfo = new GuildInfo() let guildInfo = new GuildInfo()
.setId(interaction.guildId); .setIds(interaction.guildId, interaction.guild.ownerId);
guildInfo.setRoles(waterRoleId, fruitRoleId); guildInfo.setRoles(waterRoleId, fruitRoleId);
await dbfn.setGuildInfo(guildInfo.queryBuilder("setRoles")); await dbfn.setGuildInfo(guildInfo.queryBuilder("setRoles"));
await fn.collectionBuilders.guildInfos(interaction.client); await fn.collectionBuilders.guildInfos(interaction.client);