3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

Finish preliminary rework, ready to test

This commit is contained in:
Dark 2021-06-02 04:07:50 +02:00
parent 57893e7f76
commit d0a1beb809
No known key found for this signature in database
GPG key ID: 2CD6ACB6B0A87B8A
177 changed files with 854 additions and 707 deletions

View file

@ -2,6 +2,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes";
import { slowmodeCmd } from "../types";
import humanizeDuration from "humanize-duration";
import { TextChannel } from "discord.js";
export const SlowmodeGetCmd = slowmodeCmd({
trigger: "slowmode",
@ -29,9 +30,9 @@ export const SlowmodeGetCmd = slowmodeCmd({
if (currentSlowmode) {
const humanized = humanizeDuration(channel.rateLimitPerUser * 1000);
const slowmodeType = isNative ? "native" : "bot-maintained";
msg.channel.createMessage(`The current slowmode of <#${channel.id}> is **${humanized}** (${slowmodeType})`);
msg.channel.send(`The current slowmode of <#${channel.id}> is **${humanized}** (${slowmodeType})`);
} else {
msg.channel.createMessage("Channel is not on slowmode");
msg.channel.send("Channel is not on slowmode");
}
},
});

View file

@ -3,6 +3,7 @@ import { slowmodeCmd } from "../types";
import { createChunkedMessage } from "knub/dist/helpers";
import { errorMessage } from "../../../utils";
import humanizeDuration from "humanize-duration";
import { GuildChannel, TextChannel } from "discord.js";
export const SlowmodeListCmd = slowmodeCmd({
trigger: ["slowmode list", "slowmode l", "slowmodes"],
@ -12,7 +13,7 @@ export const SlowmodeListCmd = slowmodeCmd({
const channels = pluginData.guild.channels;
const slowmodes: Array<{ channel: GuildChannel; seconds: number; native: boolean }> = [];
for (const channel of channels.values()) {
for (const channel of channels.cache.values()) {
if (!(channel instanceof TextChannel)) continue;
// Bot slowmode
@ -40,7 +41,7 @@ export const SlowmodeListCmd = slowmodeCmd({
createChunkedMessage(msg.channel, lines.join("\n"));
} else {
msg.channel.createMessage(errorMessage("No active slowmodes!"));
msg.channel.send(errorMessage("No active slowmodes!"));
}
},
});

View file

@ -9,6 +9,7 @@ import { actualDisableSlowmodeCmd } from "../util/actualDisableSlowmodeCmd";
import { getMissingPermissions } from "../../../utils/getMissingPermissions";
import { missingPermissionError } from "../../../utils/missingPermissionError";
import { BOT_SLOWMODE_PERMISSIONS, NATIVE_SLOWMODE_PERMISSIONS } from "../requiredPermissions";
import { Permissions, TextChannel } from "discord.js";
const MAX_NATIVE_SLOWMODE = 6 * HOURS; // 6 hours
const MAX_BOT_SLOWMODE = DAYS * 365 * 100; // 100 years
@ -84,10 +85,13 @@ export const SlowmodeSetCmd = slowmodeCmd({
}
// Verify permissions
const channelPermissions = channel.permissionsOf(pluginData.client.user!.id);
const channelPermissions = channel.permissionsFor(pluginData.client.user!.id);
if (mode === "native") {
const missingPermissions = getMissingPermissions(channelPermissions, NATIVE_SLOWMODE_PERMISSIONS);
const missingPermissions = getMissingPermissions(
channelPermissions ? channelPermissions : new Permissions(),
NATIVE_SLOWMODE_PERMISSIONS,
);
if (missingPermissions) {
sendErrorMessage(
pluginData,
@ -99,7 +103,10 @@ export const SlowmodeSetCmd = slowmodeCmd({
}
if (mode === "bot") {
const missingPermissions = getMissingPermissions(channelPermissions, BOT_SLOWMODE_PERMISSIONS);
const missingPermissions = getMissingPermissions(
channelPermissions ? channelPermissions : new Permissions(),
BOT_SLOWMODE_PERMISSIONS,
);
if (missingPermissions) {
sendErrorMessage(
pluginData,

View file

@ -1,6 +1,8 @@
const p = Constants.Permissions;
import { Permissions } from "discord.js";
export const NATIVE_SLOWMODE_PERMISSIONS = p.readMessages | p.manageChannels;
export const BOT_SLOWMODE_PERMISSIONS = p.readMessages | p.manageRoles | p.manageMessages;
export const BOT_SLOWMODE_CLEAR_PERMISSIONS = p.readMessages | p.manageRoles;
export const BOT_SLOWMODE_DISABLE_PERMISSIONS = p.readMessages | p.manageRoles;
const p = Permissions.FLAGS;
export const NATIVE_SLOWMODE_PERMISSIONS = p.VIEW_CHANNEL | p.MANAGE_CHANNELS;
export const BOT_SLOWMODE_PERMISSIONS = p.VIEW_CHANNEL | p.MANAGE_ROLES | p.MANAGE_MESSAGES;
export const BOT_SLOWMODE_CLEAR_PERMISSIONS = p.VIEW_CHANNEL | p.MANAGE_ROLES;
export const BOT_SLOWMODE_DISABLE_PERMISSIONS = p.VIEW_CHANNEL | p.MANAGE_ROLES;

View file

@ -4,13 +4,14 @@ import { noop } from "../../../utils";
import { getMissingChannelPermissions } from "../../../utils/getMissingChannelPermissions";
import { BOT_SLOWMODE_DISABLE_PERMISSIONS } from "../requiredPermissions";
import { missingPermissionError } from "../../../utils/missingPermissionError";
import { Message, TextChannel } from "discord.js";
export async function actualDisableSlowmodeCmd(msg: Message, args, pluginData) {
const botSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(args.channel.id);
const hasNativeSlowmode = args.channel.rateLimitPerUser;
if (!botSlowmode && hasNativeSlowmode === 0) {
sendErrorMessage(pluginData, msg.channel, "Channel is not on slowmode!");
sendErrorMessage(pluginData, msg.channel as TextChannel, "Channel is not on slowmode!");
return;
}
@ -19,13 +20,13 @@ export async function actualDisableSlowmodeCmd(msg: Message, args, pluginData) {
if (missingPermissions) {
sendErrorMessage(
pluginData,
msg.channel,
msg.channel as TextChannel,
`Unable to disable slowmode. ${missingPermissionError(missingPermissions)}`,
);
return;
}
const initMsg = await msg.channel.createMessage("Disabling slowmode...");
const initMsg = await msg.channel.send("Disabling slowmode...");
// Disable bot-maintained slowmode
let failedUsers: string[] = [];
@ -42,11 +43,11 @@ export async function actualDisableSlowmodeCmd(msg: Message, args, pluginData) {
if (failedUsers.length) {
sendSuccessMessage(
pluginData,
msg.channel,
msg.channel as TextChannel,
`Slowmode disabled! Failed to clear slowmode from the following users:\n\n<@!${failedUsers.join(">\n<@!")}>`,
);
} else {
sendSuccessMessage(pluginData, msg.channel, "Slowmode disabled!");
sendSuccessMessage(pluginData, msg.channel as TextChannel, "Slowmode disabled!");
initMsg.delete().catch(noop);
}
}

View file

@ -4,6 +4,7 @@ import { GuildPluginData } from "knub";
import { isDiscordRESTError, stripObjectToScalars, UnknownUser } from "../../../utils";
import { LogType } from "../../../data/LogType";
import { logger } from "../../../logger";
import { GuildChannel, TextChannel, Permissions } from "discord.js";
export async function applyBotSlowmodeToUserId(
pluginData: GuildPluginData<SlowmodePluginType>,
@ -12,13 +13,17 @@ export async function applyBotSlowmodeToUserId(
) {
// Deny sendMessage permission from the user. If there are existing permission overwrites, take those into account.
const existingOverride = channel.permissionOverwrites.get(userId);
const newDeniedPermissions = (existingOverride ? existingOverride.deny : 0n) | Constants.Permissions.sendMessages;
const newAllowedPermissions = (existingOverride ? existingOverride.allow : 0n) & ~Constants.Permissions.sendMessages;
const newDeniedPermissions =
(existingOverride ? existingOverride.deny.bitfield : 0n) | Permissions.FLAGS.SEND_MESSAGES;
const newAllowedPermissions =
(existingOverride ? existingOverride.allow.bitfield : 0n) & ~Permissions.FLAGS.SEND_MESSAGES;
try {
await channel.editPermission(userId, newAllowedPermissions, newDeniedPermissions, "member");
await channel.overwritePermissions([
{ id: userId, allow: newAllowedPermissions, deny: newDeniedPermissions, type: "member" },
]);
} catch (e) {
const user = pluginData.client.user!.get(userId) || new UnknownUser({ id: userId });
const user = pluginData.client.users.fetch(userId) || new UnknownUser({ id: userId });
if (isDiscordRESTError(e) && e.code === 50013) {
logger.warn(

View file

@ -1,3 +1,4 @@
import { GuildChannel, TextChannel } from "discord.js";
import { GuildPluginData } from "knub";
import { SlowmodePluginType } from "../types";
@ -12,7 +13,7 @@ export async function clearBotSlowmodeFromUserId(
// Previously we diffed the overrides so we could clear the "send messages" override without touching other
// overrides. Unfortunately, it seems that was a bit buggy - we didn't always receive the event for the changed
// overrides and then we also couldn't diff against them. For consistency's sake, we just delete the override now.
await channel.deletePermission(userId);
await channel.permissionOverwrites.get(userId)?.delete();
} catch (e) {
if (!force) {
throw e;

View file

@ -5,6 +5,7 @@ import { logger } from "../../../logger";
import { stripObjectToScalars, UnknownUser } from "../../../utils";
import { clearBotSlowmodeFromUserId } from "./clearBotSlowmodeFromUserId";
import { GuildChannel, TextChannel } from "discord.js";
export async function clearExpiredSlowmodes(pluginData: GuildPluginData<SlowmodePluginType>) {
const expiredSlowmodeUsers = await pluginData.state.slowmodes.getExpiredSlowmodeUsers();
@ -20,7 +21,7 @@ export async function clearExpiredSlowmodes(pluginData: GuildPluginData<Slowmode
} catch (e) {
logger.error(e);
const realUser = pluginData.client.user!.get(user.user_id) || new UnknownUser({ id: user.user_id });
const realUser = pluginData.client.users!.fetch(user.user_id) || new UnknownUser({ id: user.user_id });
pluginData.state.logs.log(LogType.BOT_ALERT, {
body: `Failed to clear slowmode permissions from {userMention(user)} in {channelMention(channel)}`,
user: stripObjectToScalars(realUser),

View file

@ -1,3 +1,4 @@
import { GuildChannel, TextChannel } from "discord.js";
import { GuildPluginData } from "knub";
import { SlowmodePluginType } from "../types";
import { clearBotSlowmodeFromUserId } from "./clearBotSlowmodeFromUserId";

View file

@ -11,6 +11,7 @@ import { LogsPlugin } from "../../Logs/LogsPlugin";
import { LogType } from "../../../data/LogType";
import { missingPermissionError } from "../../../utils/missingPermissionError";
import { messageLock } from "../../../utils/lockNameHelpers";
import { TextChannel } from "discord.js";
export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePluginType>, msg: SavedMessage) {
if (msg.is_bot) return;
@ -49,7 +50,7 @@ export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePlugin
// Delete any extra messages sent after a slowmode was already applied
const userHasSlowmode = await pluginData.state.slowmodes.userHasSlowmode(channel.id, msg.user_id);
if (userHasSlowmode) {
const message = await channel.getMessage(msg.id);
const message = await channel.messages.fetch(msg.id);
if (message) {
message.delete();
return thisMsgLock.interrupt();