Skip to content

Commit

Permalink
🗑️ Remove boost requirement (#158)
Browse files Browse the repository at this point in the history
* Remove boost requirement for auto-archive duration

* Fix bug with deleting wrong message
  • Loading branch information
MarcusOtter authored May 17, 2022
1 parent 4ad9ac6 commit ae065e5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
7 changes: 5 additions & 2 deletions src/handlers/messageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
replaceMessageVariables,
getThreadAuthor,
} from "../helpers/messageHelpers";
import { getRequiredPermissions, getSafeDefaultAutoArchiveDuration } from "../helpers/permissionHelpers";
import { getRequiredPermissions } from "../helpers/permissionHelpers";
import { wait } from "../helpers/promiseHelpers";

export async function handleMessageCreate(message: Message): Promise<void> {
// Server outage
Expand Down Expand Up @@ -118,7 +119,7 @@ async function autoCreateThread(message: Message, requestId: Snowflake) {
const thread = await message.startThread({
name,
rateLimitPerUser: slowmode,
autoArchiveDuration: getSafeDefaultAutoArchiveDuration(channel),
autoArchiveDuration: channel.defaultAutoArchiveDuration ?? 1440, // 24h
});

const closeButton = new MessageButton()
Expand All @@ -134,6 +135,7 @@ async function autoCreateThread(message: Message, requestId: Snowflake) {
const overrideMessageContent = getConfig(guild.id).threadChannels?.find(
x => x?.channelId === channel.id
)?.messageContent;

const msgContent = overrideMessageContent
? replaceMessageVariables(overrideMessageContent, requestId)
: getMessage("SUCCESS_THREAD_CREATE", requestId);
Expand All @@ -146,6 +148,7 @@ async function autoCreateThread(message: Message, requestId: Snowflake) {

if (botMember.permissionsIn(thread.id).has(Permissions.FLAGS.MANAGE_MESSAGES)) {
await msg.pin();
await wait(50); // Let's wait a few ms here to ensure the latest message is actually the pin message
await thread.lastMessage?.delete();
}
}
Expand Down
25 changes: 1 addition & 24 deletions src/helpers/permissionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ You should have received a copy of the GNU Affero General Public License along w
If not, see <https://www.gnu.org/licenses/>.
*/

import {
type GuildMember,
type NewsChannel,
Permissions,
type TextChannel,
type ThreadAutoArchiveDuration,
} from "discord.js";
import { type GuildMember, Permissions } from "discord.js";

export function getRequiredPermissions(slowmode?: number): bigint[] {
const output = [
Expand All @@ -44,20 +38,3 @@ export function memberIsModerator(member: GuildMember): boolean {
export function memberIsAdmin(member: GuildMember): boolean {
return member.permissions.has(Permissions.FLAGS.ADMINISTRATOR);
}

// Fixes https://github.com/MarcusOtter/discord-needle/issues/23
// Should not be required, but Discord for some reason allows the default duration to be higher than the allowed value
export function getSafeDefaultAutoArchiveDuration(channel: TextChannel | NewsChannel): ThreadAutoArchiveDuration {
const archiveDuration = channel.defaultAutoArchiveDuration;
if (!archiveDuration || archiveDuration === "MAX") return "MAX";

const highest = getHighestAllowedArchiveDuration(channel);
return archiveDuration > highest ? highest : archiveDuration;
}

function getHighestAllowedArchiveDuration(channel: TextChannel | NewsChannel) {
if (channel.guild.features.includes("SEVEN_DAY_THREAD_ARCHIVE")) return 10080;
if (channel.guild.features.includes("THREE_DAY_THREAD_ARCHIVE")) return 4320;

return 1440; // 1d
}
3 changes: 3 additions & 0 deletions src/helpers/promiseHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function wait(milliseconds: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}

0 comments on commit ae065e5

Please sign in to comment.