global command self delete react

This commit is contained in:
Cynthia Foxwell 2024-05-16 14:43:51 -06:00
parent 542070b33b
commit 73590257c8
2 changed files with 76 additions and 59 deletions

View File

@ -1,7 +1,7 @@
const Dysnomia = require("@projectdysnomia/dysnomia");
const logger = require("./lib/logger.js");
const fs = require("fs");
const {resolve} = require("path");
const fs = require("node:fs");
const {resolve} = require("node:path");
const sqlite3 = require("sqlite3");
const {instead, before} = require("spitroast");
@ -17,6 +17,7 @@ const bot = new Dysnomia.Client(config.token, {
gateway: {
intents: Object.values(Dysnomia.Constants.Intents),
},
restMode: true,
});
const commands = new Dysnomia.Collection();
@ -56,29 +57,82 @@ for (const file of fs.readdirSync(resolve(__dirname, "modules"))) {
}
bot.on("messageCreate", async (msg) => {
// fix DMs cause of gateway v8 changes
if (
!(msg.channel instanceof Dysnomia.Channel) &&
msg.author.id != bot.user.id &&
!msg.guildID
) {
const newChannel = await bot.getDMChannel(msg.author.id);
if (msg.channel.id == newChannel.id) msg.channel = newChannel;
try {
// fix DMs cause of gateway v8 changes
if (
!(msg.channel instanceof Dysnomia.Channel) &&
msg.author.id != bot.user.id &&
!msg.guildID
) {
const newChannel = await bot.getDMChannel(msg.author.id);
if (msg.channel.id == newChannel.id) msg.channel = newChannel;
}
if (!(msg.channel instanceof Dysnomia.Channel)) return;
await CommandDispatcher(msg);
} catch (err) {
const stack = (err?.stack ?? err.message).split("\n");
const error = stack.shift();
logger.error(
"hf:main",
`Failed to dispatch command: ${error}\n\t${stack.join("\n\t")}`
);
}
if (!(msg.channel instanceof Dysnomia.Channel)) return;
await CommandDispatcher(msg);
});
bot.on("messageUpdate", (msg, oldMsg) => {
const oneDay = Date.now() - 86400000;
if (
msg.timestamp > oneDay &&
!msg.hasRan &&
oldMsg &&
oldMsg.content !== msg.content
) {
CommandDispatcher(msg);
try {
const oneDay = Date.now() - 86400000;
if (
msg.timestamp > oneDay &&
!msg.hasRan &&
oldMsg &&
oldMsg.content !== msg.content
) {
CommandDispatcher(msg);
}
} catch (err) {
const stack = (err?.stack ?? err.message).split("\n");
const error = stack.shift();
logger.error(
"hf:main",
`Failed to dispatch command update: ${error}\n\t${stack.join("\n\t")}`
);
}
});
bot.on("messageReactionAdd", async (msg, reaction, reactor) => {
if (msg.author.id !== bot.user.id) return;
if (reaction.name !== "\u274c") return;
try {
let channel = msg.channel;
if (!(channel instanceof Dysnomia.Channel)) {
const newChannel = hf.bot.getChannel(channel.id);
if (newChannel) {
channel = newChannel;
} else {
channel = await hf.bot.getRESTChannel(channel.id);
}
}
if (!msg.messageReference) {
msg = await channel.getMessage(msg.id);
}
if (!msg.messageReference) return;
const ref = await channel.getMessage(msg.messageReference.messageID);
if (!ref) return;
if (ref.author.id !== reactor.id) return;
await msg.delete("Command sender requested output deletion.");
} catch (err) {
const stack = (err?.stack ?? err.message).split("\n");
const error = stack.shift();
logger.error(
"hf:main",
`Failed to self-delete message: ${error}\n\t${stack.join("\n\t")}`
);
}
});

View File

@ -155,40 +155,3 @@ events.add("messageCreate", "codePreviews", async function (msg) {
}
}
});
// TODO: maybe all command outputs should have this ability
events.add(
"messageReactionAdd",
"codePreviews",
async function (msg, reaction, reactor) {
if (!msg.guildID) return;
if (!(await hasFlag(msg.guildID, "codePreviews"))) return;
if (reaction.name != "\u274c") return;
let channel = msg.channel;
if (!channel.name) {
channel = hf.bot.getChannel(channel.id);
}
if (!msg.messageReference) {
msg = await channel.getMessage(msg.id);
}
if (!msg.messageReference) return;
const ref = await channel.getMessage(msg.messageReference.messageID);
if (!ref) return;
if (
ref.author.id != reactor.id &&
!channel.permissionsOf(reactor.id).has("manageMessages")
)
return;
if (
!REGEX_GITHUB.test(ref.content) &&
!REGEX_GITLAB.test(ref.content) &&
!REGEX_GITEA.test(ref.content)
)
return;
await msg.delete("Author requested code preview deletion");
}
);