Skip to content

Commit

Permalink
add permanent bans, slow mode, renamed package for release
Browse files Browse the repository at this point in the history
  • Loading branch information
retconned committed Sep 14, 2024
1 parent c5276ff commit 28ca9f8
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# kickbot-sdk
# kick-js

## 0.1.1

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
**kickbot-sdk**
**kick-js**

A demo package for Total TypeScript.
30 changes: 29 additions & 1 deletion examples/basic-bot.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import { createClient } from "../src";
import type { MessageData } from "../src/types/events";
import "dotenv/config";

const client = createClient("xqc", { logger: true });

client.on("ready", () => {
console.log(`Logged into ${client.user?.tag}!`);
console.log(`Bot ready & logged into ${client.user?.tag}!`);
});

client.login({ process.env.TOKEN, process.env.COOKIES });

client.on("ChatMessage", async (message: MessageData) => {
console.log(`${message.sender.username}: ${message.content}`);

if (message.content.match("!ping")) {
client.sendMessage("pong");
}

if (message.content.match("!slowmode on")) {
const splitMessage = message.content.split(" ");
const duration = splitMessage[1];
if (duration) {
const durationNumber = parseInt(duration);
client.slowMode("on", durationNumber);
}
}
if (message.content.match("!slowmode off")) {
client.slowMode("off");
}

if (message.content.match("!ban")) {
const splitMessage = message.content.split(" ");
const bannedUser = splitMessage[1];
if (bannedUser) {
client.permanentBan(bannedUser);
}
}

});

client.on("Subscription", async (subscription) => {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "kickbot-sdk",
"name": "kick-js",
"version": "0.1.1",
"description": "A typescript bot sdk for kick.com",
"description": "A typescript bot interface for kick.com",
"keywords": [
"demo",
"typescript"
],
"homepage": "https://github.com/retconned/kickbot-sdk",
"homepage": "https://github.com/retconned/kick-js",
"bugs": {
"url": "https://github.com/retconned/kickbot-sdk/issues"
"url": "https://github.com/retconned/kick-js/issues"
},
"author": "retconned <retcnned@gmail.com>",
"repository": {
"type": "git",
"url": "git+https://github.com/retconned/kickbot-sdk.git"
"url": "git+https://github.com/retconned/kick-js.git"
},
"files": [
"dist"
Expand Down
104 changes: 101 additions & 3 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { createWebSocket } from "../core/websocket";
import { parseMessage } from "../utils/messageHandling";
import type { KickChannelInfo } from "../types/channels";
import type { KickClient, ClientOptions } from "../types/client";
import type { MessageData } from "../types/events";

import axios from "axios";
import type { MessageData } from "../types/events";

export const createClient = (
channelName: string,
Expand Down Expand Up @@ -87,7 +87,7 @@ export const createClient = (
emitter.on(event, listener);
};

// TODO: Implement authentication, this is just a placeholder
// TODO: Implement proper authentication, this is just a temp token & cookies passer
const login = async (credentials: { token: string; cookies: string }) => {
token = credentials.token;
cookies = credentials.cookies;
Expand Down Expand Up @@ -129,14 +129,112 @@ export const createClient = (
}
};

const permanentBan = async (bannedUser: string) => {
if (!token || !cookies || !channelInfo) {
throw new Error("Not logged in or channel info not available");
}

if (!bannedUser) {
throw new Error("Specify a user to ban");
}

try {
const response = await axios.post(
`https://kick.com/api/v2/channels/${channelInfo.id}/bans`,
{ banned_username: bannedUser, permanent: true },
{
headers: {
accept: "application/json, text/plain, */*",
authorization: `Bearer ${token}`,
"content-type": "application/json",
"x-xsrf-token": token,
cookie: cookies,
Referer: `https://kick.com/${channelInfo.slug}`,
},
},
);

if (response.status === 200) {
console.log(`Banned user ${bannedUser} sent successfully`);
} else {
console.error(`Failed to Ban user. Status: ${response.status}`);
}
} catch (error) {
console.error("Error sending message:", error);
}
};
const slowMode = async (mode: "on" | "off", durationInSeconds?: number) => {
if (!token || !cookies || !channelInfo) {
throw new Error("Not logged in or channel info not available");
}
if (mode !== "on" && mode !== "off") {
throw new Error("Invalid mode, must be 'on' or 'off'");
}
if (mode === "on" && durationInSeconds && durationInSeconds < 1) {
throw new Error(
"Invalid duration, must be greater than 0 if mode is 'on'",
);
}

try {
if (mode === "off") {
const response = await await axios.put(
`https://kick.com/api/v2/channels/${channelInfo.slug}/chatroom`,
{ slow_mode: false },
{
headers: {
accept: "application/json, text/plain, */*",
authorization: `Bearer ${token}`,
"content-type": "application/json",
"x-xsrf-token": token,
cookie: cookies,
Referer: `https://kick.com/${channelInfo.slug}`,
},
},
);

if (response.status === 200) {
console.log(`Turned slow mode off successfully`);
} else {
console.error(`Failed to Ban user. Status: ${response.status}`);
}
} else {
const response = await await axios.put(
`https://kick.com/api/v2/channels/${channelInfo.slug}/chatroom`,
{ slow_mode: true, message_interval: durationInSeconds },
{
headers: {
accept: "application/json, text/plain, */*",
authorization: `Bearer ${token}`,
"content-type": "application/json",
"x-xsrf-token": token,
cookie: cookies,
Referer: `https://kick.com/${channelInfo.slug}`,
},
},
);

if (response.status === 200) {
console.log(`Turned slow mode on for ${durationInSeconds} seconds`);
} else {
console.error(`Failed to Ban user. Status: ${response.status}`);
}
}
} catch (error) {
console.error("Error sending message:", error);
}
};

void initialize();

return {
on,
get user() {
return getUser();
},
sendMessage,
login,
sendMessage,
permanentBan,
slowMode,
};
};
8 changes: 0 additions & 8 deletions src/types/actions.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export interface ClientOptions {

export interface KickClient {
on: (event: string, listener: (...args: any[]) => void) => void;
login: (credentials: { token: string; cookies: string }) => Promise<void>;
user: {
id: number;
username: string;
tag: string;
} | null;
sendMessage: (messageContent: string) => Promise<void>;
login: (credentials: { token: string; cookies: string }) => Promise<void>;
permanentBan: (bannedUser: string) => Promise<void>;
slowMode: (mode: "on" | "off", durationInSeconds?: number) => Promise<void>;
}
2 changes: 1 addition & 1 deletion src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export interface ChatMessage {
};
}

// these are not implemented yet
export interface Subscription {
// Define subscription event properties
id: string;
user_id: number;
username: string;
Expand Down

0 comments on commit 28ca9f8

Please sign in to comment.