Make broadcasts persistent, fix Postgres database upgrades, fix image slash commands with text not working in DMs

This commit is contained in:
Essem 2022-10-25 12:37:55 -05:00
parent 089b750838
commit 5d679dbb7a
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
8 changed files with 37 additions and 9 deletions

View file

@ -1,4 +1,5 @@
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
import database from "../../utils/database.js";
import { endBroadcast, startBroadcast } from "../../utils/misc.js"; import { endBroadcast, startBroadcast } from "../../utils/misc.js";
class BroadcastCommand extends Command { class BroadcastCommand extends Command {
@ -10,6 +11,7 @@ class BroadcastCommand extends Command {
} }
const message = this.options.message ?? this.args.join(" "); const message = this.options.message ?? this.args.join(" ");
if (message?.trim()) { if (message?.trim()) {
await database.setBroadcast(message);
startBroadcast(this.client, message); startBroadcast(this.client, message);
if (process.env.PM2_USAGE) { if (process.env.PM2_USAGE) {
process.send({ process.send({
@ -22,6 +24,7 @@ class BroadcastCommand extends Command {
} }
return "Started broadcast."; return "Started broadcast.";
} else { } else {
await database.setBroadcast(null);
endBroadcast(this.client); endBroadcast(this.client);
if (process.env.PM2_USAGE) { if (process.env.PM2_USAGE) {
process.send({ process.send({

View file

@ -195,6 +195,7 @@
"Trials Rising", "Trials Rising",
"Pizza Tower", "Pizza Tower",
"ANTONBLAST", "ANTONBLAST",
"[object Object]",
"The clock is ticking." "The clock is ticking."
] ]
} }

View file

@ -27,7 +27,7 @@ export default async (client) => {
logger.log("info", "The help docs have been generated."); logger.log("info", "The help docs have been generated.");
} }
checkBroadcast(client); await checkBroadcast(client);
activityChanger(client); activityChanger(client);
ready = true; ready = true;

View file

@ -20,6 +20,8 @@ export async function getTag() {}
export async function setTag() {} export async function setTag() {}
export async function removeTag() {} export async function removeTag() {}
export async function editTag() {} export async function editTag() {}
export async function setBroadcast() {}
export async function getBroadcast() {}
export async function setPrefix() {} export async function setPrefix() {}
export async function addGuild(guild) { export async function addGuild(guild) {
return { return {

View file

@ -7,7 +7,8 @@ const sql = Postgres(process.env.DB);
const psqlUpdates = [ const psqlUpdates = [
"", // reserved "", // reserved
"CREATE TABLE IF NOT EXISTS settings ( id smallint PRIMARY KEY, version integer NOT NULL, CHECK(id = 1) );\nALTER TABLE guilds ADD COLUMN accessed timestamp;", "CREATE TABLE IF NOT EXISTS settings ( id smallint PRIMARY KEY, version integer NOT NULL, CHECK(id = 1) );\nALTER TABLE guilds ADD COLUMN accessed timestamp;",
"ALTER TABLE guilds DROP COLUMN accessed" "ALTER TABLE guilds DROP COLUMN accessed",
"ALTER TABLE settings ADD COLUMN broadcast text"
]; ];
export async function setup() { export async function setup() {
@ -56,7 +57,7 @@ export async function upgrade(logger) {
while (version < (psqlUpdates.length - 1)) { while (version < (psqlUpdates.length - 1)) {
version++; version++;
logger.warn(`Running version ${version} update script (${psqlUpdates[version]})...`); logger.warn(`Running version ${version} update script (${psqlUpdates[version]})...`);
await db`${psqlUpdates[version]}`; await db.unsafe(psqlUpdates[version]);
} }
}); });
const ver = psqlUpdates.length - 1; const ver = psqlUpdates.length - 1;
@ -104,6 +105,15 @@ export async function removeTag(name, guild) {
await sql`DELETE FROM tags WHERE guild_id = ${guild.id} AND name = ${name}`; await sql`DELETE FROM tags WHERE guild_id = ${guild.id} AND name = ${name}`;
} }
export async function setBroadcast(msg) {
await sql`UPDATE settings SET broadcast = ${msg} WHERE id = 1`;
}
export async function getBroadcast() {
const result = await sql`SELECT broadcast FROM settings WHERE id = 1`;
return result[0].broadcast;
}
export async function disableCommand(guild, command) { export async function disableCommand(guild, command) {
const guildDB = await this.getGuild(guild); const guildDB = await this.getGuild(guild);
await sql`UPDATE guilds SET disabled_commands = ${(guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command]).filter((v) => !!v)} WHERE guild_id = ${guild}`; await sql`UPDATE guilds SET disabled_commands = ${(guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command]).filter((v) => !!v)} WHERE guild_id = ${guild}`;

View file

@ -7,7 +7,8 @@ const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
const sqliteUpdates = [ const sqliteUpdates = [
"", // reserved "", // reserved
"ALTER TABLE guilds ADD COLUMN accessed int", "ALTER TABLE guilds ADD COLUMN accessed int",
"ALTER TABLE guilds DROP COLUMN accessed" "ALTER TABLE guilds DROP COLUMN accessed",
"CREATE TABLE IF NOT EXISTS settings ( id smallint PRIMARY KEY, broadcast VARCHAR, CHECK(id = 1) );\nINSERT INTO settings (id) VALUES (1) ON CONFLICT (id) DO NOTHING;"
]; ];
export async function setup() { export async function setup() {
@ -151,6 +152,15 @@ export async function editTag(name, content, guild) {
connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, guild.id, name); connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, guild.id, name);
} }
export async function setBroadcast(msg) {
connection.prepare("UPDATE settings SET broadcast = ? WHERE id = 1").run(msg);
}
export async function getBroadcast() {
const result = connection.prepare("SELECT broadcast FROM settings WHERE id = 1").all();
return result[0].broadcast;
}
export async function setPrefix(prefix, guild) { export async function setPrefix(prefix, guild) {
connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id); connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
collections.prefixCache.set(guild.id, prefix); collections.prefixCache.set(guild.id, prefix);

View file

@ -2,6 +2,7 @@ import util from "util";
import fs from "fs"; import fs from "fs";
import pm2 from "pm2"; import pm2 from "pm2";
import { config } from "dotenv"; import { config } from "dotenv";
import db from "./database.js";
// playing messages // playing messages
const { messages } = JSON.parse(fs.readFileSync(new URL("../config/messages.json", import.meta.url))); const { messages } = JSON.parse(fs.readFileSync(new URL("../config/messages.json", import.meta.url)));
@ -62,10 +63,11 @@ export async function activityChanger(bot) {
setTimeout(() => activityChanger(bot), 900000); setTimeout(() => activityChanger(bot), 900000);
} }
export function checkBroadcast(bot) { export async function checkBroadcast(bot) {
/*if () { const message = await db.getBroadcast();
if (message) {
startBroadcast(bot, message); startBroadcast(bot, message);
}*/ }
} }
export function startBroadcast(bot, message) { export function startBroadcast(bot, message) {
@ -116,7 +118,7 @@ export function getServers(bot) {
export function cleanMessage(message, content) { export function cleanMessage(message, content) {
let cleanContent = content && content.replace(/<a?(:\w+:)[0-9]+>/g, "$1") || ""; let cleanContent = content && content.replace(/<a?(:\w+:)[0-9]+>/g, "$1") || "";
const author = message.author ?? message.member; const author = message.author ?? message.member ?? message.user;
let authorName = author.username; let authorName = author.username;
if (message.member?.nick) { if (message.member?.nick) {
authorName = message.member.nick; authorName = message.member.nick;

View file

@ -6,6 +6,6 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL ); CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL );
CREATE TABLE tags ( guild_id VARCHAR(30) NOT NULL, name text NOT NULL, content text NOT NULL, author VARCHAR(30) NOT NULL, UNIQUE(guild_id, name) ); CREATE TABLE tags ( guild_id VARCHAR(30) NOT NULL, name text NOT NULL, content text NOT NULL, author VARCHAR(30) NOT NULL, UNIQUE(guild_id, name) );
CREATE TABLE settings ( id smallint PRIMARY KEY, version integer NOT NULL, CHECK(id = 1) ); CREATE TABLE settings ( id smallint PRIMARY KEY, version integer NOT NULL, broadcast VARCHAR, CHECK(id = 1) );
INSERT INTO settings (id, version) VALUES (1, 2) ON CONFLICT (id) DO NOTHING; INSERT INTO settings (id, version) VALUES (1, 2) ON CONFLICT (id) DO NOTHING;
EOSQL EOSQL