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(" ");
for (let i = 0; i < serverIds.length; 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 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 strings = require('./data/strings.json');
const dbfn = require('./modules/dbfn.js');
const { GuildInfo } = require('./modules/CustomClasses.js');
const isDev = process.env.DEBUG;
let statusChannel;
@ -71,7 +72,7 @@ client.on('interactionCreate', async interaction => {
case 'deleteping':
if (interaction.message.deletable) {
await interaction.message.delete().catch(err => {
console.error(err);
// console.error(err);
});
}
break;
@ -128,12 +129,22 @@ client.on('guildCreate', async guild => {
const serverCount = client.guilds.cache.size;
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})`);
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 => {
const serverCount = client.guilds.cache.size;
client.user.setActivity({ name: `${serverCount} trees grow.`, type: ActivityType.Watching });
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) {

View File

@ -11,6 +11,7 @@ module.exports = {
GuildInfo: class {
constructor() {
this.guildId = "";
this.ownerId = ""; // TODO Is ownerId fully implemented?
this.treeName = "";
this.treeHeight = 0;
this.treeMessageId = "";
@ -28,8 +29,9 @@ module.exports = {
this.compareMessageId = "";
}
setId(id) {
this.guildId = id;
setIds(guildId, ownerId) {
this.guildId = guildId;
this.ownerId = ownerId === undefined ? this.ownerId : ownerId
return this;
}
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)}`,
];
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:
break;
}

View File

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

View File

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

View File

@ -4,6 +4,7 @@
const dotenv = require('dotenv');
dotenv.config();
const token = process.env.TOKEN;
const dbfn = require('./dbfn.js');
// Discord.JS
const { Client, GatewayIntentBits, Partials } = require('discord.js');
@ -25,10 +26,13 @@ const fn = require('../modules/functions.js');
client.once('ready', async () => {
// watchRequestRates();
await fn.collectionBuilders.guildInfos(client);
const guilds = client.guilds.cache;
guilds.each(g => {
console.log(g.name + "," + g.id + "," + g.ownerId);
});
console.log("I'm in " + guilds.size + " guilds with " + client.guildInfos.size + " guildInfos");
// guilds.each(g => {
// console.log(g.name + "," + g.id + "," + g.ownerId);
// });
await setAllGuildOwners();
process.exit();
});
@ -54,7 +58,41 @@ async function watchRequestRates() {
}).catch(error => {
console.error(error);
});
await fn.sleep(500).then(async () =>{
await fn.sleep(500).then(async () => {
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');
// Create a new GuildInfo object
let guildInfo = new GuildInfo()
.setId(interaction.guildId)
.setIds(interaction.guildId, interaction.guild.ownerId)
// Set the reminder configuration
.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id, true);
// 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));
} else {
let guildInfo = new GuildInfo()
.setId(interaction.guildId);
.setIds(interaction.guildId, interaction.guild.ownerId);
guildInfo.setRoles(waterRoleId, fruitRoleId);
await dbfn.setGuildInfo(guildInfo.queryBuilder("setRoles"));
await fn.collectionBuilders.guildInfos(interaction.client);