mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-12 21:05:02 +00:00
Turn on strict TS compilation. Fix up and tweak types accordingly.
This commit is contained in:
parent
690955a399
commit
629002b8d9
172 changed files with 720 additions and 534 deletions
|
@ -4,8 +4,9 @@ import { RECENT_ACTION_EXPIRY_TIME, RecentActionType } from "../constants";
|
|||
import { getEmojiInString, getRoleMentions, getUrlsInString, getUserMentions } from "../../../utils";
|
||||
|
||||
export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodPluginType>, context: AutomodContext) {
|
||||
const globalIdentifier = context.message.user_id;
|
||||
const perChannelIdentifier = `${context.message.channel_id}-${context.message.user_id}`;
|
||||
const message = context.message!;
|
||||
const globalIdentifier = message.user_id;
|
||||
const perChannelIdentifier = `${message.channel_id}-${message.user_id}`;
|
||||
const expiresAt = Date.now() + RECENT_ACTION_EXPIRY_TIME;
|
||||
|
||||
pluginData.state.recentActions.push({
|
||||
|
@ -23,8 +24,7 @@ export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodP
|
|||
});
|
||||
|
||||
const mentionCount =
|
||||
getUserMentions(context.message.data.content || "").length +
|
||||
getRoleMentions(context.message.data.content || "").length;
|
||||
getUserMentions(message.data.content || "").length + getRoleMentions(message.data.content || "").length;
|
||||
if (mentionCount) {
|
||||
pluginData.state.recentActions.push({
|
||||
context,
|
||||
|
@ -41,7 +41,7 @@ export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodP
|
|||
});
|
||||
}
|
||||
|
||||
const linkCount = getUrlsInString(context.message.data.content || "").length;
|
||||
const linkCount = getUrlsInString(message.data.content || "").length;
|
||||
if (linkCount) {
|
||||
pluginData.state.recentActions.push({
|
||||
context,
|
||||
|
@ -58,7 +58,7 @@ export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodP
|
|||
});
|
||||
}
|
||||
|
||||
const attachmentCount = context.message.data.attachments && context.message.data.attachments.length;
|
||||
const attachmentCount = message.data.attachments && message.data.attachments.length;
|
||||
if (attachmentCount) {
|
||||
pluginData.state.recentActions.push({
|
||||
context,
|
||||
|
@ -75,7 +75,7 @@ export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodP
|
|||
});
|
||||
}
|
||||
|
||||
const emojiCount = getEmojiInString(context.message.data.content || "").length;
|
||||
const emojiCount = getEmojiInString(message.data.content || "").length;
|
||||
if (emojiCount) {
|
||||
pluginData.state.recentActions.push({
|
||||
context,
|
||||
|
@ -93,7 +93,7 @@ export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodP
|
|||
}
|
||||
|
||||
// + 1 is for the first line of the message (which doesn't have a line break)
|
||||
const lineCount = context.message.data.content ? (context.message.data.content.match(/\n/g) || []).length + 1 : 0;
|
||||
const lineCount = message.data.content ? (message.data.content.match(/\n/g) || []).length + 1 : 0;
|
||||
if (lineCount) {
|
||||
pluginData.state.recentActions.push({
|
||||
context,
|
||||
|
@ -110,7 +110,7 @@ export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodP
|
|||
});
|
||||
}
|
||||
|
||||
const characterCount = [...(context.message.data.content || "")].length;
|
||||
const characterCount = [...(message.data.content || "")].length;
|
||||
if (characterCount) {
|
||||
pluginData.state.recentActions.push({
|
||||
context,
|
||||
|
@ -127,7 +127,7 @@ export function addRecentActionsFromMessage(pluginData: GuildPluginData<AutomodP
|
|||
});
|
||||
}
|
||||
|
||||
const stickerCount = (context.message.data.stickers || []).length;
|
||||
const stickerCount = (message.data.stickers || []).length;
|
||||
if (stickerCount) {
|
||||
pluginData.state.recentActions.push({
|
||||
context,
|
||||
|
|
|
@ -4,8 +4,9 @@ import { RECENT_ACTION_EXPIRY_TIME, RecentActionType } from "../constants";
|
|||
import { getEmojiInString, getRoleMentions, getUrlsInString, getUserMentions } from "../../../utils";
|
||||
|
||||
export function clearRecentActionsForMessage(pluginData: GuildPluginData<AutomodPluginType>, context: AutomodContext) {
|
||||
const globalIdentifier = context.message.user_id;
|
||||
const perChannelIdentifier = `${context.message.channel_id}-${context.message.user_id}`;
|
||||
const message = context.message!;
|
||||
const globalIdentifier = message.user_id;
|
||||
const perChannelIdentifier = `${message.channel_id}-${message.user_id}`;
|
||||
|
||||
pluginData.state.recentActions = pluginData.state.recentActions.filter(act => {
|
||||
return act.identifier !== globalIdentifier && act.identifier !== perChannelIdentifier;
|
||||
|
|
|
@ -7,6 +7,7 @@ import { findRecentSpam } from "./findRecentSpam";
|
|||
import { getMatchingMessageRecentActions } from "./getMatchingMessageRecentActions";
|
||||
import * as t from "io-ts";
|
||||
import { getMessageSpamIdentifier } from "./getSpamIdentifier";
|
||||
import { SavedMessage } from "../../../data/entities/SavedMessage";
|
||||
|
||||
const MessageSpamTriggerConfig = t.type({
|
||||
amount: t.number,
|
||||
|
@ -29,22 +30,25 @@ export function createMessageSpamTrigger(spamType: RecentActionType, prettyName:
|
|||
return;
|
||||
}
|
||||
|
||||
const spamIdentifier = getMessageSpamIdentifier(context.message, triggerConfig.per_channel);
|
||||
const spamIdentifier = getMessageSpamIdentifier(context.message, Boolean(triggerConfig.per_channel));
|
||||
|
||||
const recentSpam = findRecentSpam(pluginData, spamType, spamIdentifier);
|
||||
if (recentSpam) {
|
||||
await pluginData.state.archives.addSavedMessagesToArchive(
|
||||
recentSpam.archiveId,
|
||||
[context.message],
|
||||
pluginData.guild,
|
||||
);
|
||||
if (recentSpam.archiveId) {
|
||||
await pluginData.state.archives.addSavedMessagesToArchive(
|
||||
recentSpam.archiveId,
|
||||
[context.message],
|
||||
pluginData.guild,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
silentClean: true,
|
||||
extra: { archiveId: "" }, // FIXME: Fix up automod trigger match() typings so extra is not required when doing a silentClean
|
||||
};
|
||||
}
|
||||
|
||||
const within = convertDelayStringToMS(triggerConfig.within);
|
||||
const within = convertDelayStringToMS(triggerConfig.within) ?? 0;
|
||||
const matchedSpam = getMatchingMessageRecentActions(
|
||||
pluginData,
|
||||
context.message,
|
||||
|
@ -58,7 +62,7 @@ export function createMessageSpamTrigger(spamType: RecentActionType, prettyName:
|
|||
const messages = matchedSpam.recentActions
|
||||
.map(action => action.context.message)
|
||||
.filter(Boolean)
|
||||
.sort(sorter("posted_at"));
|
||||
.sort(sorter("posted_at")) as SavedMessage[];
|
||||
|
||||
const archiveId = await pluginData.state.archives.createFromSavedMessages(messages, pluginData.guild);
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ export function getMatchingRecentActions(
|
|||
action.type === type &&
|
||||
(!identifier || action.identifier === identifier) &&
|
||||
action.context.timestamp >= since &&
|
||||
action.context.timestamp <= to &&
|
||||
action.context.timestamp <= to! &&
|
||||
!action.context.actioned
|
||||
);
|
||||
});
|
||||
|
|
|
@ -10,23 +10,25 @@ export function getTextMatchPartialSummary(
|
|||
context: AutomodContext,
|
||||
) {
|
||||
if (type === "message") {
|
||||
const channel = pluginData.guild.channels.get(context.message.channel_id);
|
||||
const channelMention = channel ? verboseChannelMention(channel) : `\`#${context.message.channel_id}\``;
|
||||
const message = context.message!;
|
||||
const channel = pluginData.guild.channels.get(message.channel_id);
|
||||
const channelMention = channel ? verboseChannelMention(channel) : `\`#${message.channel_id}\``;
|
||||
|
||||
return `message in ${channelMention}:\n${messageSummary(context.message)}`;
|
||||
return `message in ${channelMention}:\n${messageSummary(message)}`;
|
||||
} else if (type === "embed") {
|
||||
const channel = pluginData.guild.channels.get(context.message.channel_id);
|
||||
const channelMention = channel ? verboseChannelMention(channel) : `\`#${context.message.channel_id}\``;
|
||||
const message = context.message!;
|
||||
const channel = pluginData.guild.channels.get(message.channel_id);
|
||||
const channelMention = channel ? verboseChannelMention(channel) : `\`#${message.channel_id}\``;
|
||||
|
||||
return `message embed in ${channelMention}:\n${messageSummary(context.message)}`;
|
||||
return `message embed in ${channelMention}:\n${messageSummary(message)}`;
|
||||
} else if (type === "username") {
|
||||
return `username: ${context.user.username}`;
|
||||
return `username: ${context.user!.username}`;
|
||||
} else if (type === "nickname") {
|
||||
return `nickname: ${context.member.nick}`;
|
||||
return `nickname: ${context.member!.nick}`;
|
||||
} else if (type === "visiblename") {
|
||||
const visibleName = context.member?.nick || context.user.username;
|
||||
const visibleName = context.member?.nick || context.user!.username;
|
||||
return `visible name: ${visibleName}`;
|
||||
} else if (type === "customstatus") {
|
||||
return `custom status: ${context.member.game.state}`;
|
||||
return `custom status: ${context.member!.game!.state}`;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,10 +7,10 @@ import { AutomodPluginType } from "../types";
|
|||
export function resolveActionContactMethods(
|
||||
pluginData: GuildPluginData<AutomodPluginType>,
|
||||
actionConfig: {
|
||||
notify?: string;
|
||||
notifyChannel?: string;
|
||||
notify?: string | null;
|
||||
notifyChannel?: string | null;
|
||||
},
|
||||
): UserNotificationMethod[] | null {
|
||||
): UserNotificationMethod[] {
|
||||
if (actionConfig.notify === "dm") {
|
||||
return [{ type: "dm" }];
|
||||
} else if (actionConfig.notify === "channel") {
|
||||
|
@ -28,5 +28,5 @@ export function resolveActionContactMethods(
|
|||
return [];
|
||||
}
|
||||
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
|
|
@ -5,13 +5,14 @@ import { availableActions } from "../actions/availableActions";
|
|||
import { AutomodTriggerMatchResult } from "../helpers";
|
||||
import { CleanAction } from "../actions/clean";
|
||||
import { checkAndUpdateCooldown } from "./checkAndUpdateCooldown";
|
||||
import { TextChannel } from "eris";
|
||||
|
||||
export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>, context: AutomodContext) {
|
||||
const userId = context.user?.id || context.member?.id || context.message?.user_id;
|
||||
const user = context.user || (userId && pluginData.client.users.get(userId));
|
||||
const member = context.member || (userId && pluginData.guild.members.get(userId));
|
||||
const member = context.member || (userId && pluginData.guild.members.get(userId)) || null;
|
||||
const channelId = context.message?.channel_id;
|
||||
const channel = channelId && pluginData.guild.channels.get(channelId);
|
||||
const channel = channelId ? (pluginData.guild.channels.get(channelId) as TextChannel) : null;
|
||||
const categoryId = channel?.parentID;
|
||||
|
||||
const config = pluginData.config.getMatchingConfig({
|
||||
|
@ -29,8 +30,8 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
|
|||
return;
|
||||
}
|
||||
|
||||
let matchResult: AutomodTriggerMatchResult<any>;
|
||||
let contexts: AutomodContext[];
|
||||
let matchResult: AutomodTriggerMatchResult<any> | null | undefined;
|
||||
let contexts: AutomodContext[] = [];
|
||||
|
||||
triggerLoop: for (const triggerItem of rule.triggers) {
|
||||
for (const [triggerName, triggerConfig] of Object.entries(triggerItem)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue