created timeout function and minor changes

This commit is contained in:
2025-06-24 16:13:38 +02:00
parent 39448cbad4
commit 0437a6ba3c
6 changed files with 64 additions and 16 deletions

View File

@@ -33,16 +33,9 @@ for (const file of files) {
export default commands;
export { intents };
import { singleUserMode, chatterApi, chatterId, streamerId, streamerApi } from "..";
import { singleUserMode, chatterApi, chatterId, streamerId } from "..";
/** Helper function to send a message to the stream */
export const sendMessage = async (message: string, replyParentMessageId?: string) => {
singleUserMode ? await chatterApi.chat.sendChatMessage(streamerId, message, { replyParentMessageId }) : chatterApi.asUser(chatterId, async newapi => newapi.chat.sendChatMessage(streamerId, message, { replyParentMessageId }));
};
/** Helper function to timeout a specific user */
export const doTimeout = async (userid: string, reason: string, duration = 60) => {
// TODO: make sure mods lose their sword, THEN get timed out, and get the sword back after timeout expires (check v1 code for implementation)
if ([chatterId, streamerId].includes(userid)) return; // make sure unbannable users don't get banned
await streamerApi.moderation.banUser(streamerId, { user: userid, reason, duration });
};

View File

@@ -1,10 +1,11 @@
import { Command, doTimeout, sendMessage } from ".";
import { Command, sendMessage } from ".";
import { timeout } from "../lib/timeout";
// Remake of the !yabai command in ttv/kiara_tv
export default new Command('yabai',
['yabai', 'goon'],
['moderator:manage:banned_users'],
async msg => {
async (msg, user) => {
const rand = Math.floor(Math.random() * 100) + 1;
if (rand < 25) sendMessage(`${rand}% yabai! GIGACHAD`, msg.messageId);
else if (rand < 50) sendMessage(`${rand}% yabai POGGERS`, msg.messageId);
@@ -12,7 +13,7 @@ export default new Command('yabai',
else if (rand < 90) sendMessage(`${rand}% yabai AINTNOWAY`, msg.messageId);
else {
sendMessage(`${msg.chatterDisplayName} is ${rand}% yabai CAUGHT`);
doTimeout(msg.chatterId, 'TOO SUS');
timeout(user, "TOO YABAI!", 60);
};
}
);

View File

@@ -4,7 +4,7 @@ import { EventSubHttpListener, ReverseProxyAdapter } from "@twurple/eventsub-htt
import { intents } from "./commands";
const CHATTERBASEINTENTS = ["user:read:chat", "user:write:chat", "user:bot"];
const STREAMERBASEINTENTS = ["user:read:chat", "channel:bot", "moderation:read"];
const STREAMERBASEINTENTS = ["user:read:chat", "moderation:read", "channel:manage:moderators"];
export const singleUserMode = process.env.CHATTER_IS_STREAMER === 'true';
export const chatterId = process.env.CHATTER_ID ?? "";
@@ -38,4 +38,6 @@ export const eventSub = new EventSubHttpListener({
export const commandPrefix = process.env.COMMAND_PREFIX ?? "!";
export const unbannableUsers = [chatterId, streamerId]
await import("./events");

52
bot/lib/timeout.ts Normal file
View File

@@ -0,0 +1,52 @@
import { streamerApi, streamerId, unbannableUsers } from "..";
import { User } from "../user";
type SuccessfulTimeout = { status: true };
type UnSuccessfulTimeout = { status: false; reason: 'noexist' | 'banned' | 'unknown' | 'illegal' };
type TimeoutResult = SuccessfulTimeout | UnSuccessfulTimeout;
/** Give a user a timeout/ban
* @param target - userid or User class to timeout/ban
* @param reason - reason for timeout/ban
* @param duration - duration of timeout. don't specifiy for ban */
export const timeout = async (target: string | User, reason: string, duration?: number): Promise<TimeoutResult> => {
// set the user object to either the predefined user obj or a new user obj
const user = typeof (target) === 'string' ? await User.initUsername(target) : target;
if (!user) return { status: false, reason: 'noexist' };
if (unbannableUsers.includes(user.id)) return { status: false, reason: 'illegal' };
// Check if user already has a timeout
const banStatus = await streamerApi.moderation.getBannedUsers(streamerId, { userId: user.id }).then(a => a.data);
if (banStatus[0]) return { status: false, reason: 'banned' };
if (await streamerApi.moderation.checkUserMod(streamerId, user.id!)) {
if (!duration) duration = 60;
remodMod(user, duration);
await streamerApi.moderation.removeModerator(streamerId, user.id!);
};
try {
await streamerApi.moderation.banUser(streamerId, { user: user.id, reason, duration });
} catch (err) {
console.error(err);
return { status: false, reason: 'unknown' }
};
return { status: true };
};
/** Give the target mod status back after timeout */
function remodMod(target: User, duration: number) {
setTimeout(async () => {
const bandata = await streamerApi.moderation.getBannedUsers(streamerId, { userId: target.id }).then(a => a.data);
if (bandata[0]) { // If the target is still timed out, try again when new timeout expires
const timeoutleft = Date.parse(bandata[0].expiryDate?.toString()!) - Date.now(); // date when timeout expires - current date
remodMod(target, timeoutleft); // Call the current function with new time (recursion)
} else {
try {
await streamerApi.moderation.addModerator(streamerId, target.id);
} catch (err) { }; // This triggers when the timeout got shortened. try/catch so no runtime error
};
}, duration + 3000); // callback gets called after duration of timeout + 3 seconds
};

View File

@@ -5,9 +5,9 @@ import { HelixUser } from "@twurple/api"
const EXPIRETIME = 60 * 30 // 30 minutes
export class User {
public username: string | undefined;
public id: string | undefined;
public displayName: string | undefined;
public username!: string;
public id!: string;
public displayName!: string;
static async initUsername(username: string): Promise<User | null> {
const userObj = new User();

View File

@@ -12,7 +12,7 @@
"@types/bun": "latest",
},
"peerDependencies": {
"typescript": "^5",
"typescript": "^5.8.3",
},
},
},