diff --git a/src/commands/impl/vanish.ts b/src/commands/impl/vanish.ts new file mode 100644 index 0000000..a84f84b --- /dev/null +++ b/src/commands/impl/vanish.ts @@ -0,0 +1,28 @@ +import type { ChatMessage } from "@twurple/chat"; +import { purgeByIds } from "../../util/api-functions.ts"; +import { logSuccess } from "../../util/logger.ts"; +import { BaseCommand } from "../base-command.ts"; +import type { ICommandRequirements } from "../interface.ts"; + +export class VanishCommand extends BaseCommand { + name = "v"; + cooldown = 0; + enabled = true; + + requirements: ICommandRequirements = { + developer: false, + mod: false, + }; + + triggered = async ( + channel: string, + user: string, + _text: string, + msg: ChatMessage, + ) => { + logSuccess(`${channel} ${user} vanish command triggered`); + if (msg.channelId) { + purgeByIds(msg.channelId, msg.userInfo.userId); + } + }; +} diff --git a/src/util/api-functions.ts b/src/util/api-functions.ts new file mode 100644 index 0000000..725b12b --- /dev/null +++ b/src/util/api-functions.ts @@ -0,0 +1,61 @@ +import type { UserIdResolvable } from "@twurple/api"; +import { Config } from "../config/config"; +import { apiClient } from "../core/api-client"; +import { getUserId } from "./general"; +import { logError } from "./logger"; + +export async function timeoutByIds( + channelId: UserIdResolvable, + userId: UserIdResolvable, + duration: number, + reason: string, +): Promise { + if (!channelId || !userId || !duration) { + logError(`timout id command missing a required parameter`); + } + + await apiClient.asUser(Config.bot_user_id, async (apiClient) => + apiClient.moderation.banUser(channelId, { user: userId, duration, reason }), + ); +} + +export async function timeout( + channelName: string, + userName: string, + duration: number, + reason: string, +): Promise { + if (!channelName || !userName || !duration) { + logError(`timout name command missing a required parameter`); + } + + const channelId = await getUserId(channelName); + const userId = await getUserId(userName); + + await timeoutByIds(channelId, userId, duration, reason); +} + +export async function purgeByIds( + channelId: UserIdResolvable, + userId: UserIdResolvable, +): Promise { + if (!channelId || !userId) { + logError(`purge id command missing a required parameter`); + } + + await timeoutByIds(channelId, userId, 1, "purged"); +} + +export async function purge( + channelName: string, + userName: string, +): Promise { + if (!channelName || !userName) { + logError(`purge name command missing a required parameter`); + } + + const channelId = await getUserId(channelName); + const userId = await getUserId(userName); + + await purgeByIds(channelId, userId); +} diff --git a/src/util/general.ts b/src/util/general.ts index 4a527f8..8fd5431 100644 --- a/src/util/general.ts +++ b/src/util/general.ts @@ -1,15 +1,16 @@ import * as readline from "node:readline"; +import type { UserIdResolvable } from "@twurple/api"; import { apiClient } from "../core/api-client.ts"; import { logSuccess, logWarning } from "./logger.ts"; -export async function getUserId(username: string): Promise { +export async function getUserId(username: string): Promise { const user = await apiClient.users.getUserByName(username); if (user?.id) { logSuccess(`${user.name} => ${user.id}`); return user.id; } logWarning(`no user with name ${username} found`); - return null; + return ""; } export async function promptForInput(prompt: string): Promise {