rename bot directory to src, add chatwidget

This commit is contained in:
2025-07-09 16:50:16 +02:00
parent 8fd889856b
commit 3e025a586a
59 changed files with 417 additions and 1 deletions

51
src/items/blaster.ts Normal file
View File

@@ -0,0 +1,51 @@
import { changeItemCount, Item } from ".";
import { sendMessage } from "../commands";
import { createTimeoutRecord } from "../db/dbTimeouts";
import { createUsedItemRecord } from "../db/dbUsedItems";
import { getUserRecord } from "../db/dbUser";
import parseCommandArgs from "../lib/parseCommandArgs";
import { timeout } from "../lib/timeout";
import { User } from "../user";
const ITEMNAME = 'blaster';
export default new Item(ITEMNAME, 'Blaster', 's',
'Times a specific person out for 60 seconds',
['blaster', 'blast'],
async (msg, user) => {
const userObj = await getUserRecord(user);
if (userObj.inventory[ITEMNAME]! < 1) { await sendMessage(`You don't have any blasters!`, msg.messageId); return; };
const messagequery = parseCommandArgs(msg.messageText);
if (!messagequery[0]) { await sendMessage('Please specify a target'); return; };
const target = await User.initUsername(messagequery[0].toLowerCase());
if (!target) { await sendMessage(`${messagequery[0]} doesn't exist`); return; };
await getUserRecord(target); // make sure the user record exist in the database
if (await user.itemLock()) { await sendMessage('Cannot use an item right now', msg.messageId); return; };
await user.setLock();
const result = await timeout(target, `You got blasted by ${user.displayName}!`, 60);
if (result.status) await Promise.all([
sendMessage(`GOTTEM ${target.displayName} got BLASTED by ${user.displayName} GOTTEM`),
changeItemCount(user, userObj, ITEMNAME),
createTimeoutRecord(user, target, ITEMNAME),
createUsedItemRecord(user, ITEMNAME)
]);
else {
switch (result.reason) {
case "banned":
await sendMessage(`${target.displayName} is already timed out/banned`, msg.messageId);
break;
case "illegal":
await Promise.all([
sendMessage(`${user.displayName} Nou Nou Nou`),
timeout(user, 'nah', 60)
]);
break;
case "unknown":
await sendMessage('Something went wrong...', msg.messageId);
break;
};
};
await user.clearLock();
}
);

37
src/items/grenade.ts Normal file
View File

@@ -0,0 +1,37 @@
import { redis } from "bun";
import { sendMessage } from "../commands";
import { timeout } from "../lib/timeout";
import { changeItemCount, Item } from ".";
import { User } from "../user";
import { getUserRecord } from "../db/dbUser";
import { createTimeoutRecord } from "../db/dbTimeouts";
import { createUsedItemRecord } from "../db/dbUsedItems";
const ITEMNAME = 'grenade';
export default new Item(ITEMNAME, 'Grenade', 's',
'Give a random chatter a 60s timeout',
['grenade'],
async (msg, user) => {
const userObj = await getUserRecord(user);
if (userObj.inventory[ITEMNAME]! < 1) { await sendMessage(`You don't have any grenades!`, msg.messageId); return; };
const targets = await redis.keys('vulnchatters:*');
if (targets.length === 0) { await sendMessage('No vulnerable chatters to blow up', msg.messageId); return; };
const selection = targets[Math.floor(Math.random() * targets.length)]!;
const target = await User.initUserId(selection.split(':')[1]!);
await getUserRecord(target!); // make sure the user record exist in the database
if (await user.itemLock()) { await sendMessage('Cannot use an item right now', msg.messageId); return; };
await user.setLock();
await Promise.all([
timeout(target!, `You got hit by ${user.displayName}'s grenade!`, 60),
redis.del(selection),
sendMessage(`wybuh ${target?.displayName} got hit by ${user.displayName}'s grenade wybuh`),
changeItemCount(user, userObj, ITEMNAME),
createTimeoutRecord(user, target!, ITEMNAME),
createUsedItemRecord(user, ITEMNAME)
]);
await user.clearLock();
}
);

63
src/items/index.ts Normal file
View File

@@ -0,0 +1,63 @@
import { EventSubChannelChatMessageEvent } from "@twurple/eventsub-base";
import { User } from "../user";
import { type userType } from "../commands";
export class Item {
public readonly name: string;
public readonly prettyName: string;
public readonly plural: string;
public readonly description: string;
public readonly aliases: string[];
public readonly usertype: userType;
public readonly execute: (message: EventSubChannelChatMessageEvent, sender: User) => Promise<void>;
public readonly disableable: boolean;
/** Creates an item object
* @param name - internal name of item
* @param prettyName - name of item for presenting to chat
* @param plural - plural appendage; example: lootbox(es)
* @param description - description of what item does
* @param aliases - alternative ways to activate item
* @param execution - code that gets executed when item gets used */
constructor(name: string, prettyName: string, plural: string, description: string, aliases: string[], execution: (message: EventSubChannelChatMessageEvent, sender: User) => Promise<void>) {
this.name = name;
this.prettyName = prettyName;
this.plural = plural;
this.description = description;
this.aliases = aliases;
this.usertype = 'chatter'; // Items are usable by everyone
this.execute = execution;
this.disableable = true;
};
};
import { readdir } from 'node:fs/promises';
import type { userRecord } from "../db/connection";
import { updateUserRecord } from "../db/dbUser";
const items = new Map<string, Item>;
const emptyInventory: inventory = {};
const itemarray: string[] = [];
const files = await readdir(import.meta.dir);
for (const file of files) {
if (!file.endsWith('.ts')) continue;
if (file === import.meta.file) continue;
const item: Item = await import(import.meta.dir + '/' + file.slice(0, -3)).then(a => a.default);
emptyInventory[item.name] = 0;
itemarray.push(item.name);
for (const alias of item.aliases) {
items.set(alias, item); // Since it's not a primitive type the map is filled with references to the item, not the actual object
};
};
export default items;
export { emptyInventory, itemarray };
export type inventory = {
[key: string]: number;
};
export async function changeItemCount(user: User, userRecord: userRecord, itemname: string, amount = -1): Promise<false | userRecord> {
userRecord.inventory[itemname] = userRecord.inventory[itemname]! += amount;
if (userRecord.inventory[itemname] < 0) return false;
await updateUserRecord(user, userRecord);
return userRecord;
};

