Allow automatic tag invoke deletion (#131)

This commit is contained in:
Nils 2021-01-28 00:30:18 +01:00 committed by GitHub
parent 1c24910a20
commit 460fb2f29b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View file

@ -19,6 +19,7 @@ import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
import { mapToPublicFn } from "../../pluginUtils";
import { renderTagBody } from "./util/renderTagBody";
import { findTagByName } from "./util/findTagByName";
import { StrictValidationError } from "src/validatorUtils";
const defaultOptions: PluginOptions<TagsPluginType> = {
config: {
@ -29,6 +30,7 @@ const defaultOptions: PluginOptions<TagsPluginType> = {
global_tag_cooldown: null,
user_cooldown: null,
global_cooldown: null,
auto_delete_command: false,
categories: {},
@ -71,6 +73,28 @@ export const TagsPlugin = zeppelinGuildPlugin<TagsPluginType>()("tags", {
findTagByName: mapToPublicFn(findTagByName),
},
configPreprocessor(options) {
if (options.config.delete_with_command && options.config.auto_delete_command) {
throw new StrictValidationError([
`Cannot have both (global) delete_with_command and global_delete_invoke enabled`,
]);
}
// Check each category for conflicting options
if (options.config?.categories) {
for (const [name, opts] of Object.entries(options.config.categories)) {
const cat = options.config.categories[name];
if (cat.delete_with_command && cat.auto_delete_command) {
throw new StrictValidationError([
`Cannot have both (category specific) delete_with_command and category_delete_invoke enabled at <categories/${name}>`,
]);
}
}
}
return options;
},
onLoad(pluginData) {
const { state, guild } = pluginData;

View file

@ -16,6 +16,7 @@ export const TagCategory = t.type({
user_category_cooldown: tNullable(t.union([t.string, t.number])), // Per user, per tag category
global_tag_cooldown: tNullable(t.union([t.string, t.number])), // Any user, per tag
global_category_cooldown: tNullable(t.union([t.string, t.number])), // Any user, per category
auto_delete_command: tNullable(t.boolean), // Any tag, per tag category
tags: t.record(t.string, Tag),
@ -31,6 +32,7 @@ export const ConfigSchema = t.type({
global_tag_cooldown: tNullable(t.union([t.string, t.number])), // Any user, per tag
user_cooldown: tNullable(t.union([t.string, t.number])), // Per user
global_cooldown: tNullable(t.union([t.string, t.number])), // Any tag use
auto_delete_command: t.boolean, // Any tag
categories: t.record(t.string, TagCategory),

View file

@ -1,7 +1,7 @@
import { TagsPluginType } from "../types";
import { SavedMessage } from "../../../data/entities/SavedMessage";
import { GuildPluginData } from "knub";
import { convertDelayStringToMS, resolveMember, tStrictMessageContent } from "../../../utils";
import { convertDelayStringToMS, noop, resolveMember, tStrictMessageContent } from "../../../utils";
import { validate } from "../../../validatorUtils";
import { LogType } from "../../../data/LogType";
import { TextChannel } from "eris";
@ -108,4 +108,10 @@ export async function onMessageCreate(pluginData: GuildPluginData<TagsPluginType
await pluginData.state.tags.addResponse(msg.id, responseMsg.id);
});
}
const deleteInvoke = tagResult.category?.category_delete_invoke ?? config.global_delete_invoke;
if (!deleteWithCommand && deleteInvoke) {
// Try deleting the invoking message, ignore errors silently
pluginData.client.deleteMessage(msg.channel_id, msg.id).catch(noop);
}
}