Add permission checks and alerts to AutoDelete
This commit is contained in:
parent
e6461d541d
commit
eb221859d8
2 changed files with 33 additions and 5 deletions
|
@ -7,6 +7,7 @@ import { onMessageCreate } from "./util/onMessageCreate";
|
|||
import { onMessageDelete } from "./util/onMessageDelete";
|
||||
import { onMessageDeleteBulk } from "./util/onMessageDeleteBulk";
|
||||
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
|
||||
import { LogsPlugin } from "../Logs/LogsPlugin";
|
||||
|
||||
const defaultOptions: PluginOptions<AutoDeletePluginType> = {
|
||||
config: {
|
||||
|
@ -23,7 +24,7 @@ export const AutoDeletePlugin = zeppelinGuildPlugin<AutoDeletePluginType>()("aut
|
|||
configurationGuide: "Maximum deletion delay is currently 5 minutes",
|
||||
},
|
||||
|
||||
dependencies: [TimeAndDatePlugin],
|
||||
dependencies: [TimeAndDatePlugin, LogsPlugin],
|
||||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
|
||||
|
|
|
@ -2,24 +2,51 @@ import { GuildPluginData } from "knub";
|
|||
import { AutoDeletePluginType } from "../types";
|
||||
import moment from "moment-timezone";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { stripObjectToScalars, resolveUser } from "../../../utils";
|
||||
import { resolveUser, stripObjectToScalars, verboseChannelMention } from "../../../utils";
|
||||
import { logger } from "../../../logger";
|
||||
import { scheduleNextDeletion } from "./scheduleNextDeletion";
|
||||
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
||||
import { hasDiscordPermissions } from "../../../utils/hasDiscordPermissions";
|
||||
import { Constants } from "eris";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
|
||||
export async function deleteNextItem(pluginData: GuildPluginData<AutoDeletePluginType>) {
|
||||
const [itemToDelete] = pluginData.state.deletionQueue.splice(0, 1);
|
||||
if (!itemToDelete) return;
|
||||
|
||||
scheduleNextDeletion(pluginData);
|
||||
|
||||
const channel = pluginData.guild.channels.get(itemToDelete.message.channel_id);
|
||||
if (!channel) {
|
||||
// Channel was deleted, ignore
|
||||
return;
|
||||
}
|
||||
|
||||
const logs = pluginData.getPlugin(LogsPlugin);
|
||||
const perms = channel.permissionsOf(pluginData.client.user.id);
|
||||
|
||||
if (!hasDiscordPermissions(perms, Constants.Permissions.readMessages | Constants.Permissions.readMessageHistory)) {
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Missing permissions to read messages or message history in auto-delete channel ${verboseChannelMention(
|
||||
channel,
|
||||
)}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasDiscordPermissions(perms, Constants.Permissions.manageMessages)) {
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Missing permissions to delete messages in auto-delete channel ${verboseChannelMention(channel)}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
|
||||
|
||||
pluginData.state.guildLogs.ignoreLog(LogType.MESSAGE_DELETE, itemToDelete.message.id);
|
||||
pluginData.client.deleteMessage(itemToDelete.message.channel_id, itemToDelete.message.id).catch(logger.warn);
|
||||
|
||||
scheduleNextDeletion(pluginData);
|
||||
|
||||
const user = await resolveUser(pluginData.client, itemToDelete.message.user_id);
|
||||
const channel = pluginData.guild.channels.get(itemToDelete.message.channel_id);
|
||||
const messageDate = timeAndDate
|
||||
.inGuildTz(moment.utc(itemToDelete.message.data.timestamp, "x"))
|
||||
.format(timeAndDate.getDateFormat("pretty_datetime"));
|
||||
|
|
Loading…
Add table
Reference in a new issue