51
src/items/silverbullet.ts Normal file
View File

@@ -0,0 +1,51 @@
import { changeItemCount, Item } from ".";
import { sendMessage } from "../commands";
import { createTimeoutRecord } from "../db/dbTimeouts";
import { createUsedItemRecord } from "../db/dbUsedItems";
import { getUserRecord } from "../db/dbUser";
import parseCommandArgs from "../lib/parseCommandArgs";
import { timeout } from "../lib/timeout";
import { User } from "../user";
const ITEMNAME = 'silverbullet';
export default new Item(ITEMNAME, 'Silver bullet', 's',
'Times a specific person out for 24 hours',
['execute', 'silverbullet'],
async (msg, user) => {
const userObj = await getUserRecord(user);
if (userObj.inventory[ITEMNAME]! < 1) { await sendMessage(`You don't have any silver bullets!`, msg.messageId); return; };
const messagequery = parseCommandArgs(msg.messageText);
if (!messagequery[0]) { await sendMessage('Please specify a target'); return; };
const target = await User.initUsername(messagequery[0].toLowerCase());
if (!target) { await sendMessage(`${messagequery[0]} doesn't exist`); return; };
await getUserRecord(target); // make sure the user record exist in the database
if (await user.itemLock()) { await sendMessage('Cannot use an item right now', msg.messageId); return; };
await user.setLock();
const result = await timeout(target, `You got blasted by ${user.displayName}!`, 60 * 60 * 24);
if (result.status) await Promise.all([
sendMessage(`${target.displayName} RIPBOZO RIPBOZO RIPBOZO RIPBOZO RIPBOZO RIPBOZO RIPBOZO`),
changeItemCount(user, userObj, ITEMNAME),
createTimeoutRecord(user, target, ITEMNAME),
createUsedItemRecord(user, ITEMNAME)
]);
else {
switch (result.reason) {
case "banned":
await sendMessage(`${target.displayName} is already timed out/banned`, msg.messageId);
break;
case "illegal":
await Promise.all([
sendMessage(`${user.displayName} Nou Nou Nou`),
timeout(user, 'nah', 60)
]);
break;
case "unknown":
await sendMessage('Something went wrong...', msg.messageId);
break;
};
};
await user.clearLock();
}
);

53
src/items/tnt.ts Normal file
View File

@@ -0,0 +1,53 @@
import { redis } from "bun";
import { sendMessage } from "../commands";
import { timeout } from "../lib/timeout";
import { changeItemCount, Item } from ".";
import { User } from "../user";
import { getUserRecord } from "../db/dbUser";
import { createTimeoutRecord } from "../db/dbTimeouts";
import { createUsedItemRecord } from "../db/dbUsedItems";
const ITEMNAME = 'tnt';
export default new Item(ITEMNAME, 'TNT', 's',
'Give 5-10 random chatters 60 second timeouts',
['tnt'],
async (msg, user) => {
const userObj = await getUserRecord(user);
if (userObj.inventory[ITEMNAME]! < 1) { await sendMessage(`You don't have any TNTs!`, msg.messageId); return; };
const vulntargets = await redis.keys('vulnchatters:*');
if (vulntargets.length === 0) { await sendMessage('No vulnerable chatters to blow up', msg.messageId); return; };
const targets = getTNTTargets(vulntargets);
if (await user.itemLock()) { await sendMessage('Cannot use an item right now', msg.messageId); return; };
await user.setLock();
await Promise.all(targets.map(async targetid => {
const target = await User.initUserId(targetid.split(':')[1]!);
await getUserRecord(target!); // make sure the user record exist in the database
await Promise.all([
timeout(target!, `You got hit by ${user.displayName}'s TNT!`, 60),
redis.del(targetid),
sendMessage(`wybuh ${target?.displayName} got hit by ${user.displayName}'s TNT wybuh`),
createTimeoutRecord(user, target!, ITEMNAME),
]);
}));
await Promise.all([
createUsedItemRecord(user, ITEMNAME),
changeItemCount(user, userObj, ITEMNAME)
]);
await user.clearLock();
await sendMessage(`RIPBOZO ${user.displayName} exploded ${targets.length} chatter${targets.length === 1 ? '' : 's'} with their TNT RIPBOZO`);
}
);
function getTNTTargets<T>(arr: T[]): T[] {
if (arr.length <= 5) {
return arr;
};
const count = Math.floor(Math.random() * 6) + 5; // Random number between 5 and 10
const shuffled = [...arr].sort(() => 0.5 - Math.random()); // Shuffle array
return shuffled.slice(0, Math.min(count, arr.length)); // Return up to `count` entries
};